使用matplotlib绘图时更改刻度标签的位置(移动) [英] change the position (move) of tick labels when plotting with matplotlib
问题描述
我正在用matplotlib密谋.代码如下(zvals具有值)
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)
这将导致下图: http://imageshack.us/a/img145/7325/histogrammoverview.png
我想将xticks(1,2,3,..)的标签向左移动一点,使其位于相应颜色框的下方.相应地,我也想将yticks(用户1和用户2)的标签向下移动一点,以便它们正确显示.该怎么办?事实上,我可以更改以下行 xlocations = na.array(range(30))+ 0.5 到 xlocations = na.array(range(30))
然后得到的图片是这样的: http://imageshack.us/a/img338/7325/histogrammoverview.png
请查看网格正在通过"彩色框,这不是我想要的.我希望网格如上图所示在彩色框的边缘.在此版本中,标签(1,2,3,...)正确地放置在框的下方.我如何才能正确地将标签(在彩色框下面)和一个网格放置在彩色框周围,而不是穿过彩色框的中间.
解决方案
此解决方案有效(根据答案的建议):
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='-')
结果:hxxp://imageshack.us/a/img9/7325/histogrammoverview.png
我认为您可以通过
进行管理- 将主要刻度位置设置为每个正方形的中间.
- 将次刻度设置为每个正方形的边缘.
- 将网格设置为仅在较小的刻度中显示.
使用只能在次要刻度中显示网格
plt.grid(True, which='minor')
我也将线型设置为'-'
.
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
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.
这篇关于使用matplotlib绘图时更改刻度标签的位置(移动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!