如何设置Matplotlib默认的轴颜色循环

6
Matplotlib 2.0 中默认的轴颜色循环被称为 tab10

enter image description here

我想使用不同的定性色彩循环,比如tab20c

enter image description here

我已经使用了这段代码来更改默认的轴颜色循环:

import matplotlib.pyplot as plt
from cycler import cycler

c = plt.get_cmap('tab20c').colors
plt.rcParams['axes.prop_cycle'] = cycler(color=c)

这看起来对我来说相当混乱。有更好的方法吗?


这个答案展示了一种稍微不同的方式;虽然可能并不更好。但是,除非你明确定义“更好”,否则很难知道你真正想要什么。 - ImportanceOfBeingErnest
我希望有类似于 ax.set_color_cycle('tab10') 这样的东西。 - onewhaleid
`File "\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\rcsetup.py", line 395, in validate_color if s.find(',') >= 0:AttributeError: 'dict' object has no attribute 'find'` - onewhaleid
抱歉,我没有测试过,但应该是 ax.set_prop_cycle(cycler(color=plt.get_cmap('tab20c').color‌​s)) - ImportanceOfBeingErnest
那个很好用。你能把它加到答案里吗? - onewhaleid
ax.set_prop_cycle(color=plt.get_cmap('tab20c').colors) 也可以使用。根据文档,“这只是一个显式创建循环器的快捷方式”。 - toliveira
1个回答

5

评论中提到的“更好”意义不明确。所以我可以想到两种不同的方法,除了问题中已经非常好的一种方法。

(a) 使用seaborn

只是为了展示一种不同的设置颜色循环的方式:Seaborn有一个函数set_palette,它本质上是设置matplotlib的颜色循环。您可以像这样使用它:

import matplotlib.pyplot as plt
import seaborn as sns
sns.set_palette("tab20c",plt.cm.tab20c.N )

(b)仅为轴设置循环器

如果您需要为每个轴单独设置循环器,可以使用 ax.set_prop_cycle 为轴ax设置。

import matplotlib.pyplot as plt
from cycler import cycler

fig, ax = plt.subplots()
ax.set_prop_cycle(cycler(color=plt.get_cmap('tab20c').color‌‌​​s))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接