Python 将matplotlib颜色贴图用于颜色循环
如果我创建颜色,例如:Python 将matplotlib颜色贴图用于颜色循环,python,matplotlib,Python,Matplotlib,如果我创建颜色,例如: import numpy as np from matplotlib import pyplot as plt n = 6 color = plt.cm.coolwarm(np.linspace(0.1,0.9,n)) color 颜色是一个numpy数组: array([[ 0.34832334, 0.46571115, 0.88834616, 1. ], [ 0.56518158, 0.69943844, 0.99663507
import numpy as np
from matplotlib import pyplot as plt
n = 6
color = plt.cm.coolwarm(np.linspace(0.1,0.9,n))
color
颜色是一个numpy数组:
array([[ 0.34832334, 0.46571115, 0.88834616, 1. ],
[ 0.56518158, 0.69943844, 0.99663507, 1. ],
[ 0.77737753, 0.84092121, 0.9461493 , 1. ],
[ 0.93577377, 0.8122367 , 0.74715647, 1. ],
[ 0.96049006, 0.61627642, 0.4954666 , 1. ],
[ 0.83936494, 0.32185622, 0.26492398, 1. ]])
但是,如果我在.mplstyle
文件(map(tuple,color[:,0:-1])
中将RGB值(不带alpha值1)作为元组插入,我会得到与此类似的错误:
in file "/home/moritz/.config/matplotlib/stylelib/ggplot.mplstyle"
Key axes.color_cycle: [(0.34832334141176474 does not look like a color arg
(val, error_details, msg))
想知道为什么吗?编辑2021年4月:从matplotlib 2.2.0开始,键
轴。颜色循环已被弃用()。
新方法是使用设置prop\u循环()
实际上,细节在matplotlibrc本身中:它需要一个字符串代表(十六进制、字母或单词,而不是元组)
对于Matplotlib 2.2,使用循环器
模块将实现此功能,而无需转换为十六进制值
import cycler
n = 100
color = pyplot.cm.viridis(np.linspace(0, 1,n))
mpl.rcParams['axes.prop_cycle'] = cycler.cycler('color', color)
“连续”颜色映射
如果您想从“连续”颜色贴图(如默认的viridis贴图)中循环使用N
颜色,效果会很好
import matplotlib.pyplot as plt
N = 6
plt.rcParams["axes.prop_cycle"] = plt.cycler("color", plt.cm.viridis(np.linspace(0,1,N)))
fig, ax = plt.subplots()
for i in range(N):
ax.plot([0,1], [i, 2*i])
plt.show()
“离散”颜色映射
Matplotlib提供了一些“离散”颜色映射,因为它们为定性视觉效果保留了少量不同的颜色,如tab10
colormap。要在这样的颜色映射中循环,解决方案可能是不使用N
,而只是将映射的所有颜色移植到循环器
import matplotlib.pyplot as plt
plt.rcParams["axes.prop_cycle"] = plt.cycler("color", plt.cm.tab20c.colors)
fig, ax = plt.subplots()
for i in range(15):
ax.plot([0,1], [i, 2*i])
plt.show()
请注意,只有ListedColormaps
具有.colors
属性,因此这仅适用于那些colormap,但不适用于jet
贴图
组合溶液
以下是一个通用函数,它将颜色映射作为输入并输出相应的循环器。我最初是在年提出这个解决方案的
“连续”情况的用法:
“离散”情况的用法
颜色参数应该以(
而不是[(
,是吗?仍然不起作用。我尝试了((…)、(…);(…)、(…);(…)(…)(…)(…)轴“axes.color\u cycle”
在matplotlib 1.5
中被弃用。我正在尝试发现如何使用循环获得相同的行为=plt.cycler(“color”,plt.cm.tab20c.colors)
是我要找的产品线!这太棒了!感谢您提供的组合解决方案!
import matplotlib.pyplot as plt
plt.rcParams["axes.prop_cycle"] = plt.cycler("color", plt.cm.tab20c.colors)
fig, ax = plt.subplots()
for i in range(15):
ax.plot([0,1], [i, 2*i])
plt.show()
from matplotlib.pyplot import cycler
import numpy as np
from matplotlib.colors import LinearSegmentedColormap, ListedColormap
import matplotlib.cm
def get_cycle(cmap, N=None, use_index="auto"):
if isinstance(cmap, str):
if use_index == "auto":
if cmap in ['Pastel1', 'Pastel2', 'Paired', 'Accent',
'Dark2', 'Set1', 'Set2', 'Set3',
'tab10', 'tab20', 'tab20b', 'tab20c']:
use_index=True
else:
use_index=False
cmap = matplotlib.cm.get_cmap(cmap)
if not N:
N = cmap.N
if use_index=="auto":
if cmap.N > 100:
use_index=False
elif isinstance(cmap, LinearSegmentedColormap):
use_index=False
elif isinstance(cmap, ListedColormap):
use_index=True
if use_index:
ind = np.arange(int(N)) % cmap.N
return cycler("color",cmap(ind))
else:
colors = cmap(np.linspace(0,1,N))
return cycler("color",colors)
import matplotlib.pyplot as plt
N = 6
plt.rcParams["axes.prop_cycle"] = get_cycle("viridis", N)
fig, ax = plt.subplots()
for i in range(N):
ax.plot([0,1], [i, 2*i])
plt.show()
import matplotlib.pyplot as plt
plt.rcParams["axes.prop_cycle"] = get_cycle("tab20c")
fig, ax = plt.subplots()
for i in range(15):
ax.plot([0,1], [i, 2*i])
plt.show()