Python Matplotlib,打印常量字段

2024-05-16 19:34:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我想画一个恒定的领域,但它不是很漂亮。。。 它的值是零,我想要一个介于-1和1之间的色条! 我试着这样做:

import numpy as np
import numpy.ma as ma
import matplotlib.pyplot as plt
from matplotlib import cm

plt.rcParams['text.usetex']=True
plt.rcParams['text.latex.unicode']=True

Lx=80.
Ly=120.
x0 = 30.
y0 = Ly/2.

YA, XA = np.mgrid[0:Ly, 0:Lx]

Order0 = 0*np.ones((YA.shape[0], YA.shape[1]))

plt.imshow(Order0,aspect='auto',cmap=plt.cm.hot,origin="lower")
plt.colorbar()
plt.show()


fig, ax = plt.subplots()
cax = ax.imshow(Order0, interpolation='nearest', cmap=cm.hot)
ax.set_title(r"\ test",fontsize=20)
plt.axis('off')

cbar = fig.colorbar(cax, ticks=[-1, -0.5, 0, 0.5, 1])
cbar.ax.set_yticklabels(['-1', '-0.5', '0','0.5' ' 1'])# vertically oriented colorbar
plt.show()

enter image description here

为了尊重其他结果,我希望得到这样的图像:


Tags: textimportnumpymatplotlibasnplycm
1条回答
网友
1楼 · 发布于 2024-05-16 19:34:35

我怀疑你想换台词:

plt.imshow(Order0,aspect='auto',cmap=plt.cm.hot,origin="lower")

要添加vmin和vmax:

plt.imshow(Order0,aspect='auto',cmap=plt.cm.hot,origin="lower", vmin=-1, vmax=1)

给予:

enter image description here

相关问题 更多 >