How to Draw a plotly Violin Plot in Python (Example)
Here is a quick highlight of what this tutorial is going to teach you:
Looks like we are ready to get started. Let’s go!
What is a Violin Plot?
A violin plot is a type of plot that is used to view the distribution of numeric data, and make comparisons among groups. It shows different statistics, such as the first quartile, the median, and the third quartile. It is quite similar to a box plot but also quite different in that it reports more information than a box plot by showing the entire distribution (and density) of the numeric data. Violin plots are sometimes considered as a combination of a box plot and a kernel-density plot.
Requirements to Draw a plotly Violin Plot
Now that we know what a violin plot is and how it is used, we are going to build one in plotly using Python. To achieve that, we must first install the plotly library in our Python programming IDE of choice by running the code below:
pip install plotly
The above line of code will install the plotly library and all its dependencies. Let us now load the Python plotly library and all the plot-building functions it contains, including the one we are going to use to draw our violin plot. Please run the code below:
import plotly.express as px
How to Draw a plotly Violin Plot
Great! We are now ready to draw a violin plot in plotly. We will make use of the in-built tips DataFrame. Run the code below to load the DataFrame:
df = px.data.tips()
And you can print it to your console by running:
# total_bill tip sex smoker day time size #0 16.99 1.01 Female No Sun Dinner 2 #1 10.34 1.66 Male No Sun Dinner 3 #2 21.01 3.50 Male No Sun Dinner 3 #3 23.68 3.31 Male No Sun Dinner 2 #4 24.59 3.61 Female No Sun Dinner 4 #.. ... ... ... ... ... ... ... #239 29.03 5.92 Male No Sat Dinner 3 #240 27.18 2.00 Female Yes Sat Dinner 2 #241 22.67 2.00 Male Yes Sat Dinner 2 #242 17.82 1.75 Male No Sat Dinner 2 #243 18.78 3.00 Female No Thur Dinner 2
We shall now draw a violin plot of the total bill column of the DataFrame. Run the below lines of code:
fig = px.violin(df, y="total_bill") fig.show()
Cool! We have just drawn a basic violin plot. When you hover over the plot, you can see some useful statistical information about the data, like the max, the min, median, q1, and q3. These statistics help you to understand the distribution of the numeric data, as explained earlier in this tutorial.
Combine a Violin Plot and a Box Plot
At the start of this tutorial, we said that the violin plot and the box plot are quite similar, but also have their differences. Now, we are going to combine both plots and see how useful that combination is. In your IDE, please run the code below to draw a combined plot:
fig = px.violin(df, y="total_bill", box=True) fig.show()
One immediate benefit of combining a violin plot with a box plot is that without hovering over the plot, you can easily tell the median point of the data, which is around $17. Also, by looking at the box plot whiskers, you can readily tell that some bills are as high as $40 and as low as $10. And this makes your graph more informative to your user. All we needed to do to combine plots was to set the argument
box = True in the
So there you have it! In this tutorial, we have been introduced to the violin plot and have seen how we can draw one in plotly using the Python programming language. We have also seen how we can combine the violin plot and the box plot for enhanced visual communication.
I hope you liked this tutorial and learned something new. Thanks, and I will see you in the next one!
Video, Further Resources & Summary
Do you need more explanations on how to draw a violin graph in plotly using Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to a violin plot in plotly using Python.
The YouTube video will be added soon.
Furthermore, you could have a look at some other tutorials on Statistics Globe:
- How to Draw a plotly Scatterplot in Python (Example)
- How to Draw a plotly Line plot in Python (Example)
- Change plotly Axis Label in Python (Example)
- Percentage as Axis Ticks Labels in plotly Graph in Python (Example)
- How to Order Bars in plotly Barchart in Python (Example)
- Format Title of plotly Graph in Python (Example)
- Learn Python
This post has shown how to how to draw a violin graphic in plotly using the Python programming language. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.