onewhaleid
onewhaleid

Reputation: 355

How to set default Matplotlib axis colour cycle

The default axis colour cycle in Matplotlib 2.0 is called tab10:

enter image description here

I want to use a different qualitative colour cycle, such as tab20c:

enter image description here

I have used this code to change the default axis colour cycle:

import matplotlib.pyplot as plt
from cycler import cycler

c = plt.get_cmap('tab20c').colors
plt.rcParams['axes.prop_cycle'] = cycler(color=c)

This looks pretty dirty to me. Is there a better way?

Upvotes: 6

Views: 3693

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339725

As said in the comments, it's not clear what "better" would mean. So I can think of two different ways in addition to the one from the question, which works perfectly fine.

(a) use seaborn

Just to show a different way of setting the color cycler: Seaborn has a function set_palette, which does essentially set the matplotlib color cycle. You could use it like

import matplotlib.pyplot as plt
import seaborn as sns
sns.set_palette("tab20c",plt.cm.tab20c.N )

(b) set the cycler for the axes only

If you want the cycler for each axes individually, you may use ax.set_prop_cycle for an axes 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))

Upvotes: 5

Related Questions