gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:32:55 26 4
gpt4 key购买 nike

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)

这对我来说看起来很脏。有没有更好的办法?

最佳答案

正如评论中所说,目前尚不清楚“更好”是什么意思。因此,除了问题中的一种方法之外,我还可以想到两种不同的方法,这种方法非常有效。

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

关于matplotlib - 如何设置默认的 Matplotlib 轴颜色循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46148193/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com