Home Artificial Intelligence Find out how to Create Cyberpunk-Styled Seaborn Violin Plots with Minimal Python Code Importing Libraries and Loading Data Creating the Seaborn Violin Plots Applying Cyberpunk Style to Seaborn Figures Summary Dataset Utilized in this Tutorial

Find out how to Create Cyberpunk-Styled Seaborn Violin Plots with Minimal Python Code Importing Libraries and Loading Data Creating the Seaborn Violin Plots Applying Cyberpunk Style to Seaborn Figures Summary Dataset Utilized in this Tutorial

0
Find out how to Create Cyberpunk-Styled Seaborn Violin Plots with Minimal Python Code
Importing Libraries and Loading Data
Creating the Seaborn Violin Plots
Applying Cyberpunk Style to Seaborn Figures
Summary
Dataset Utilized in this Tutorial

An easy tutorial on learn how to enhance your Seaborn violin plots with ease

Towards Data Science
Cyberpunked seaborn violin plots showing density variations for various lithologies encountered inside a well. Image by the creator.

Violin plots are a standard data visualisation that mixes the facility of a boxplot and a density plot right into a single plot. This enables us to visualise more information inside a single figure. For instance, we are able to view the fundamental statistics from the boxplot, discover possible outliers, and consider the distribution of that data. This will help us understand if the info is skewed or incorporates multi-modal distributions.

Inside my latest series of articles, I actually have been exploring ways to enhance and enhance basic matplotlib figures using various themes, including a cyberpunk style. This style provides a futuristic neon-like appearance to the plots and only requires a few lines of code to use to matplotlib and seaborn figures.

If you need to learn more, you may see how I applied it to matplotlib figures within the article below.

Inside this short tutorial, we’ll take the fundamental seaborn violin plot and cyberpunk it.

We’ll start by importing the libraries we’ll work inside this tutorial.

These are matplotlib and seaborn for visualising our data, pandas for loading and storing our data, and mplcyberpunk for applying 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 following step we’d like to perform is to load our data. This is completed using the read_csv() function from pandas and passing in the placement of the info file.

The info we’re going to be using is a subset of the combined XEEK and Force 2020 Machine Learning competition that was aimed toward predicting lithology from well log measurements. Further details of this dataset may be found at the tip of the article.

df = pd.read_csv('data/Xeek_Well_15-9-15.csv')
Pandas dataframe containing well log measurements for well 15/19–15. Image by the creator.

After we view the dataframe ( df ) we get the above image. We will see that we’ve got a single well’s value of knowledge extending from 485m right 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 will call upon the next code to create the fundamental violin plot.

We first set the figure size to 10 x 5, which is able to give us a decent-sized figure to take a look at, after which we call upon sns.violinplot() and pass within the required parameters.

plt.figure(figsize=(10,5))
sns.violinplot(x='LITH', y='RHOB', data=df)

After we run the above code, we get back the next plot.

Basic seaborn violin plot showing variation in Bulk Density (RHOB) with each lithology. Image by the creator.

At first glance, the returned plot looks good and useable, nonetheless, we are able to improve the style using the mplcyberpunk library.

To use the cyberpunk style to our plot, all we’d like to do is add an additional line to the code. This line of code uses a with statement after which calls upon plt.style.context and it allows us to use the style simply to the plot that’s being called beneath this line moderately than changing the worldwide style for all plots.

with plt.style.context('cyberpunk'):
plt.figure(figsize=(10,5))
sns.violinplot(x='LITH', y='RHOB', data=df)

After we run the code above, we’ll get the next violin plot which has many of the cyberpunk theme applied.

Seaborn violin plot after applying the mplcyberpunk theme. Image by the creator.

One in all the processes that the mplcyberpunk library should do is change the colors of the violins. Nevertheless, in our case, this hasn’t been applied. But it may easily be fixed.

We’d like to create an inventory of the cyberpunk colors to repair it. These colors were extracted from the mplcyberpunk source code, but they may be modified to any colors you wish. Remember, should you are going for cyberpunk styling, we’d likely use brilliant neon colors.

Along with creating an inventory of colors, we also can sort the order of the violins so that they’re in alphabetical order. That is an optional step, but an excellent one, especially when comparing multiple datasets with the identical categories.

my_pal=['#08F7FE', '#FE53BB', '#F5D300', '#00ff41', 'r', '#9467bd', '#de014f']

lith_order = df['LITH'].sort_values().unique()

To use the colors to the files, we are able to pass my_pal into the palette parameter for the violin plot.

Nevertheless, to use the identical color to the sides/lines of the plots, we’d like to access collections, which store an inventory of all of the parts of the violin plot.

Inside this list, every two consecutive items correspond to 1 violin: the primary is the body of the violin, and the second is the mini box plot.

Due to this fact we’d like to account for this in our for loop.

with plt.style.context('cyberpunk'):
plt.figure(figsize=(15,10))
g=sns.violinplot(x='LITH', y='RHOB', data=df, palette=my_pal,
order=lith_order)

for i in range(len(g.collections)//2):
# divide by 2 because collections include each violin
# bodies and the mini box plots
g.collections[i*2].set_edgecolor(my_pal[i])
g.collections[i*2].set_alpha(0.8)

After we run the above code, we get back the next plot with our cyberpunk violins.

Cyberpunked seaborn violin plot for various lithologies encountered inside a well. Image by the Writer.

Now that we’re capable of control the lines and the colors of our plot, we are able to make just a few final tweaks by changing the alpha of fill to make it barely brighter and increasing the dimensions of our x and y-axis labels.

with plt.style.context('cyberpunk'):
plt.figure(figsize=(15,10))

g=sns.violinplot(x='LITH', y='RHOB', data=df, palette=my_pal,
order=lith_order)

for i in range(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 well. Image by the Writer.

The mplcyberpunk library provides a fast and straightforward strategy to immediately transform your plots from the default styling to something that has a futuristic appearance.

When creating graphics like this, it’s at all times essential to contemplate your audience and be certain that the message and story you are attempting to convey continues to be clear.

Subset of a training dataset used as a part of a Machine Learning competition run by Xeek and FORCE 2020 (Bormann et al., 2020). This dataset is licensed under Creative Commons Attribution 4.0 International.

The complete dataset may be accessed at the next link: https://doi.org/10.5281/zenodo.4351155.

LEAVE A REPLY

Please enter your comment!
Please enter your name here