Quantcast
Channel: Wrapping long y labels in matplotlib tight layout using setp - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Wrapping long y labels in matplotlib tight layout using setp

$
0
0

I've been trying to wrap text for long labels in my code. I tried the textwrap method suggested earlier here, but my code defines yticklabels through an array imported from a csv using the pyplot.setp() method. I'm using tight_layout() for the formatting otherwise.

So the question is - is there a way to wrap the really long y labels to newlines easily?

Here is some sample code that I'd like a fix for:

import numpy as npimport matplotlib.pyplot as pltlabels=('Really really really really really really long label 1', 'Really really really really really really long label 2', 'Really really really really really really long label 3')values=(30,50,40)fig = plt.figure()ax=fig.add_subplot(111)plt.ylim((0,40))for i 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 something like thisenter image description hereI'd like the labels to go to newlines after a fixed width. Any ideas?


Viewing all articles
Browse latest Browse all 3

Trending Articles





<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>