DDC-1: Violin plot
A data challenge a day helps you master machine learning
About these daily data challenges
Each post is an exercise that helps you learn about data in Python.
Try to solve the exercise before checking my solution at the bottom of the post 🤓
You can share your solution or visualization in the comments!
Today’s challenge
Generate the following violin plot. The two datasets are normally distributed random numbers with a mean and standard deviation of 0 and 1 (data “1”), and 2 and 2 (data “2”). 100 samples per group.

.
.
.
.
Scroll down for the solution!
.
.
.
.
Keep scrolling!
.
.
.
.
import numpy as np
import matplotlib.pyplot as plt
# generate the data
N = 100
x1 = np.random.normal(0,1,N)
x2 = np.random.normal(2,2,N)
# create the violin plot
v = plt.violinplot([x1,x2])
plt.gca().set(title='Violin Plot', ylabel='Value',xticks=[1,2])
# change the colors
v['bodies'][0].set_facecolor([.7,.7,.9])
v['bodies'][1].set_facecolor([.7,.9,.7])
v['cbars'].set_edgecolor('w')
v['cmins'].set_edgecolor('w')
v['cmaxes'].set_edgecolor('w')
plt.show()
thanks !