Playing with Matplotlib style sheets
Team Data21 on matplotlib style visualization
Longing for good looking Matplotlib charts? But feeling too lazy to set each and every parameter manually? Try this time saving tip.
There's a rumor in data visualization community that you can do any conceivable visualization with Matplotlib. It is very likely true but the path to mastery might be challenging to many.
What is the right parameter? Should I apply it to axis, figure or plot? And what is the name of a method for changing the ticks?
Indeed, Matplotlib can be very confusing sometimes even for an advanced user.
How do you create attractive visualizations with minimum effort? Built-in style sheets can come to a rescue.
It is a simple way to change style of a chart. Think of it as of CSS for web sites. To put it another way - code for chart creation remains the same and you only change styling by calling one line of code. Neat.
# Set the style sheet plt.style.use('fivethirtyeight')
How do you get to know all available style sheets?
Easily. You can list their names.
# Available styles print(plt.style.available)
We put together a handy gallery of all built-in Matplotlib styles.
Yes, you can. The best start is to copy existing style sheet. Search for files with
.mplstyle extension, usually somewhere in
<environment-name>/lib/python3.x/site-packages/matplotlib/mpl-data/stylelib. It is just a text file with parameters.
Use path to your custom style sheet.
# Use external style sheet plt.style.use('./styles/my-custom-style.mplstyle')
matplotlib.rcdefaults() and you are back at defaults.
# Reset to defaults matplotlib.rcdefaults()
Note: We used Matplotlib version
3.5.1 for this tutorial.