Matplotlib Makeover: 6 Python Styling Libraries for Amazing Plots
If you’re a data scientist, as I am, you know that no matter how well you understand and investigate a piece of data, the final judgement of your results is going to be based on your visualizations. And what’s even worse, the plots are supposed to be not only informative and intuitive for a random observer, but also stylish.
In my opinion, python matplotlib and seaborn styles are somewhat boring and overused. Sometimes, they may even suggest that the author didn’t invest much time or care into the project. To avoid this and add flair to your plots, I’ve discovered 6 python libraries which can color regular line/scatter plots, histograms and other basic visualizations. For each package I provided a link for a github repo, where you can find more needy-greedy details and give a star to the authors!
Let’s kick off by generating some synthetical data. I decided to make 4 subplots using each library, so you could compare them on the same kind of data. The full version of my code you can find here.
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
'''Generating points to create a scatter plot'''
def scatter():
x = np.random.random(100)
y = x-np.random.random(100)
z = np.random.randint(0,4,100)
df = pd.DataFrame({'X':x, 'Y': y, 'Z':z})
return df
'''Generating points to create a line plot'''
def line():
x = np.arange(0,10,0.1)
y_1 = np.sin(x)
y_2 = np.sin(x+5)
y_3 = np.sin(x+10)
y_4 = np.sin(x+15)
y_5 = np.sin(x+20)
df = pd.DataFrame({'X':x,'y_1':y_1, 'y_2':y_2,
'y_3':y_3, 'y_4':y_4, 'y_5':y_5})
return df
'''Sampling data from several distributions'''
def hist():
x_1 = np.random.normal(1,0.1,1000)
x_2 = np.random.gamma(1,0.25,1000)
x_3 = np.random.normal(0.4, 0.1,1000)
x_4 = np.random.normal(-0.1, 0.3,1000)
df = pd.DataFrame({'X_1': x_1, 'X_2':x_2, 'X_3': x_3, 'X_4':x_4})
return df
Now let’s dive into the libraries I explored.
Note: I didn’t specify any color or colormap, the goal was just to apply a style to the same set of plots.
1. Aquarel
This library provides 11 (!) different styles, including dark and light ones, and can be installed using pip:
pip install aquarel
You can use these themes via context manager like I did down below:
with load_theme("arctic_light"):
fig, ax = plt.subplots(ncols=2, nrows=2, figsize=(16,9))
df = scatter()
f= ax[0,0].scatter(df.X,df.Y, c=df.Z, s=50)
ax[0,0].set_xlabel('X data')
ax[0,0].set_ylabel('Y data')
handles, labels = f.legend_elements(prop="colors", alpha=0.6)
legend2 = ax[0,0].legend(handles, labels, loc="lower right")
df=line()
df.plot(x='X', ax=ax[0,1])
df=hist()
sns.kdeplot(df, fill=True, ax=ax[1,0])
ax[1,0].set_xlabel('Value')
sns.kdeplot(df, x="X_1", y="X_2", fill=True, ax=ax[1,1])
sns.kdeplot(df, x="X_3", y="X_4",fill=True, ax=ax[1,1])
ax[1,1].set_xlabel('Dist 1')
ax[1,1].set_ylabel('Dist 2')
plt.suptitle('Aquarel\narctic_light', fontsize=24)
plt.savefig('arctic_light.jpg')
plt.show()
Or alternatively you can just use:
from aquarel import load_theme
theme = load_theme("arctic_light")
theme.apply()
# ... plotting code here
theme.apply_transforms()
2. Rosé Pine
The second repo, which I really liked, isn’t a library, but a set of themes which you need to download and then specify a path for matplotlib:
plt.style.use('./themes/rose-pine-moon.mplstyle')
And after that you just do the same plotting steps. From my perspective, this package has a really soft palette, and at the same time it has enough contrast.
3. Catppuccin
This library you’ll need to install using pip. It comprises 4 different styles with different degree of darkness. To produce this plot I used:
matplotlib.style.use("mocha")
Catppuccin package has also one interesting feature — mixing different style sheets. Let’s combine basic seaborn-v0_8-dark and mocha.
It was definitely not the best decision of my life, but I hope you catch my drift — Catppuccin mixes themes:)
4. mplcyberpunk
The next library is really famous, because everyone loves cyberpunk. This package not only provides you with proper colors and a background, but also it can add glowing effect to your plots, which is absolutely amazing!
Just look at what it can do!
5. matplotx
A truly lovely library matplotx is another extention for matplotlib can be installed like the previous ones using pip. It has 20 (!!!!!!) different themes for you, so it can be used for scientific, pitching or any other purposes as these styles range from really strict to informal and stylish.
You definitely should have a look, I bet you’ll find your favorite there.
Even though in this repo there is only one theme, I do like the combination of font, line and background colors, so I couldn’t help but include this one into the article!
Hopefully you’ll use some of these themes in future, because they are amazing!
What is your favorite here? Share in comments👇
===========================================
P.s. I’m extremely passionate about (Geo)Data Science, ML/AI and Climate Change. So if you want to work together on some project pls contact me here or in LinkedIn.
🛰️Follow for more🛰️