1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python中等高线填充颜色_Python matplotlib使用colormap更改contourf plot中指定值的颜色...

python中等高线填充颜色_Python matplotlib使用colormap更改contourf plot中指定值的颜色...

时间:2021-07-25 08:44:49

相关推荐

python中等高线填充颜色_Python matplotlib使用colormap更改contourf plot中指定值的颜色...

我正在尝试使用colormap在matplotlib中创建一个填充的等高线图.

我想改变指定值的颜色.

例如,

levs = [-3,-1,1,3]

plt.contourf(x,y,z,levs,cmap=cm.jet,extend='both')

我希望-1和1之间的颜色为白色,保持其他颜色默认的colormap.

对不起,我的英语不好.

任何帮助,将不胜感激.

解决方法:

我只是绘制相同的轮廓图,但只是在-1和1之间的水平,颜色为白色.

例如:

from matplotlib import pyplot as plt

from matplotlib import cm

import numpy as np

x, y = np.mgrid[-100:100,-100:100]

x /= 10.

y /= 10.

r = np.sqrt(x*x+y*y)

z = 10*np.sin(r)/(r+0.01)

levels = [-4, -3, -2, -1, 1, 2, 3, 4]

plt.contourf(x, y, z, levels=levels, extend='both', cmap=cm.jet)

levels = [-1, 1]

plt.contourf(x, y, z, levels=levels, colors='w')

plt.savefig('contours.png')

更新

如果您想要更多控制,请将contourf中的colors关键字设置为matplotlib颜色的元组,其元素数量与您的级别相同(减1:颜色对应于级别边界之间的间隔).然后,您不需要绘制单独的轮廓,并且您的颜色条也是正确的:

# same as before

levels = [-4, -3, -2, -1, 1, 2, 3, 4]

plt.contourf(x, y, z, levels=levels, extend='both', colors=('#ff0000', '#ff9900', '#999900', 'w', '#009999', '#0099ff', '#0000ff'))

plt.savefig('contours.png')

更好的可能是定义自己的色彩映射,但是你可能会遇到一个问题,你必须将色彩映射与你的轮廓水平完全匹配.这就是为什么,方便地,颜色关键字存在的原因.

标签:python,matplotlib

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。