Matplotlib将colormap tab20更改为三种颜色

2024-04-25 19:59:48 发布

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

Matplotlib有一些新的非常方便的彩色地图(tab colormap)。我错过的是一个功能,生成一个彩色地图像tab20b或tab20c,但有三个色调级别,而不是四个?

这个解决方案有点复杂,有更简单的吗?

skip = []
for i in range(0,len(cm.colors)//4+1):
    skip.append(4*i)
# the colormap is called Vega in my Matplotlib version
cm = plt.cm.get_cmap('Vega20c')
cm_skip = [cm.colors[i] for i in range(len(cm.colors)) if i not in skip]

for i, c in enumerate(cm_skip):
    x = np.linspace(0,1)
    y = (i+1)*x + i
    plt.plot(x, y, color=c, linewidth=4)

enter image description here

Matplotlib中的颜色映射: enter image description here

编辑:此SO post中提供了更通用的方法。


Tags: in图像功能forlenmatplotlib地图cm
1条回答
网友
1楼 · 发布于 2024-04-25 19:59:48

如果“更简单”是指更短,则可以直接计算numpy数组上的颜色映射。

import matplotlib.pyplot as plt
import numpy as np

colors =  plt.cm.Vega20c( (4./3*np.arange(20*3/4)).astype(int) )
plt.scatter(np.arange(15),np.ones(15), c=colors, s=180)

plt.show()

enter image description here

相关问题 更多 >