Customizing Matplotlib's Plotting Styles
Matplotlib is an amazingly powerful library to create graphs with Python. The default Matplotlib style is arguably not very beautiful, but there are several ways to customize the look of plots.
You can use libraries built on top of Matplotlib like prettyplotlib (no longer maintained) or Seaborn. The latter not only produces more appealing plots out of the box, but also provides a higher-level API, that has a more gentle learning curve.
Matplotlib's lower-level API obviously has advantages regarding your flexibility in specifying many bits and pieces of plots and it allows you to change the style as well. Probably the easiest way is to use one of the predefined styles Matplotlib ships with, for example:
import matplotlib.pyplot as plt
plt.style.use('ggplot')
The result is that your plots will look similar to those created with the ggplot library for the R programming language. Version 1.4.3 of Matplotlib provides the following 5 styles: fivethirtyeight
, bmh
, grayscale
, dark_background
, ggplot
. To see, the styles your version supports, execute:
print(plt.style.available)
Another thing you can do is to override style definitions within your program by changing values of the rcParams
dictionary:
import matplotlib as mpl
mpl.rcParams['font.family'] = 'Ubuntu'
Overriding rcParams
values within a script makes sense for styles you only want to apply to a particular plot, but you don't want to set the same definitions in every program or IPython notebook which uses Matplotlib.
Fortunately, there is a better way. You can create your own style sheet in your Matplotlib configuration directory and set that style as the one to use in your script.
To determine the location of the configuration directory on your system, execute:
print(mpl.get_configdir())
In that directory create a subdirectory called stylelib
and add your style sheet with the extension mplstyle
. If you named your style mystyle.mplstyle
you would set it as follows:
plt.style.use('mystyle')
To learn more about the properties you can set in your configuration file, see the Customizing matplotlib documentation page. The example matplotlibrc
file may be a bit overwhelming. For something more digestible, you can check out the predefined styles mentioned above and customize the one you like best.
Below you see a side by side comparison of a plot created with Matplotlib's default style and one with a customized version of the fivethirtyeight
style.
Thankfully, a lot of the hard work has already been done by people who contributed to this and other open source projects, so there really no need to create plots with Matplotlib that don't look nice.
References
Featured Merch
Latest Posts
- Troubleshooting External Hard Drives on Linux
- How to Prevent SSH Timeout on Linux Systems
- Getting Started with Print-on-Demand Merchandise
- Understanding the Bash Fork Bomb: A Breakdown of :(){ :|:& };:
- 50 Must-Know Bash Commands for Linux & Unix Sysadmins
Featured Book
Subscribe to RSS Feed
This post was written by Ramiro Gómez (@yaph) and published on . Subscribe to the Geeksta RSS feed to be informed about new posts.
Tags: code matplotlib python style sheet tutorial
Disclosure: External links on this website may contain affiliate IDs, which means that I earn a commission if you make a purchase using these links. This allows me to offer hopefully valuable content for free while keeping this website sustainable. For more information, please see the disclosure section on the about page.
Share post: Facebook LinkedIn Reddit Twitter