Violin Plot
Let’s import the same data as last lesson.
import wbdata
import pandas as pd
countries = ["AFR","CHN","USA","DNK","FRA","LBN","LKA","LUX"]
import datetime
df = wbdata.get_dataframe({'NY.GDP.MKTP.KD.ZG': 'GDP Rate'}, country=countries)
df = df.unstack("country")
df.columns = df.columns.droplevel()
df
Seaborn uses violinplot() for plotting, you just need to give the data as an argument.
import seaborn as sns
import matplotlib.pyplot as plt
sns.violinplot(data=df)
plt.title("GDP Growth Rate")
plt.show()
Let’s restrict values to -10 to 20.
sns.violinplot(data=df[(df < 20) & (df > -10)])
plt.title("GDP Growth Rate")
plt.show()
We can use inner=”quart” to set up quarter lines in the violin plot.
sns.violinplot(data=df[(df < 20) & (df > -10)],inner="quart")
plt.title("GDP Growth Rate")
plt.show()
We can also plot a swarmplot over the violin plot by setting inner=None on the violin plot and using a swarmplot after.
Challenge
Try this for yourself.