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

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python

Build a Face Recognition System in Python using FaceNet
In this deep learning project, you will build your own face recognition system in Python using OpenCV and FaceNet by extracting features from an image of a person's face.

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

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

OSZAR »