Katt - 2 years ago 316

Python Question

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

`fig=figure(i)`

ax=gca()

ax.set_color_cycle([colormap(k) for k in linspace(0, 1, 10)])

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

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

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

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**