beta
beta

Reputation: 5686

change the position (move) of tick labels when plotting with matplotlib

i am plotting with matplotlib. the code is the following (zvals has the values)

cmap = mpl.colors.ListedColormap(['darkblue', 'blue', 'lightblue','lightgreen','yellow','gold','orange','darkorange','orangered','red'])
bounds=[0, 10,20,30,40,50,60,70,80,100,200,1000]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
img2 = plt.imshow(zvals,interpolation='nearest',
                cmap = cmap,
                norm=norm,
                origin='lower')


xlocations = na.array(range(30)) + 0.5
xticks(xlocations, [str(x+1) for x in arange(30)], rotation=0, size=5)
gca().xaxis.set_ticks_position('none')
gca().yaxis.set_ticks_position('none')  
grid(True)

this results in the following picture: http://imageshack.us/a/img145/7325/histogrammoverview.png

i would like to move the labels of the xticks (1,2,3,..) to the left a bit, so they are underneath the corresponding color boxes. correspondingly i would also like to move the labels of the yticks (user1 and user2) down a bit so they are displayed correctly. how can this be done?

EDIT: as a matter of fact i could change the following line xlocations = na.array(range(30)) + 0.5 to xlocations = na.array(range(30))

then the resulting pictures is like this: http://imageshack.us/a/img338/7325/histogrammoverview.png

please see that the grid is going "through" the colored boxes, which is not what i want. i'd like the grid to edge the colored boxes as in the above picture. in this version though the labels (1,2,3,...) are placed correctly underneath the boxes. how can i have correctly places labels (underneath the colored boxes) and a grid which is around the colored boxes and not through the middle of the colored boxes.

SOLUTION

this solution works (as suggested by the answer):

periods = 30
xlocations = na.array(range(periods))
xminorlocations = na.array(range(periods))+0.5
xticks(xlocations, [str(x+1) for x in arange(periods)], rotation=0, size=5)
plt.set_xticks(xminorlocations, minor=True)
grid(True, which='minor', linestyle='-')

result: hxxp://imageshack.us/a/img9/7325/histogrammoverview.png

Upvotes: 4

Views: 7056

Answers (1)

Pablo Navarro
Pablo Navarro

Reputation: 8264

I think that you can manage that by

  • Setting the major tick locations to the middle of each square.
  • Setting the minor ticks to the edges of each square.
  • Setting the grid to show only in the minor ticks.

The grid can be showed only in the minor ticks using

plt.grid(True, which='minor')

I would set the line style to '-' too.

Upvotes: 2

Related Questions