How to remove margins from Matplotlib bar chart?
1
-
4Why have you added blurry screenshots of your code and the output rather than include them in the question? – roganjosh Oct 17 '17 at 19:48
Add a comment
|
You can set the limits of the axes to -.5
and len(df)-.5
to avoid the margins of the bar chart.
import matplotlib.pyplot as plt
import pandas as pd
plt.rcParams["patch.force_edgecolor"] = True
df = pd.DataFrame([1,2])
df.plot.bar(width=1)
plt.xlim(-0.5,len(df)-.5)
plt.show()
-
Also, if you use
plt.xlim(ax.patches[0].get_x(), ax.patches[-1].get_x())
you don't need to hardcode the values. – Raquel Jun 11 '20 at 14:33 -