Format plots using matplotlib styles

Introduction

You can customize your plots using matplotlib in a few ways: in code (verbose), a matplotlibrc file (better), or a matplotlib style sheet (best)! The matplotlibrc and mplstyle have the exact same syntax but the difference is how they are used. Matplotlibrc is read at runtime and active for any of the plotting commands. Using the mplstyle requires the use of the command

>>> style.use("mystylename") 

This must be executed before any plots commands. Matplotlib styles allow you to change the format of a plot is easily without adjusting any inline code—just call the style.use command—useful when publishing to various journals.

Install

You can define your own matplotlib style sheet as <filename>.mplstyle and store it in the <mpl_configdir>/stylelib/ directory. You can download the physrev.mplstyle I have defined here.

You need to create the stylelib directory manually.

Below is an example for *BSD/Linux, assuming you downloaded the physrev.mplstyle file to /home/username/downloads. Other

$ python -c "import matplotlib; print(matplotlib.get_configdir())"
/home/username/.matplotlib
$ mkdir /home/username/.matplotlib/stylelib
$ cp /home/username/downloads/physrev.mplstyle /home/username/.matplotlib/stylelib/

The above assumes your using Python >= 3.0. If you are using Python 2.7 replace

print(matplotlib.get_configdir()) 

with

print matplotlib.get_configdir()

© 2017–2019 David Kalliecharan