Barchart with vertical labels in python/matplotlib


Question

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?

1
53
7/20/2011 10:42:04 AM

Accepted Answer

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.

75
8/3/2009 8:52:09 AM

In Jupyter Notebook you might use something like this

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

plt.xticks(rotation='vertical')
plt.plot(np.random.randn(100).cumsum())

or you can use:

plt.xticks(rotation=90)

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon