I'm trying to plot a graph with long x label strings. This post describes a similar problem (how to autowrap y-axis labels with tight_layout
) but doesn't work for me because tight_layout
changes the scale. This other post describes an approach that does NOT achieve automatic wrapping of axis labels - the package textwrap
requires one to specify the number of characters to wrap around.
My code:
import matplotlib.pyplot as plt
plt.figure()
plt.bar([0,1,2,3],[0,1,4,9])
labels = ['superdupersuperduperlonglabel0','superdupersuperduperlonglabel1',
'superdupersuperduperlonglabel2','superdupersuperduperlonglabel3']
plt.xticks([0,1,2,3],labels, wrap = True)
plt.show()
This is the resulting plot, with x labels overlapping:
According to matplotlib documentation:
Text properties can be used to control the appearance of the labels.
But wrap = True
doesn't seem to wrap the x-labels properly. Any suggestion?