Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yticklabels only at major ticks matplotlib

I have a problem regarding yticklabels in matplotlib.

I' m trying to make a vertical barplot (plt.barh) and then trying to use ax.set_yticklabels command. The problem that I have is that it only puts the labels at the major ticks! The list that I'm passing is of length 18, however it only labels 10 of the bars!!

Help please?

like image 377
jonas Avatar asked Aug 15 '13 08:08

jonas


2 Answers

you need to set yticks before you set yticklabels:

from numpy import *
import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(111)
x=random.uniform(0,5,size=5)

#plot
ax.barh(arange(len(x)),x,1)

#set ticks
T=arange(len(x))+0.5
ax.set_yticks(T)

#set labels
labels=['a','b','c','d','e']
ax.set_yticklabels(labels)

plt.show()
like image 67
user1665220 Avatar answered Sep 27 '22 17:09

user1665220


You can use set_yticks, but passing the argument minor True or False:

majorticks = [1., 2., 3.]
minorticks = [1.5, 2.5]
ax.set_yticks( minorticks, minor=True )
ax.set_yticks( majorticks, minor=False ) # The default in version 1.3.0

The same works for set_yticklabels...

like image 41
Saullo G. P. Castro Avatar answered Sep 27 '22 18:09

Saullo G. P. Castro