Python Matplotlib/底图颜色循环 [英] Python Matplotlib/Basemap Color Cycling
问题描述
我是 Python 和其他程序的新手,我正在尝试使用 for 循环对集合成员绘制集合预测的 21 个成员,每个成员都有一个专用的数字 1-21.当我绘制地图时,所有成员都变成蓝色.我对颜色图进行了一些研究,发现默认图没有索引,即我不能像调用列表或数组一样调用 colormap[1].是否有任何索引的色彩图,或者有什么办法解决?简而言之,我想使用简单的数字循环显示颜色,为每个预测成员使用不同的颜色.
I'm new to Python and the other programs and I'm attempting to plot 21 members of an ensemble forecast using for loops over the ensemble members, each one having a dedicated number 1-21. When I plot the map, the members all come up blue. I've done some research on colormaps and found that the default maps are not indexed, i.e. I can't call colormap[1] as if it were a list or array. Are there any indexed colormaps out there, or is there any way around this? Simply put, I want to cycle through colors, a different one for each forecast member, using simple numerics.
推荐答案
您可以使用 ax.set_prop_cycle 指定要循环的颜色列表.
You could use ax.set_prop_cycle to specify a list of colors to cycle through.
颜色图是可调用的,因此生成颜色的一种简单方法是将 0 到 1 之间的浮点序列传递给颜色图,然后颜色图将返回一个 RGBA 颜色数组.
Colormaps are callable, so an easy way to generate colors is to pass an sequence of floats between 0 and 1 to a colormap, which will then return an array of RGBA colors.
例如,
In [93]: jet = plt.cm.jet
In [94]: jet([0,0.5,1])
Out[94]:
array([[ 0. , 0. , 0.5 , 1. ],
[ 0.49019608, 1. , 0.47754586, 1. ],
[ 0.5 , 0. , 0. , 1. ]])
<小时>
import matplotlib.pylab as plt
import matplotlib.rcsetup as rcsetup
import numpy as np
jet = plt.cm.jet
fig, ax = plt.subplots()
N = 20
idx = np.linspace(0, 1, N)
ax.set_prop_cycle(rcsetup.cycler('color', jet(idx)))
x = np.linspace(0, 100, 200)
for i in range(1, N+1):
ax.plot(x, np.sin(x)+i)
ax.set_ylim(0, N+1)
plt.show()
这篇关于Python Matplotlib/底图颜色循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!