Boxplot vs Violin

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This tutorial will guide you through the process of creating box plot and violin plot using Python Matplotlib library. Box plots and violin plots are used to visualize the distribution of data.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Import libraries

Before creating the plots, we need to import the necessary libraries. We will be using numpy to generate random data and matplotlib.pyplot to create the plots.

import matplotlib.pyplot as plt
import numpy as np

Generate data

We will generate some random test data using numpy.

np.random.seed(19680801)
all_data = [np.random.normal(0, std, 100) for std in range(6, 10)]

Create violin plot

We will create a violin plot using violinplot() method. This method takes multiple arguments such as data, showmeans, showmedians etc.

fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(9, 4))
axs[0].violinplot(all_data, showmeans=False, showmedians=True)
axs[0].set_title('Violin plot')

Create box plot

We will create a box plot using boxplot() method. This method takes multiple arguments such as data, labels, showmeans, notch etc.

axs[1].boxplot(all_data)
axs[1].set_title('Box plot')

Add grid lines and labels

We will add horizontal grid lines, set x-labels and y-labels to the plots.

for ax in axs:
    ax.yaxis.grid(True)
    ax.set_xticks([y + 1 for y in range(len(all_data))], labels=['x1', 'x2', 'x3', 'x4'])
    ax.set_xlabel('Four separate samples')
    ax.set_ylabel('Observed values')

Display the plots

Finally, we will display the plots using show() method.

plt.show()

Summary

In this tutorial, we learned how to create box plot and violin plot using Python Matplotlib library. We also learned how to add horizontal grid lines, set x-labels and y-labels to the plots. Box plots and violin plots are useful in visualizing the distribution of data.

Other Python Tutorials you may like