Book Image

Mastering Matplotlib 2.x

By : Benjamin Walter Keller
Book Image

Mastering Matplotlib 2.x

By: Benjamin Walter Keller

Overview of this book

In this book, you’ll get hands-on with customizing your data plots with the help of Matplotlib. You’ll start with customizing plots, making a handful of special-purpose plots, and building 3D plots. You’ll explore non-trivial layouts, Pylab customization, and more about tile configuration. You’ll be able to add text, put lines in plots, and also handle polygons, shapes, and annotations. Non-Cartesian and vector plots are exciting to construct, and you’ll explore them further in this book. You’ll delve into niche plots and visualize ordinal and tabular data. In this book, you’ll be exploring 3D plotting, one of the best features when it comes to 3D data visualization, along with Jupyter Notebook, widgets, and creating movies for enhanced data representation. Geospatial plotting will also be explored. Finally, you’ll learn how to create interactive plots with the help of Jupyter. Learn expert techniques for effective data visualization using Matplotlib 3 and Python with our latest offering -- Matplotlib 3.0 Cookbook
Table of Contents (7 chapters)

The Matplotlib configuration files

In this section, we will look at the matplotlib configuration file, matplotlibrc, which contains all of the default configuration settings for how Matplotlib works on your system.

Matplotlibrc – where does it live?

matplotlibrc is multiple different configuration files; it's a hierarchy of configuration files that allows you to configure Matplotlib to behave differently for different users or different projects depending on what you need. By default, the hierarchy of which matplotlibrc config file overrides others begins with the one in the current directory, which oftentimes does not exist but ultimately will be the one that overrides all others.

The next one is within the matplotlibrc environment variable in that directory.

The third is within the config home, if you're running a Linux or BSD-based operating system with a directory called Matplotlib and a file called matplotlibrc.

Finally, with the Matplotlib installation pending on what system you're on, that installation living in some install directory will have a directory called Matplotlib data. A Matplotlib RC file that comes with Matplotlib installation is actually a fairly rich template that includes a wide variety of comments and example code which we will be discussing.

We will take a copy of the example file and put it in the .config/matplotlib file.

After opening the configuration file, we can see that it is quite big—in fact, it's 510 lines by default with the current version of Matplotlib, as shown here:

It also has a number of different options for configuring things such as fonts and appearance, as shown here:

There are also options such as the Latex behavior:

There are also options for the behavior of different backends, and we can configure the options that do not necessarily get covered by style sheets that would just be things like appearance, as follows:

The following screenshot shows the SVG writer:

Also, taking an example on what the default backend is, by default, we will set the backend to be TkAgg. We can, of course, change this depending on the system:

For example, we can change the color of text, as shown here:

# text.color : black

After uncommenting text.color (since this ultimately is the highest matplotlibrc in that hierarchy, overwriting versions of the .config file), there isn't a custom version in the directory with the Jupyter Notebooks. We will restart the kernel like we did earlier in this chapter:

Reimporting NumPy and Matplotlib, we can see here that after adding some text, hello, and inserting 0.5, we can see the colored text automatically without having to specify any other configuration options that you apply would get automatically applied without having to go in and change things by hand:

It's recommended to see all of the different options that Matplotlib provides. Go and take a look at that default template file:

Also, take a look at this documentation here: http://matplotlib.org/users/customizing.html#the-matplotlibrc-file. There is a little bit of extra text about the Matplotlib RC file. The template itself contains enough documentation. Understand this config file and look at all of the various options that are available to you.

So, if you want to configure your Matplotlib to behave in a more sensible way for your projects, it is recommended that you make a copy of this and start hacking away at it to tweak Matplotlib to do exactly what it is that you want it to.