[英]How do I re-write the axes in matplotlib?
I have a 2D array and it's contents will display correctly as an image when I simply use我有一个二维数组,当我简单地使用它的内容将正确显示为图像
img = plt.imshow(full2DArray)
but my problem is that the axes just naively show the number of rows and columns.但我的问题是轴只是天真地显示了行数和列数。 For example if my 2D array is 53x53 then the axes will count 0-53 on the y-axis and 0-53 on the x-axis.
例如,如果我的二维数组是 53x53,那么轴将在 y 轴上计数 0-53,在 x 轴上计数 0-53。
I need to show the exact same image but have the axes display a linear scale from -130 to +130 instead.我需要显示完全相同的图像,但让轴显示从 -130 到 +130 的线性比例。
If I understand it correctly, you need predifined axis, instead of pyplot infering these from the image.如果我理解正确,您需要预定义轴,而不是 pyplot 从图像中推断这些轴。
Setting xlim before calling imshow will do the job.在调用imshow之前设置 xlim 将完成这项工作。
plt.xlim([-130, 130])
Similarly, you can call ylim for the y axis.同样,您可以为 y 轴调用 ylim。
I have a similar answer to this question here but to explain for your case, we can take an array data = np.random.rand(53,53)
filled with random values, and plot it with imshow
.我在这里对这个问题有类似的答案,但为了解释你的情况,我们可以使用一个填充随机值的数组
data = np.random.rand(53,53)
,并使用imshow
。 You simply need to adjust the extent=[<xmin>,<xmax>,<ymin>,<ymax>]
parameter, so in the example code:您只需调整
extent=[<xmin>,<xmax>,<ymin>,<ymax>]
参数,因此在示例代码中:
import numpy as np
import matplotlib.pyplot as plt
data = np.random.rand(53,53)
print(data.shape) # Displays (53,53)
plt.figure()
plt.xlabel("x")
plt.ylabel("y")
plt.imshow(data, origin='lower', aspect='auto',
extent = [-130,130,-130,130], cmap=plt.cm.jet)
plt.colorbar()
plt.show()
We get the following plot with your desired bounds:我们得到以下 plot 与您想要的界限:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.