How to Style Plots with Matplotlib | by Lee Vaughan | Nov, 2023


Quick Success Data Science

Don’t settle for the defaults!

Setting the Style (Source: Cole Keister on Unsplash)

Decades ago, my mother gave me a maroon velour tracksuit as a Christmas present. It was a God-awful thing, and I replied that it wasn’t really in style. She snorted derisively and said, “You set the style! Be a trendsetter!”

Needless to say, I did NOT set the style, but my wife still teases me with the “You set the style!” quote. I do set the style, however, when using Matplotlib, and unlike a velour tracksuit, that’s a good thing.

For convenience, Python’s Matplotlib library lets you override its default plotting options. You can use this powerful feature to not only customize plots but to apply consistent, automatic, and reusable styles for reports, publications, and presentations.

In this Quick Success Data Science project, we’ll take a quick look at how to style plots with Matplotlib.

If you’ve used Matplotlib much, you’ve probably changed the default settings for a plot, such as for the color of a line, by passing new values to methods that made the plot. But what if you want to set these values for multiple plots at the same time, so that all the curves are the same color, or to cycle through a pre-defined order of colors?

Well, you can do this by using either:

  1. Runtime Configuration Parameters
  2. Style Files
  3. Style Sheets

Let’s look at each of these in turn.

One way to style plots is to set the parameters at runtime, using an instance of the RcParams class. The name of this class stands for runtime configuration parameters, and you run it from a notebook, script, or console using either the pyplot approach or the object-oriented style. (If you’re not familiar with these two methods, see my article, Demystifying Matplotlib).

The plotting parameters are stored in the matplotlib.rcParams variable, which is a dictionary-like object. There’s a very long list of configurable parameters, which you can view…



Source link

Be the first to comment

Leave a Reply

Your email address will not be published.


*