如何使用matplotlib为所有子图设置默认的颜色循环?

时间:2021-01-05 23:40:09

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

如何为使用matplotlib的情节设置默认的颜色集?我可以像这样设置一个特定的颜色地图

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?

但是有没有什么方法可以为所有的情节设置相同的颜色,包括子情节?

2 个解决方案

#1


66  

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

当然!指定轴。在您的.matplotlibrc文件中使用color_cycle,或者使用matplotlib在运行时设置它。rcParams或matplotlib.rc。

As an example of the latter:

作为后者的一个例子:

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

# Set the default color cycle
mpl.rcParams['axes.color_cycle'] = ['r', 'k', 'c']

# Alternately, we could use rc:
# mpl.rc('axes', color_cycle=['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为所有子图设置默认的颜色循环?

#2


26  

Starting from matplotlib 1.5, mpl.rcParams['axes.color_cycle'] is deprecated. You should use axes.prop_cycle:

从matplotlib 1.5开始,mpl.rcParams['axes]。color_cycle ']是弃用。您应该使用axes.prop_cycle:

import matplotlib as mpl
mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=["r", "#e94cdc", "0.7"]) 

#1


66  

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

当然!指定轴。在您的.matplotlibrc文件中使用color_cycle,或者使用matplotlib在运行时设置它。rcParams或matplotlib.rc。

As an example of the latter:

作为后者的一个例子:

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

# Set the default color cycle
mpl.rcParams['axes.color_cycle'] = ['r', 'k', 'c']

# Alternately, we could use rc:
# mpl.rc('axes', color_cycle=['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为所有子图设置默认的颜色循环?

#2


26  

Starting from matplotlib 1.5, mpl.rcParams['axes.color_cycle'] is deprecated. You should use axes.prop_cycle:

从matplotlib 1.5开始,mpl.rcParams['axes]。color_cycle ']是弃用。您应该使用axes.prop_cycle:

import matplotlib as mpl
mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=["r", "#e94cdc", "0.7"])