如何使用 matplotlib 为所有子图设置默认颜色循环? [英] How to set the default color cycle for all subplots with matplotlib?

查看:52
本文介绍了如何使用 matplotlib 为所有子图设置默认颜色循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为使用 matplotlib 绘制的绘图设置一组默认颜色?我可以像这样设置特定的颜色图

How can I set a default set of colors for plots made with matplotlib? I can set a particular color map like this

import numpy as np
import matplotlib.pyplot as plt

fig=plt.figure(i)
ax=plt.gca()
colormap = plt.get_cmap('jet')
ax.set_color_cycle([colormap(k) for k in np.linspace(0, 1, 10)])

但是有没有办法为所有绘图(包括子绘图)设置相同的颜色集?

but is there some way to set the same set of colors for all plots, including subplots?

推荐答案

好的!在您的 .matplotlibrc 中指定 axes.color_cycle文件 或在运行时使用 matplotlib.rcParamsmatplotlib.rc 设置它.

Sure! Either specify axes.color_cycle in your .matplotlibrc file or set it at runtime using matplotlib.rcParams or matplotlib.rc.

以后者为例:

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

# Set the default color cycle
mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=["r", "k", "c"]) 

x = np.linspace(0, 20, 100)

fig, axes = plt.subplots(nrows=2)

for i in range(10):
    axes[0].plot(x, i * (x - 10)**2)

for i in range(10):
    axes[1].plot(x, i * np.cos(x))

plt.show()

这篇关于如何使用 matplotlib 为所有子图设置默认颜色循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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