NumPy – 81 – personalizzare Matplotlib: configurazioni e stylesheets

Continuo da qui, copio qui.

Matplotlib’s default plot settings are often the subject of complaint among its users. While much is slated to change in the 2.0 Matplotlib release [uh! ormai…] in late 2016, the ability to customize default settings helps bring the package inline with your own aesthetic preferences.

Here we’ll walk through some of Matplotlib’s runtime configuration (rc) options, and take a look at the newer stylesheets feature, which contains some nice sets of default configurations.

Personalizzazioni al volo del grafico
Through this chapter, we’ve seen how it is possible to tweak individual plot settings to end up with something that looks a little bit nicer than the default. It’s possible to do these customizations for each individual plot. For example, here is a fairly drab default histogram:

We can adjust this by hand to make it a much more visually pleasing plot:

OK, ci sarebbe da aggiornarsi 😯

This looks better, and you may recognize the look as inspired by the look of the R language’s ggplot visualization package. But this took a whole lot of effort! We definitely do not want to have to do all that tweaking each time we create a plot. Fortunately, there is a way to adjust these defaults once in a way that will work for all plots.

Cambiare i defaults: rcParams
Each time Matplotlib loads, it defines a runtime configuration (rc) containing the default styles for every plot element you create. This configuration can be adjusted at any time using the plt.rc convenience routine. Let’s see what it looks like to modify the rc parameters so that our default plot will look similar to what we did before.

We’ll start by saving a copy of the current rcParams dictionary, so we can easily reset these changes in the current session:

Now we can use the plt.rc function to change some of these settings:

Let’s see what simple line plots look like with these rc parameters:

I find this much more aesthetically pleasing than the default styling. If you disagree with my aesthetic sense, the good news is that you can adjust the rc parameters to suit your own tastes! These settings can be saved in a .matplotlibrc file, which you can read about in the Matplotlib documentation. That said, I prefer to customize Matplotlib using its stylesheets instead.

The version 1.4 release of Matplotlib in August 2014 added a very convenient style module, which includes a number of new default stylesheets, as well as the ability to create and package your own styles. These stylesheets are formatted similarly to the .matplotlibrc files mentioned earlier, but must be named with a .mplstyle extension.

Even if you don’t create your own style, the stylesheets included by default are extremely useful. The available styles are listed in —here I’ll list only the first five for brevity:

The basic way to switch to a stylesheet is to call'stylename')

But keep in mind that this will change the style for the rest of the session! Alternatively, you can use the style context manager, which sets a style temporarily:


Let’s create a function that will make two basic types of plot:

We’ll use this to explore how these plots look using the various built-in styles.

stile di default
The default style is what we’ve been seeing so far throughout the book; we’ll start with that. First, let’s reset our runtime configuration to the notebook default:

stile FiveThiryEight
The fivethirtyeight style mimics the graphics found on the popular FiveThirtyEight website. As you can see here, it is typified by bold colors, thick lines, and transparent axes:

stile ggplot
The ggplot package in the R language is a very popular visualization tool. Matplotlib’s ggplot style mimics the default styles from that package:

stile Bayesian Methods for Hackers
There is a very nice short online book called Probabilistic Programming and Bayesian Methods for Hackers; it features figures created with Matplotlib, and uses a nice set of rc parameters to create a consistent and visually-appealing style throughout the book. This style is reproduced in the bmh stylesheet:

stile dark background
For figures used within presentations, it is often useful to have a dark rather than light background. The dark_background style provides this:

stile Grayscale
Sometimes you might find yourself preparing figures for a print publication that does not accept color figures. For this, the grayscale style, shown here, can be very useful:

stile Seaborn
Matplotlib also has stylesheets inspired by the Seaborn library (discussed more fully in Visualization With Seaborn [prossimamente]). As we will see, these styles are loaded automatically when Seaborn is imported into a notebook. I’ve found these settings to be very nice, and tend to use them as defaults in my own data exploration.

With all of these built-in options for various plot styles, Matplotlib becomes much more useful for both interactive visualization and creation of figures for publication. Throughout this book, I will generally use one or more of these style conventions when creating plots.


Post a comment or leave a trackback: Trackback URL.



Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:


Stai commentando usando il tuo account Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

%d blogger hanno fatto clic su Mi Piace per questo: