SMeznaric
SMeznaric

Reputation: 492

How to permanently set matplotlib pyplot style?

I like the ggplot style that is available in matplotlib. So when I'm in an interactive session I typically do

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

This produces very nice styles. Is there an easy way to make this setting persistent so I don't need to type the above command every time I start up Python?

Upvotes: 4

Views: 3668

Answers (4)

E. Turok
E. Turok

Reputation: 116

I hope to expand upon the answer of @ImportanceOfBeingErnest in a bit more detail.

How to Permanently Change Default Matplotlib Style

This is a three step process where we 1. find out where the matplotlibrc file is located 2. copy that file to another location and 3. add the matplotlibrc parameters of a specific style. Let's go over each in a bit more detail.

  1. Find where the default matplotlibrc file is in your computer with the command
import matplotlib
print(matplotlib.matplotlib_fname())
  1. Copy the default matplotlibrc file into the directory $HOME/.config/matplotlib/matplotlibrc with the command
cp path_matplotlibrc $HOME/.config/matplotlib/matplotlibrc`

where path_matplotlibrc is the path where matplotlibrc is located, ie path_matplotlibrc is the result of step one.

  1. Copy the code from one of the styles files and paste it in the '$HOME/.config/matplotlib/matplotlibrc` file.

Go to the directory matplotlib/lib/matplotlib/mpl-data/stylelib (or just go to the same spot in matplotlib's github here). Copy the code from one of the .mplstyle files in that directory. This code will be a list of matplotlibrc parameters that correspond to a specific style indicated by the file name. For example, going to github, you can just copy the code for the fivethirtyeight style which looks like this.

enter image description here

Paste the code with that style's matplotlibrc parameters at the bottom of the '$HOME/.config/matplotlib/matplotlibrcfile. So in this example we would paste the thefivethirtyeightstylematplotlibrc` parameter code depicted above at the end of the new file.

And that's it you are done. Just find out where the matplotlibrc file is originally located, copy that file to another location, and add the matplotlibrc parameters of a specific style. Pretty simple.

Why does this work?

The matplotlib documentation here explains:

Matplotlib uses matplotlibrc configuration files to customize all kinds of properties, which we call 'rc settings' or 'rc parameters'. You can control the defaults of almost every property in Matplotlib: figure size and DPI, line width, color and style, axes, axis and grid properties, text and font properties and so on. The matplotlibrc is read at startup to configure Matplotlib... When a style sheet is given with style.use('/.mplstyle'), settings specified in the style sheet take precedence over settings in the matplotlibrc file.

Each style is really a collection of parameters and properties that override the default matplotlibrc file found here. We can see this explicitly by looking at the directory matplotlib/lib/matplotlib/mpl-data/stylelib (found on github here) which contains a file for each built-in style. Click on a file in the directory and we can clearly see that the style is merely a list of matplotlibrc parameters. (This is what we showed above, the fivethirtyeight matplotlibrc parameters.)

So Matplotlib uses the matplotlibrc file to determine the style of the graphs. Now how does pasting the matplotlibrc settings of a certain style in a new file location override the default matplotlibrc settings?

Matplotlib looks for the matplotlibrc file in four locations in the following order:

  1. matplotlibrc in the current working directory. It is just seeing if a file exists in the current working directory called matplotlibrc.
  2. $MATPLOTLIBRC if it is a file, else $MATPLOTLIBRC/matplotlibrc.
  3. On Linux and FreeBSD, it looks in .config/matplotlib/matplotlibrc (or $XDG_CONFIG_HOME/matplotlib/matplotlibrc) if you've customized your environment. On other platforms, it looks in .matplotlib/matplotlibrc.
  4. INSTALL/matplotlib/mpl-data/matplotlibrc, where INSTALL is where you installed the matplotlib package. It may be something like /usr/lib/python3.7/site-packages on Linux, and maybe C:\Python37\Lib\site-packages on Windows.

Once a matplotlibrc file has been found, it will not search any of the other paths.

So by default matplotlibrcis only found in location four. This effectively means we can override the default settings found in location 4 by creating a new matplotlibrc in locations one, two, or three. By convention you should create a new matplotlibrc file in the highest numbered location possible so you can have more room to override that file if need be. In the instructions I just gave, we create a new matplotlibrc file in location 3. This is why and how we overrode the default matplotlibrc settings, thus creating new matplotlibrc settings that correspond to a specific style that is now used by default.


A bit more about Matplotlib Styles can be found here.

About Matplotlib Styles

matplotlib has several built-in styles to choose from. You can see here how each built-in style will change how your plots looks. To call a specific style use the command plt.style.use('stylename') where stylename is any arbitrary style name and to list all available styles, use print(plt.style.available).

Upvotes: 0

Roelant
Roelant

Reputation: 5119

You can specify your required style in a matplotlibrc format file in the installation directory.

Edit: on github we find

# from http://www.huyng.com/posts/sane-color-scheme-for-matplotlib/

patch.linewidth: 0.5
patch.facecolor: 348ABD  # blue
patch.edgecolor: EEEEEE
patch.antialiased: True

font.size: 10.0

axes.facecolor: E5E5E5
axes.edgecolor: white
axes.linewidth: 1
axes.grid: True
axes.titlesize: x-large
axes.labelsize: large
axes.labelcolor: 555555
axes.axisbelow: True       # grid/ticks are below elements (e.g., lines, text)

axes.prop_cycle: cycler('color', ['E24A33', '348ABD', '988ED5', '777777',     'FBC15E', '8EBA42', 'FFB5B8'])
               # E24A33 : red
               # 348ABD : blue
               # 988ED5 : purple
               # 777777 : gray
               # FBC15E : yellow
               # 8EBA42 : green
               # FFB5B8 : pink

xtick.color: 555555
xtick.direction: out

ytick.color: 555555
ytick.direction: out

grid.color: white
grid.linestyle: -    # solid line

figure.facecolor: white
figure.edgecolor: 0.50

Upvotes: 3

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339715

In principle, @Roelants answer is correct. Just to go a bit more in detail here:

  1. Locate your matplotlibrc file as discussed here and make a backup of it.
  2. Locate the ggplot.mplstyle (might be in a subfolder compared to the matplotlibrc file).
  3. Replace the content of matplotlibrc file by the content of ggplot.mplstyle.

From that moment on, the default style will be identical to the one defined by the ggplot style.

Upvotes: 0

mikuszefski
mikuszefski

Reputation: 4043

You can append

use('ggplot')

to .../lib/python2.7/site-packages/matplotlib-2.0.0-py2.7-linux-x86_64.egg/matplotlib/style/__init__.py

Your specific path might look slightly different.

Upvotes: 1

Related Questions