Matplotlib等高线图上颜色条的Python最小和最大范围

2024-05-26 00:33:29 发布

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

我正在尝试编辑我的轮廓图上的色条范围从0到0.12,我尝试了一些东西,但没有成功。我一直把全彩条的范围提高到0.3,这不是我想要的。

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as tri
triang = tri.Triangulation(x, y)

plt.tricontour(x, y, z, 15, colors='k')

plt.tricontourf(x, y, z, 15, cmap='Blues', vmin=0, vmax=0.12,\
                extend ='both')
plt.colorbar()

plt.clim(0,0.12)

plt.ylim (0.5,350)

plt.xlim(-87.5,87.5)

plt.show()

x,y,z都是一列多行的数组

你可以在这里查看我的图表:

enter image description here

请帮忙!


Tags: 代码importnumpy编辑matplotlibasnpplt
1条回答
网友
1楼 · 发布于 2024-05-26 00:33:29

我认为这个问题确实是正确的。 @Fatma90:您需要提供一个工作示例,在您的案例中提供x、y、z。

无论如何,我们可以自己创造一些价值观。所以问题是,vmin和vmax被plt.tricontourf()忽略了,我不知道有什么好的解决方案。

但是这里有一个解决方法,手动设置levels

plt.tricontourf(x, y, z, levels=np.linspace(0,0.12,11), cmap='Blues' )

这里我们使用了10个不同的级别,看起来很不错(如果使用了不同数量的级别,一个问题可能是有很好的记号)。

我提供了一个工作示例来说明效果:

import numpy as np
import matplotlib.pyplot as plt

#random numbers for tricontourf plot
x = (np.random.ranf(100)-0.5)*2.
y = (np.random.ranf(100)-0.5)*2.
#uniform number grid for pcolor
X, Y = np.meshgrid(np.linspace(-1,1), np.linspace(-1,1))

z = lambda x,y : np.exp(-x**2 - y**2)*0.12

fig, ax = plt.subplots(2,1)

# tricontourf ignores the vmin, vmax, so we need to manually set the levels
# in this case we use 11-1=10 equally spaced levels.
im = ax[0].tricontourf(x, y, z(x,y), levels=np.linspace(0,0.12,11), cmap='Blues' )
# pcolor works as expected
im2 = ax[1].pcolor(z(X,Y), cmap='Blues', vmin=0, vmax=0.12 )

plt.colorbar(im, ax=ax[0])
plt.colorbar(im2, ax=ax[1])

for axis in ax:
    axis.set_yticks([])
    axis.set_xticks([])
plt.tight_layout()
plt.show()

这就产生了

enter image description here

相关问题 更多 >