python - Wrapping long y labels in matplotlib tight layout using setp -
i've been trying wrap text long labels in code. tried textwrap method suggested earlier here, code defines yticklabels through array imported csv using pyplot.setp()
method. i'm using tight_layout()
formatting otherwise.
so question - there way wrap long y labels newlines easily?
here sample code i'd fix for:
import numpy np import matplotlib.pyplot plt labels=('really really really long label 1', 'really really really long label 2', 'really really really long label 3') values=(30,50,40) fig = plt.figure() ax=fig.add_subplot(111) plt.ylim((0,40)) in np.arange(3): plt.barh(15*i, values[i]) plt.yticks(15*np.arange(3)) plt.setp(ax.set_yticklabels(labels)) plt.tight_layout() plt.show()
this plots i'd labels go newlines after fixed width. ideas?
i have tried using textwrap
on labels , works me.
from textwrap import wrap labels=['really really really long label 1', 'really really really long label 2', 'really really really long label 3'] labels = [ '\n'.join(wrap(l, 20)) l in labels ]
inserting in code gives us:
Comments
Post a Comment