此问题已在此处有答案:
Change values on matplotlib imshow() graph axis(3个答案)
4天前关闭。
我有一些python代码,它在x和y值的范围内应用一个2-D函数,并使用matplotlib将结果绘制为色彩图。但是,此图上的轴显示输出数组的整数索引。相反,我希望这些轴显示x和y值的范围,从-1.0
到1.0
,就像一个典型的图形应用程序。
如何设置坐标轴的范围?
import matplotlib.pyplot as plt
# let x, y values be in range of (-1, 1)
x, y = np.mgrid[-1:1:.05, -1:1:.05]
# Apply the function to the values
z = x * y
# Get matplotlib figure and axis
fig, ax = plt.subplots(figsize=(3, 3), ncols=1)
# Plot the colormap
pos_neg_clipped = ax.imshow(z, cmap='RdBu', interpolation='none')
# Display the image
plt.show()
输出:
1条答案
按热度按时间6qfn3psc1#
不要使用
imshow
,它只应该用于绘制图像,而使用pcolormesh
,您可以为轴传递x
和y
值。