Python 更改 matplotlib imshow() 图形轴上的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18696122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change values on matplotlib imshow() graph axis
提问by atomh33ls
Say I have some input data:
假设我有一些输入数据:
data = np.random.normal(loc=100,scale=10,size=(500,1,32))
hist = np.ones((32,20)) # initialise hist
for z in range(32):
hist[z],edges = np.histogram(data[:,0,z],bins=np.arange(80,122,2))
I can plot it using imshow()
:
我可以使用imshow()
以下方法绘制它:
plt.imshow(hist,cmap='Reds')
getting:
得到:
However, the x-axis values do not match the input data (i.e. mean of 100, range from 80 to 122). Therefore, I'd like to change the x-axis to show the values in edges
.
但是,x 轴值与输入数据不匹配(即 100 的平均值,范围从 80 到 122)。因此,我想更改 x 轴以显示edges
.
I have tried:
我试过了:
ax = plt.gca()
ax.set_xlabel([80,122]) # range of values in edges
...
# this shifts the plot so that nothing is visible
and
和
ax.set_xticklabels(edges)
...
# this labels the axis but does not centre around the mean:
Any ideas on how I can change the axis values to reflect the input data I am using?
关于如何更改轴值以反映我正在使用的输入数据的任何想法?
采纳答案by Rutger Kassies
I would try to avoid changing the xticklabels
if possible, otherwise it can get very confusing if you for example overplot your histogram with additional data.
xticklabels
如果可能的话,我会尽量避免更改,否则,如果您使用其他数据过度绘制直方图,则会变得非常混乱。
Defining the range of your grid is probably the best and with imshow
it can be done by adding the extent
keyword. This way the axes gets adjusted automatically. If you want to change the labels i would use set_xticks
with perhaps some formatter. Altering the labels directly should be the last resort.
定义网格的范围可能是最好的,imshow
并且可以通过添加extent
关键字来完成。这样,轴会自动调整。如果您想更改标签,我可能会使用set_xticks
一些格式化程序。直接改变标签应该是最后的手段。
fig, ax = plt.subplots(figsize=(6,6))
ax.imshow(hist, cmap=plt.cm.Reds, interpolation='none', extent=[80,120,32,0])
ax.set_aspect(2) # you may also use am.imshow(..., aspect="auto") to restore the aspect ratio
回答by rxs
I had a similar problem and google was sending me to this post. My solution was a bit different and less compact, but hopefully this can be useful to someone.
我有一个类似的问题,谷歌将我发送到这篇文章。我的解决方案有点不同,也不那么紧凑,但希望这对某人有用。
Showing your image with matplotlib.pyplot.imshow is generally a fast way to display 2D data. However this by default labels the axes with the pixel count. If the 2D data you are plotting corresponds to some uniform grid defined by arrays x and y, then you can use matplotlib.pyplot.xticks and matplotlib.pyplot.yticks to label the x and y axes using the values in those arrays. These will associate some labels, corresponding to the actual grid data, to the pixel counts on the axes. And doing this is much faster than using something like pcolor for example.
使用 matplotlib.pyplot.imshow 显示图像通常是显示 2D 数据的快速方法。但是,默认情况下,这会使用像素数标记轴。如果您绘制的二维数据对应于由数组 x 和 y 定义的某个统一网格,那么您可以使用 matplotlib.pyplot.xticks 和 matplotlib.pyplot.yticks 使用这些数组中的值标记 x 和 y 轴。这些会将与实际网格数据相对应的一些标签与轴上的像素计数相关联。这样做比使用诸如 pcolor 之类的东西要快得多。
Here is an attempt at this with your data:
这是对您的数据的尝试:
import matplotlib.pyplot as plt
# ... define 2D array hist as you did
plt.imshow(hist, cmap='Reds')
x = np.arange(80,122,2) # the grid to which your data corresponds
nx = x.shape[0]
no_labels = 7 # how many labels to see on axis x
step_x = int(nx / (no_labels - 1)) # step between consecutive labels
x_positions = np.arange(0,nx,step_x) # pixel count at label position
x_labels = x[::step_x] # labels you want to see
plt.xticks(x_positions, x_labels)
# in principle you can do the same for y, but it is not necessary in your case