Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mastering Matplotlib 2.x

You're reading from  Mastering Matplotlib 2.x

Product type Book
Published in Nov 2018
Publisher Packt
ISBN-13 9781789617696
Pages 214 pages
Edition 1st Edition
Languages
Author (1):
Benjamin Walter Keller Benjamin Walter Keller
Profile icon Benjamin Walter Keller

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.

You have been reading a chapter from
Mastering Matplotlib 2.x
Published in: Nov 2018 Publisher: Packt ISBN-13: 9781789617696
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}