views:

1389

answers:

3

I'm using matplotlib to generate a (vertical) barchart. The problem is my labels are rather long. Is there any way to display them vertically, either in the bar or above it or below it?

Note: The best answer will get a 500 bounty.

+3  A: 

Do you mean something like this:

>>> from matplotlib import *
>>> plot(xrange(10))
>>> yticks(xrange(10), rotation='vertical')

?

In general, to show any text in matplotlib with a vertical orientation, you can add the keyword rotation='vertical'.

For further options, you can look at help(matplotlib.pyplot.text)

The yticks function plots the ticks on the y axis; I am not sure whether you originally meant this or the ylabel function, but the procedure is alwasy the same, you have to add rotation='vertical'

Maybe you can also find useful the options 'verticalalignment' and 'horizontalalignment', which allows you to define how to align the text with respect to the ticks or the other elements.

dalloliogm
Note that I'm not using plot(); but bar(). But rotation='vertical' seems to be the key. However, this still doesn't draw the ticks in the bars.
phihag
Do you mean the ticks in the xy axis?Have a look at the grid function. For example grid(ls='', marker='v') .grid() controls the plotting of the grid on the xy axis; with the marker option you define the stile for the tick markers, and with ls='' you get an invisible grid.
dalloliogm
@dalloliogm sounds good
phihag
Unless a better answer comes, you'll get the bounty, promise. I'll be without internet access until 2009-8-9 though :(
phihag
Thanks!! However, maybe 500 points is too much for such an answer, it wasn't so difficult. So if you, you can save the points for another time :)
dalloliogm
+1  A: 

I would suggest looking at the matplotlib gallery. At least two of the examples seem to be relevant:

  • text_rotation.py for understanding how text layout works
  • barchart_demo2.py, an example of a bar chart with somewhat more complicated layout than the most basic example.
Jouni K. Seppänen
+1  A: 

My solution doesn't use matplotlib. However, I know gnuplot can do what you are asking.

Steven