6

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?

6
  • This answer shows a slightly different way; it's probably not better though. But unless you clearly define "better", it hard to know what you want really. Commented Sep 11, 2017 at 7:40
  • I was hoping for something like ax.set_color_cycle('tab10') Commented Sep 12, 2017 at 0:31
  • File "\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\rcsetup.py", line 395, in validate_color if s.find(',') >= 0: AttributeError: 'dict' object has no attribute 'find' Commented Sep 12, 2017 at 22:50
  • 1
    Sorry, I didn't test it, but it should be ax.set_prop_cycle(cycler(color=plt.get_cmap('tab20c').color‌​s)) Commented Sep 13, 2017 at 7:48
  • That works nicely. Could you please add it as an answer? Commented Sep 13, 2017 at 12:39

1 Answer 1

6

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.