How to Style Pandas Plots and Charts | by Alan Jones | Towards Data Science

Web sites, publications and news sources all have their own styles. Take a look at the financial data published by, say, the BBC or The NewYork Times. Or polling data on Nate Silver’s FiveThirtyEight web site. They each have a clear and consistent look.
We are going to look at how we can achieve something similar with our Pandas charts and plots. First, by using the built-in styles that are available to us and then by looking at how we can create our own customizations.
The default style renders a line graph like the image, below. It’s a clean looking image but there are several more built-in styles, if you prefer something different.
To get a list of all of the styles available from Mathplotlib use the following command.
plt.style.available
You will get a list that looks like this (note that it is possible that different versions of mathplotlib may have different styles — these are the ones that are included on my system with matplotlib 3.3.4)
['Solarize_Light2', '_classic_test_patch', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark', 'seaborn-dark-palette', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10']
You can change to a particular style by executing code like this before you start plotting.
matplotlib.style.use('ggplot')
This selects the ggplot style which would give you a plot that looks like the image below.
Many of the styles have been created for the plotting package Seaborn but you can use them with any plotting library that is based on mathplotlib.
Other styles are emulations of other plotting systems or web sites. The ggplot style is based on the the ggplot2 library that is commonly used in the R language.
At the end of this article I’ve appended a list of images that show all the styles from the list above.
Here is a bit of code that you can play around with to experiment with different styles.
import pandas as pdimport matplotlibimport matplotlib.pyplot as pltimport numpy as np# Change 'default' to the style that you want to try outmatplotlib.style.use('default')df = pd.DataFrame(np.random.rand(100, 2), columns=['x', 'y'])df.plot.scatter(x='x', y='y')
The code just generates two columns of random data and plots a scatter diagram. Here’s what I get when I change default to dark_background.
And this is Solarize_Light2:
So that’s how we change to one of the built-in styles and one of these might be suitable for your purposes. Or maybe you want to be a little more adventurous.
The attributes for Matplotlib graphs are stored in a dictionary called rcParams. You can change individual attributes by setting values in that dictionary. So, if you wanted to change the font size from it default value of 10, to something smaller, you could do this:
matplotlib.rcParams['font.size'] = 8
Any subsequent plot that you make will now have font size of 8.
You can easily find all of the rcParams simply by printing them.
print(matplotlib.rcParams)
There are an awful lot of them and the purpose of some are not that obvious but here are a few where the purpose is fairly clear:
          'font.family': ['sans-serif'],          'font.size': 10.0,          'font.style': 'normal',          'font.weight': 'normal',          'lines.color': 'C0',          'lines.linestyle': '-',          'lines.linewidth': 1.5,          'scatter.marker': 'o',          'text.color': 'black',
Here is a short program that illustrates how these parameters can be used. We’re going to plot a scatter diagram like the ones above but with some of the parameters changed.
matplotlib.rcParams['scatter.marker'] = '*'matplotlib.rcParams['figure.facecolor'] = 'yellow'matplotlib.rcParams['axes.facecolor'] = 'lightblue'matplotlib.rcParams['font.family'] = 'serif'matplotlib.rcParams['font.size'] = 8matplotlib.rcParams['font.weight'] = 'bold'matplotlib.rcParams['text.color'] = 'red'df.plot.scatter(x='x', y='y', title='Scatter graph')plt.show()
And here is the result:
You can see what changing the each of the parameters has done in the resulting chart — the meaning of these parameters is fairly clear. (I am not, by the way suggesting that you adopt this particular color scheme!)
You don’t have to specify the parameters you want in every program you write. You can create your own stylesheet and ue that instead of one of the built-in ones.
All you have to do is create a text file with the rcParams set the way you want them and then use that as your stylesheet in a similar way to the built-in ones, for example:
matplotlib.style.use('path-to-my-style-sheet')
You don’t have to specify all of the rcParams, of course, only the ones that you want to change.
So, as you can see, Matplotlib rcParams lets you create your own unique, and reusable, style for your Pandas plots.
The examples here art, of course, deliberately simple, but you can explore how the parameters are used in the built-in styles by looking at their source code.
You can find the code for all the different styles in the Matplotlib Github repository in the Stylelib folder. Try looking at the Classic style — this demonstrates setting pretty much all of the possible parameters but you should take a look at some of the others, too.
Finally, below are the images related to each of the built-in themes.
As always, thanks for reading and I hope you found this useful. If you’d like to know when I publish other articles, please consider subscribing to my free, occasional, newsletter here.
Thee images below are the same graph rendered with the built-in themes in matplotlib version 3.3.4.