How to make a violinplot in matplotlib example 2?

This recipe helps you make a violinplot in matplotlib example 2

Recipe Objective

Violin plots are similar to boxplots which showcases the probability density along with interquartile, median and range at different values. They are more informative than boxplots which are used to showcase the full distribution of the data. They are also known to combine the features of histogram and boxplots.

Sentiment Analysis Project on eCommerce Product Reviews with Source Code

They are mainly used to compare the distribution of different variables/columns in the dataset. There are different libraries used to plot this chart. The basic library that we can use is Matplotlib.

This recipe demonstrates how to make a violin plot using matplotlib

Step 1: Import required libraries

# importing matplotlib import matplotlib.pyplot as plt # importing numpy library to get 2 samples of normal distributions import numpy as np

Step 2: Creating 2 sample normal distribution arrays

We use np.random.normal(size = n) function to get the normal distribution array of size "n"

x = np.random.normal(size = 1000) # normal distribution with mean 10 and standard deviation 5 y = np.random.normal(10, 5, size = 1000) # creating a list of arrays for comparison in the later step l = [x, y]

Step 3: Violin Plot

We use violinplot() function to plot the chart.

Syntax: violinplot(dataset, showmeans=False, showextrema=True, showmedians=False, quantiles=None)

  1. dataset = (input data) vector or list of arrays ;
  2. showmeans: (optional) If True, will display means. ;
  3. showextrema:(optional) If True, will display extremas. ;
  4. showmedians: (optional) If True, will display medians
  5. quantiles: (optional) If not None, set a list of floats in interval [0, 1]

# Create a figure instance fig = plt.figure() # Create an axes instance ax = fig.add_axes([0,0,1,1]) # Create the boxplot bp = ax.violinplot(l, showmeans = True , showmedians = True, quantiles = [[0.25,0.75],[0.25,0.75]]) # Giving a title to the plot plt.title("Violin Plot") # Showcasing the plot plt.show()

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Llama2 Project for MetaData Generation using FAISS and RAGs
In this LLM Llama2 Project, you will automate metadata generation using Llama2, RAGs, and AWS to reduce manual efforts.

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.