Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the default color cycle for all subplots with 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?

like image 924
Katt Avatar asked Feb 22 '12 15:02

Katt


People also ask

What is the default color cycle matplotlib?

The default interactive figure background color has changed from grey to white, which matches the default background color used when saving. in your matplotlibrc file.

How do I set the default style in matplotlib?

plt. style. use('default') worked for me. As I understand this, it tells matplotlib to switch back to its default style mode.


2 Answers

Sure! Either specify axes.color_cycle in your .matplotlibrc file or set it at runtime using matplotlib.rcParams or 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.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() 

enter image description here

like image 149
Joe Kington Avatar answered Sep 19 '22 08:09

Joe Kington


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

import matplotlib as mpl mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=["r", "#e94cdc", "0.7"])  
like image 20
volodymyr Avatar answered Sep 19 '22 08:09

volodymyr