Prettier default plot colors in matplotlib

PythonMatplotlib

Python Problem Overview


The default colors used in matplotlib (example here: http://matplotlib.org/examples/pylab_examples/pie_demo.html) are kind of plain and ugly. I've also noticed that if you plot more than 5-6 different series in a single plot, matplotlib starts repeating colors.

I've seen some gorgeous graphs coming out of other visualization packages (in other languages, by default) that can have 5-6 different series covered by just one color in different shades. Does anyone have a good color set to use in matplotlib? And a way to make matplotlib use it by default?

Python Solutions


Solution 1 - Python

You can use Matplotlib's style sheets. It has been ported from the mpltools library which has a style module that redefine matplotlib rc parameters.

As an example, see the use of the ggplot style and Matplotlib's manual.

image

Solution 2 - Python

The question was asked 2 years ago, and today it's much easier to get better style for your plot. You don't even need external packages for that. As @asmaier mentioned in his comment, mpltools.style functionality has been integrated into Matplotlib 1.4, so you can switch styles with:

plt.style.use(style_name)

For example:

import matplotlib.pyplot as plt
import numpy as np

plt.style.use('ggplot')

num_lines = 6

ax = plt.subplot(111)

for i in range(num_lines):
    x = np.linspace(0,20,200)
    ax.plot(x,np.sin(x)+i)

plt.show()

enter image description here

You can list all available styles with:

print plt.style.available

In Matplotlib 1.5 several new styles have been added, including many styles from the Seaborn project:

plt.style.use('seaborn-dark-palette')

enter image description here

Solution 3 - Python

Have a look at prettyplotlib a library — just pointed out to me recently by friends — that modifies matplotlib to be better aligned with the ideas of Edward Tufte, as well as some very carefully studied work by Cynthia Brewer on color perception.

Solution 4 - Python

The Seaborn package (based on Matplotlib) has nice default graph styles, and I've found that it's a nice way to create an appealing color-cycle.

They have a nice discussion of the colorpalettes here: https://stanford.edu/~mwaskom/software/seaborn/tutorial/color_palettes.html

The following code demonstrates how you can pick a new color_cycle automatically for a simple line plot:

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns   

num_lines = 6

ax = plt.subplot(111)
ax.set_color_cycle(sns.color_palette("coolwarm_r",num_lines))

for i in range(num_lines):
    x = np.linspace(0,20,200)
    ax.plot(x,np.sin(x)+i)

plt.show()

enter image description here

If you want to just change the linecolors and not use the other seaborn pre-sets such as the gray background, just import seaborn with

import seaborn.apionly as sns

Solution 5 - Python

You can setup a .matplotlibrc file. A really heavily commented example is here. It looks to me like the option you want to change is axes.color_cycle. I don't have any advice on what to make it for a prettier interface -- That's a little too subjective Stack Overflow ;-) (and I'm happy with the defaults)

Solution 6 - Python

You can use the colormap functionality of matplotlib.

A good example is in this question. You can show your colormap options using this script.

Solution 7 - Python

For a richer set of colours that can be used with matplotlib, check out palettable, featuring the wonderful Wes Anderson palletes.

$ pip install palettable
$ python
>>> from palettable.colorbrewer.qualitative import Dark2_7

Solution 8 - Python

I use ggplot style for my works you can see list of other styles in this link matplotlib.org

USAGE:

import matplotlib.pyplot as plt
plt.style.use('ggplot')

Solution 9 - Python

A while ago, I created dufte in matplotx, a minimalistic style for matplotlib. Install with

pip install matplotx

and use with

plt.style.use(matplotx.styles.dufte)

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRandy OlsonView Question on Stackoverflow
Solution 1 - PythongcalmettesView Answer on Stackoverflow
Solution 2 - PythonwombatonfireView Answer on Stackoverflow
Solution 3 - PythonMarcus P SView Answer on Stackoverflow
Solution 4 - PythonDanHicksteinView Answer on Stackoverflow
Solution 5 - PythonmgilsonView Answer on Stackoverflow
Solution 6 - PythonStuGreyView Answer on Stackoverflow
Solution 7 - PythonchristopherlovellView Answer on Stackoverflow
Solution 8 - PythonRadeshView Answer on Stackoverflow
Solution 9 - PythonNico SchlömerView Answer on Stackoverflow