Prettier default plot colors in matplotlib
Question:
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?
Answers:
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)
You can use the colormap functionality of matplotlib.
A good example is in this question. You can show your colormap options using this script.
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.
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.
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()
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
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()
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')
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
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')
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)
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?
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)
You can use the colormap functionality of matplotlib.
A good example is in this question. You can show your colormap options using this script.
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.
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.
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()
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
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()
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')
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
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')
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)