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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/SpecializedPlotsGroup(["`Specialized Plots`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/box_plots("`Box Plots`") matplotlib/PlotCustomizationGroup -.-> matplotlib/grid_config("`Grid Configuration`") matplotlib/SpecializedPlotsGroup -.-> matplotlib/violin_plots("`Violin Plots`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/list_comprehensions("`List Comprehensions`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48590{{"`Boxplot vs Violin`"}} matplotlib/figures_axes -.-> lab-48590{{"`Boxplot vs Violin`"}} matplotlib/box_plots -.-> lab-48590{{"`Boxplot vs Violin`"}} matplotlib/grid_config -.-> lab-48590{{"`Boxplot vs Violin`"}} matplotlib/violin_plots -.-> lab-48590{{"`Boxplot vs Violin`"}} python/booleans -.-> lab-48590{{"`Boxplot vs Violin`"}} python/for_loops -.-> lab-48590{{"`Boxplot vs Violin`"}} python/list_comprehensions -.-> lab-48590{{"`Boxplot vs Violin`"}} python/lists -.-> lab-48590{{"`Boxplot vs Violin`"}} python/tuples -.-> lab-48590{{"`Boxplot vs Violin`"}} python/importing_modules -.-> lab-48590{{"`Boxplot vs Violin`"}} python/standard_libraries -.-> lab-48590{{"`Boxplot vs Violin`"}} python/math_random -.-> lab-48590{{"`Boxplot vs Violin`"}} python/numerical_computing -.-> lab-48590{{"`Boxplot vs Violin`"}} python/data_visualization -.-> lab-48590{{"`Boxplot vs Violin`"}} python/build_in_functions -.-> lab-48590{{"`Boxplot vs Violin`"}} end

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