Change the position (movement) of label labels when plotting using matplotlib

I draw using matplotlib. The code is as follows (zvals matters)

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 leads to the following figure: http://imageshack.us/a/img145/7325/histogrammoverview.png

I would like to change the labels a bit on xticks (1,2,3, ...) on the left, so they are under the corresponding color fields. accordingly, I would also like to slightly change the labels of yticks (user1 and user2) so that they display correctly. How can I do that?

EDIT: essentially, I could change the following line xlocations = na.array (range (30)) + 0.5 to xlocations = na.array (range (30))

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

please see that the grid goes “through” the colored rectangles, which I don’t want. I would like the grid to crop the colored rectangles, as in the above image. in this version, although the labels (1,2,3, ...) are located correctly under the boxes. how can I correctly place the labels (under the colored rectangles) and the grid located around the colored boxes, and not through the middle of the colored rectangles.

Decision

this solution works (as suggested in response):

 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

+4
source share
1 answer

I think you can handle it

  • Set the main tick points in the middle of each square.
  • Set small ticks along the edges of each square.
  • Grid setting to display only in minor ticks.

Grid can only be displayed in minor ticks using

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

I would set the line style to '-' .

+2
source

Source: https://habr.com/ru/post/1439160/


All Articles