Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop the x-axis labels from getting cut off from the bottom of the bar graph?

Here's my code:

fig = plt.figure()
plt.figure(figsize=(15, 6))
plt.bar(x_labels, y_labels, width=0.9)
plt.xticks(rotation=90)

This yields this graph: matplotlib sucks

As you can see, the bottoms of the labels keep getting cut off. I know one solution is to use plt.savefig('rest.png', bbox_inches='tight') but how do I fit it all without using that argument in savefig()? How can I achieve that effect of bbox_inches='tight' without using plt.savefig()? What else can be done to fit it all?

like image 395
investigate311 Avatar asked Oct 29 '25 10:10

investigate311


2 Answers

try giving the x-axis a little bit of a buffer

fig.subplots_adjust(bottom=0.6)
like image 104
Peter Avatar answered Oct 31 '25 00:10

Peter


The line that worked for me while I was working on long tick marks was:

plt.tight_layout()
plt.show()
like image 22
Shubhayan Saha Avatar answered Oct 31 '25 00:10

Shubhayan Saha