Is it possible to wrap the text of xticks in matplotlib in python? Is it possible to wrap the text of xticks in matplotlib in python? django django

Is it possible to wrap the text of xticks in matplotlib in python?


Perhaps try:

ax.set_xticklabels(labels, rotation=45)

Thanks to Amro for pointing out that rotation can be any degree.


If you want to wrap the labels manually you can insert a '\n' into the label name, which will break the label into two lines at the point you have the '\n'. You can see an example of this here.

There also appears to be an autowrap function now that seems to do the trick nicely. This example uses plt.text, but it's an attribute you can specify with plt.xticks, too. (i.e. wrap=True) I found this kind of messed up the alignment of the labels, so I needed to tweak the horizontal and vertical alignments as well.