Matplotlib 将颜色图 tab20 更改为三种颜色 [英] Matplotlib change colormap tab20 to have three colors

查看:167
本文介绍了Matplotlib 将颜色图 tab20 更改为三种颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Matplotlib具有一些新的且非常方便的颜色图(

Matplotlib 中的颜色图:

Matplotlib has some new and very handy colormaps (tab colormap). What I miss is a feature to generate a colormap like tab20b or tab20c but with three hue levels instead of four ?

This solution is a bit complicated, is there an easier one ?

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)

The colormap in Matplotlib:

EDIT: A more generic approach has been provided in this SO post.

解决方案

If by "easier" you mean shorter, you can directly evaluate the colormap on a numpy array.

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()

这篇关于Matplotlib 将颜色图 tab20 更改为三种颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆