How to Create Violin Plot in Matplotlib: Simple Guide
Use
matplotlib.pyplot.violinplot() to create a violin plot by passing your data as an array or list. This function visualizes data distribution combining box plot and kernel density estimation in one plot.Syntax
The basic syntax for creating a violin plot in Matplotlib is:
matplotlib.pyplot.violinplot(data, positions=None, widths=0.5, showmeans=False, showmedians=False, showextrema=True)
Here, data is your dataset (list or array). positions sets the location of violins on the x-axis. widths controls the width of each violin. The showmeans, showmedians, and showextrema flags control which summary statistics are displayed.
python
matplotlib.pyplot.violinplot(data, positions=None, widths=0.5, showmeans=False, showmedians=False, showextrema=True)
Example
This example shows how to create a violin plot for three groups of random data. It demonstrates how the plot displays the data distribution and summary statistics.
python
import matplotlib.pyplot as plt import numpy as np # Create sample data: 3 groups with 100 points each np.random.seed(0) data = [np.random.normal(loc, 1, 100) for loc in [0, 5, 10]] # Create violin plot plt.violinplot(data, showmeans=True, showmedians=True) plt.title('Violin Plot Example') plt.xlabel('Group') plt.ylabel('Value') plt.show()
Output
A window opens showing a violin plot with three violins representing the three groups. Each violin shows the data distribution shape with a white dot for the mean and a thick line for the median.
Common Pitfalls
Common mistakes when creating violin plots include:
- Passing data in the wrong shape (e.g., a flat list instead of a list of groups).
- Not setting
positionswhen plotting multiple groups, causing overlapping violins. - Confusing violin plots with box plots; violin plots show distribution shape, not just summary stats.
Always check your data shape and use showmeans or showmedians to highlight key statistics.
python
import matplotlib.pyplot as plt import numpy as np # Wrong: flat list instead of list of groups wrong_data = np.random.normal(0, 1, 300) # Right: list of arrays for groups right_data = [np.random.normal(loc, 1, 100) for loc in [0, 5, 10]] plt.figure(figsize=(10,4)) plt.subplot(1,2,1) plt.violinplot([wrong_data]) plt.title('Wrong Data Shape') plt.subplot(1,2,2) plt.violinplot(right_data) plt.title('Correct Data Shape') plt.show()
Output
Two violin plots side by side: the left one shows a single violin for all data combined (wrong), the right one shows three separate violins for each group (correct).
Quick Reference
Tips for using violinplot():
- Pass data as a list of arrays for multiple groups.
- Use
positionsto control x-axis placement. - Set
showmeans=Trueorshowmedians=Trueto highlight statistics. - Adjust
widthsto change violin thickness. - Combine with
plt.xticks()to label groups.
Key Takeaways
Use matplotlib.pyplot.violinplot() with data as a list of arrays to create violin plots.
Show means or medians with flags to highlight key statistics on the plot.
Ensure data shape matches expected input to avoid plotting errors.
Adjust positions and widths to control violin placement and size.
Violin plots visualize data distribution shape, not just summary statistics.