繁体   English   中英

如何在python中自定义颜色条?

[英]How to customize the colorbar in python?

使用此代码,我不知道如何自定义颜色栏。 这个网站上的色彩图让我不满意。

shade = m.contourf(Lon,Lat,TBB,np.arange(-90, -20, 10),extend='both',cmap=plt.cm.get_cmap('jet'))       
m.colorbar(shade)

my_colorbar

我想用明显的颜色条来绘制这样的图片。 所以我该怎么做? 彩条

您可以使用matplotlib.colors.LinearSegmentedColormap()matplotlib.colors.ListedColormap()定义自己的颜色图,并将其用于绘图。

例:

import numpy as np; np.random.seed(0)
import matplotlib.pyplot as plt
import matplotlib.colors

x = np.arange(0,25)
a = np.random.randint(0,130, size=(25,25))-115
a = np.sort(a).reshape(25,25)

colors = ["#eaa941", "#efef39", "#53a447", "#3b387f", "#48a2ba"]
cmap= matplotlib.colors.ListedColormap(colors)
cmap.set_under("crimson")
cmap.set_over("w")
norm= matplotlib.colors.Normalize(vmin=-100,vmax=-0)

fig, ax = plt.subplots()
im = ax.contourf(x,x,a, levels=[-100,-80,-60,-40,-20,0],
                 extend='both',cmap=cmap, norm=norm)

fig.colorbar(im, extend="both")

plt.show()

在此处输入图片说明

看起来很像在matplotlib页面上给出的spectral颜色图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM