numpy 如何更改matplotlib色彩Map图中的轴编号?[副本]

eaf3rand  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(146)

此问题已在此处有答案

Change values on matplotlib imshow() graph axis(3个答案)
4天前关闭。
我有一些python代码,它在x和y值的范围内应用一个2-D函数,并使用matplotlib将结果绘制为色彩图。但是,此图上的轴显示输出数组的整数索引。相反,我希望这些轴显示x和y值的范围,从-1.01.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()

输出:

6qfn3psc

6qfn3psc1#

不要使用imshow,它只应该用于绘制图像,而使用pcolormesh,您可以为轴传递xy值。

import numpy as np
import matplotlib.pyplot as plt

plt.close("all")

x, y = np.mgrid[-1:1:0.05, -1:1:0.05]
z = x*y

fig, ax = plt.subplots()
ax.pcolormesh(x, y, z, cmap="RdBu")
ax.set_aspect(1)
fig.show()

相关问题