in

Easy methods to Create Cyberpunk-Styled Seaborn Violin Plots with Minimal Python Code | by Andy McDonald | Jun, 2023


A easy tutorial on how one can improve your Seaborn violin plots with ease

Cyberpunked seaborn violin plots exhibiting density variations for various lithologies encountered inside a effectively. Picture by the writer.

Violin plots are a standard information visualisation that mixes the ability of a boxplot and a density plot right into a single plot. This enables us to visualise extra data inside a single determine. For instance, we will view the essential statistics from the boxplot, determine potential outliers, and consider the distribution of that information. This may also help us perceive if the information is skewed or incorporates 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 type. This type gives a futuristic neon-like look to the plots and solely requires a few strains of code to use to matplotlib and seaborn figures.

If you wish to study extra, you may see how I utilized it to matplotlib figures within the article beneath.

Inside this brief tutorial, we are going to take the essential seaborn violin plot and cyberpunk it.

We are going to 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 finished 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 will be discovered on the finish of the article.

df = pd.read_csv('information/Xeek_Well_15-9-15.csv')
Pandas dataframe containing effectively log measurements for effectively 15/19–15. Picture by the writer.

Once we view the dataframe ( df ) we get the above picture. We are able to see that we have now a single effectively’s value of information extending from 485m all the way down to 3200m.

From the dataframe, we’re going to use two columns. The RHOB column, which incorporates the Bulk Density measurements, and the LITH column, which incorporates the lithological descriptions.

We are able to name upon the next code to create the essential violin plot.

We first set the determine measurement to 10 x 5, which can give us a decent-sized determine to take a look at, after which we name upon sns.violinplot() and move 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.

Primary seaborn violin plot exhibiting variation in Bulk Density (RHOB) with every lithology. Picture by the writer.

At first look, the returned plot appears to be like good and useable, nonetheless, we will enhance the type utilizing the mplcyberpunk library.

To use the cyberpunk type 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.type.context and it permits us to use the type simply to the plot that’s being referred to as beneath this line fairly than altering the worldwide type for all plots.

with plt.type.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 many of the cyberpunk theme utilized.

Seaborn violin plot after making use of the mplcyberpunk theme. Picture by the writer.

One of many processes that the mplcyberpunk library ought to do is change the colors of the violins. Nevertheless, in our case, this hasn’t been utilized. However it may well simply be fastened.

We have to create an inventory of the cyberpunk colors to repair it. These colors had been extracted from the mplcyberpunk supply code, however they are often modified to any colors you need. Bear in mind, if you’re going for cyberpunk styling, we might seemingly use vivid neon colors.

Along with creating an inventory of colors, we will additionally type the order of the violins in order that they’re in alphabetical order. That is an optionally available step, however a superb 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 move my_pal into the palette parameter for the violin plot.

Nevertheless, to use the identical color to the sides/strains of the plots, we have to entry collections, which retailer an inventory of all of the elements of the violin plot.

Inside this listing, 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.type.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 embody 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.

Cyberpunked seaborn violin plot for various lithologies encountered inside a effectively. Picture by the Writer.

Now that we’re in a position to management the strains 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.type.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)

Cyberpunked seaborn violin plot for various lithologies encountered inside a effectively. Picture by the Writer.

The mplcyberpunk library gives a fast and simple method to immediately remodel 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 be sure that the message and story you are attempting to convey continues 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 underneath Artistic Commons Attribution 4.0 Worldwide.

The complete dataset will be accessed on the following hyperlink: https://doi.org/10.5281/zenodo.4351155.


Environment friendly Picture Segmentation Utilizing PyTorch: Half 4 | by Dhruv Matani | Jun, 2023

Environment friendly Picture Segmentation Utilizing PyTorch: Half 3 | by Dhruv Matani | Jun, 2023