python - Matplotlib 将颜色图 tab20 更改为具有三种颜色

标签 python matplotlib

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 中提供了一种更通用的方法.

最佳答案

如果您所说的“更简单”是指更短,您可以直接在 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

关于python - Matplotlib 将颜色图 tab20 更改为具有三种颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43938425/

相关文章:

python - 带 x 值标签的 seaborn 条形图(无色调)

python - 具有两行的单个图例项

python - Pandas - 提取以特定字符开头的字符串

python - Pandas Excel 到 DF 抛出意外的关键字参数 'index' 错误

python - 单击第一个链接 Mechanize python

python - 通过 3D x、y、z 散点图数据拟合一条线

python - 如何在 Python 中将 3D 数据绘制为 2D 网格颜色图?

python - 如何通过 Selenium 和 Python 将文本发送到搜索字段

c++ - 可从 Python 调用的编译 C++ CGAL 模块

matplotlib - python中的词云具有三个单独的值