How To Permanently Set Matplotlib Pyplot Style?
Solution 1:
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.5patch.facecolor:348ABD# bluepatch.edgecolor:EEEEEEpatch.antialiased:Truefont.size:10.0axes.facecolor:E5E5E5axes.edgecolor:whiteaxes.linewidth:1axes.grid:Trueaxes.titlesize:x-largeaxes.labelsize:largeaxes.labelcolor:555555axes.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 : pinkxtick.color:555555xtick.direction:outytick.color:555555ytick.direction:outgrid.color:whitegrid.linestyle:-# solid linefigure.facecolor:whitefigure.edgecolor:0.50
Solution 2:
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.
Solution 3:
In principle, @Roelants answer is correct. Just to go a bit more in detail here:
- Locate your
matplotlibrc
file as discussed here and make a backup of it. - Locate the
ggplot.mplstyle
(might be in a subfolder compared to thematplotlibrc
file). - Replace the content of
matplotlibrc
file by the content ofggplot.mplstyle
.
From that moment on, the default style will be identical to the one defined by the ggplot style.
Solution 4:
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.
- Find where the default
matplotlibrc
file is in your computer with the command
import matplotlib
print(matplotlib.matplotlib_fname())
- 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.
- 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.
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 the
fivethirtyeightstyle
matplotlibrc` 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:
matplotlibrc
in the current working directory. It is just seeing if a file exists in the current working directory calledmatplotlibrc
.$MATPLOTLIBRC
if it is a file, else$MATPLOTLIBRC/matplotlibrc
.- 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
. INSTALL/matplotlib/mpl-data/matplotlibrc
, whereINSTALL
is where you installed thematplotlib
package. It may be something like/usr/lib/python3.7/site-packages
on Linux, and maybeC:\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 matplotlibrc
is 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)
.
Post a Comment for "How To Permanently Set Matplotlib Pyplot Style?"