Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yticklabels Cut Off in Pandas plot

I'm making a simple horizontal stacked bar plot:

df = pd.DataFrame({'firstBox':firstL,'secondBox':secondL,'thirdBox':thirdL})
ax = df.plot.barh(stacked=True)
ax.set_title("My Example Plot")
ax.set_yticklabels(labels=['A label this long is cut off','this label is also cut off])
plt.show()

but the values on my ytick labels are being cut off. If I increase the width of the returned plot window, I can see slightly more of them, but I need to stretch the window well past the width of my monitor to see the entire label. Is there a way to just push the plot to the right to include my long labels?

Thanks!

like image 559
singmotor Avatar asked Jun 22 '17 00:06

singmotor


3 Answers

You should try this:

from matplotlib import rcParams
rcParams.update({'figure.autolayout': True})
like image 139
AaronDT Avatar answered Oct 12 '22 10:10

AaronDT


In case you are using savefig, bbox_inches='tight' is an option that automatically try to figure out the tight bbox of the figure.

like image 21
idansc Avatar answered Oct 12 '22 11:10

idansc


You can use ax.get_figure().tight_layout() or plt.gcf().tight_layout(). This will fix the figure layout.

like image 20
John Avatar answered Oct 12 '22 09:10

John