Violin plots are a typical information visualisation that mixes the ability of a boxplot and a density plot right into a single plot. This permits us to visualise extra data inside a single determine. For instance, we will view the fundamental statistics from the boxplot, determine attainable outliers, and consider the distribution of that information. This can assist us perceive if the information is skewed or comprises multi-modal distributions.
Inside my newest sequence of articles, I’ve been exploring methods to enhance and improve primary matplotlib figures utilizing numerous themes, together with a cyberpunk fashion. This fashion supplies a futuristic neon-like look to the plots and solely requires a few traces of code to use to matplotlib and seaborn figures.
If you wish to be taught extra, you possibly can see how I utilized it to matplotlib figures within the article under.
Inside this brief tutorial, we are going to take the fundamental seaborn violin plot and cyberpunk it.
We’ll begin by importing the libraries we are going to work inside this tutorial.
These are matplotlib and seaborn for visualising our information, pandas for loading and storing our information, and mplcyberpunk for making use of the cyberpunk theme to the seaborn chart.
import matplotlib.pyplot as plt
import pandas as pd
import mplcyberpunk
import seaborn as sns
After importing the required libraries, the subsequent step we have to perform is to load our information. That is executed utilizing the read_csv()
operate from pandas and passing within the location of the information file.
The information we’re going to be utilizing is a subset of the mixed XEEK and Force 2020 Machine Learning competition that was aimed toward predicting lithology from effectively log measurements. Additional particulars of this dataset may be discovered on the finish of the article.
df = pd.read_csv('information/Xeek_Well_15-9-15.csv')
Once we view the dataframe ( df
) we get the above picture. We are able to see that we’ve a single effectively’s price of knowledge extending from 485m all the way down to 3200m.
From the dataframe, we’re going to use two columns. The RHOB column, which comprises the Bulk Density measurements, and the LITH column, which comprises the lithological descriptions.
We are able to name upon the next code to create the fundamental violin plot.
We first set the determine dimension to 10 x 5, which is able to give us a decent-sized determine to have a look at, after which we name upon sns.violinplot()
and go within the required parameters.
plt.determine(figsize=(10,5))
sns.violinplot(x='LITH', y='RHOB', information=df)
Once we run the above code, we get again the next plot.
At first look, the returned plot seems to be good and useable, nonetheless, we will enhance the fashion utilizing the mplcyberpunk
library.
To use the cyberpunk fashion to our plot, all we have to do is add an additional line to the code. This line of code makes use of a with assertion after which calls upon plt.fashion.context
and it permits us to use the fashion simply to the plot that’s being referred to as beneath this line somewhat than altering the worldwide fashion for all plots.
with plt.fashion.context('cyberpunk'):
plt.determine(figsize=(10,5))
sns.violinplot(x='LITH', y='RHOB', information=df)
Once we run the code above, we are going to get the next violin plot which has a lot of the cyberpunk theme utilized.
One of many processes that the mplcyberpunk library ought to do is change the colors of the violins. Nonetheless, in our case, this hasn’t been utilized. However it could possibly simply be fastened.
We have to create a listing of the cyberpunk colors to repair it. These colors have been extracted from the mplcyberpunk supply code, however they are often modified to any colors you need. Keep in mind, if you’re going for cyberpunk styling, we’d doubtless use vivid neon colors.
Along with creating a listing of colors, we will additionally kind the order of the violins in order that they’re in alphabetical order. That is an non-compulsory step, however a very good one, particularly when evaluating a number of datasets with the identical classes.
my_pal=['#08F7FE', '#FE53BB', '#F5D300', '#00ff41', 'r', '#9467bd', '#de014f']lith_order = df['LITH'].sort_values().distinctive()
To use the colors to the information, we will go my_pal
into the palette parameter for the violin plot.
Nonetheless, to use the identical color to the perimeters/traces of the plots, we have to entry collections, which retailer a listing of all of the components of the violin plot.
Inside this record, each two consecutive gadgets correspond to 1 violin: the primary is the physique of the violin, and the second is the mini field plot.
Subsequently we have to account for this in our for loop.
with plt.fashion.context('cyberpunk'):
plt.determine(figsize=(15,10))
g=sns.violinplot(x='LITH', y='RHOB', information=df, palette=my_pal,
order=lith_order)for i in vary(len(g.collections)//2):
# divide by 2 as a result of collections embrace each violin
# our bodies and the mini field plots
g.collections[i*2].set_edgecolor(my_pal[i])
g.collections[i*2].set_alpha(0.8)
Once we run the above code, we get again the next plot with our cyberpunk violins.
Now that we’re in a position to management the traces and the colors of our plot, we will make just a few remaining tweaks by altering the alpha of fill to make it barely brighter and rising the scale of our x and y-axis labels.
with plt.fashion.context('cyberpunk'):
plt.determine(figsize=(15,10))g=sns.violinplot(x='LITH', y='RHOB', information=df, palette=my_pal,
order=lith_order)
for i in vary(len(g.collections)//2):
g.collections[i*2].set_edgecolor(my_pal[i])
g.collections[i*2].set_alpha(0.9)
g.set_ylabel('RHOBnn', fontsize=16)
g.set_xlabel('nnLithology', fontsize=16)
The mplcyberpunk library supplies a fast and simple solution to immediately rework your plots from the default styling to one thing that has a futuristic look.
When creating graphics like this, it’s at all times necessary to contemplate your viewers and make sure that the message and story you are attempting to convey remains to be clear.
Subset of a coaching dataset used as a part of a Machine Studying competitors run by Xeek and FORCE 2020 (Bormann et al., 2020). This dataset is licensed beneath Inventive Commons Attribution 4.0 Worldwide.
The complete dataset may be accessed on the following hyperlink: https://doi.org/10.5281/zenodo.4351155.