-
wbdata 5
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
Lecture1.4
-
Lecture1.5
-
-
Hexbin Plots 7
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
Lecture2.4
-
Lecture2.5
-
Lecture2.6
-
Lecture2.7
-
-
Heatmap 5
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
Lecture3.4
-
Lecture3.5
-
-
Boxplot 2
-
Lecture4.1
-
Lecture4.2
-
-
Violin Plot 5
-
Lecture5.1
-
Lecture5.2
-
Lecture5.3
-
Lecture5.4
-
Lecture5.5
-
-
Time Series 2
-
Lecture6.1
-
Lecture6.2
-
-
Pairplot 2
-
Lecture7.1
-
Lecture7.2
-
-
Kernel Density Estimation 3
-
Lecture8.1
-
Lecture8.2
-
Lecture8.3
-
Violin Plot Part 3
Solution
def name(x):
if x < 1980:
return "Before"
else:
return "After"
df["Era"] = df["date"].apply(name)
df
Now we can call our violin plot but we need to give an x, y, data and hue variable. X will be country, y will be GDP Rate, and hue will be the era since that is what we are trying to differentiate.
sns.violinplot(x="country", y="GDP Rate", hue="Era", data=df)
plt.show()
We can also switch hue and x to get a different view of the plots.
sns.violinplot(x="Era", y="GDP Rate", hue="country", data=df)
plt.show()
Going back to the original plot, we can have it so that instead of two plots side by side we get the two together as one side each of the plot. We do this by using split=True
sns.violinplot(x="country", y="GDP Rate", hue="Era", data=df, split=True)
plt.show()
Something interesting to note is how the variance on China’s GDP growth in recent years has declined heavily. Finally, something we can also do is set the color palette so that we pick the color to represent each era. We do this by giving a dictionary which maps the era to a color code. Let’s do green and red.
sns.violinplot(x="country", y="GDP Rate", hue="Era", data=df, split=True,palette={"Before": "r", "After": "g"})
plt.show()
Let’s restrict the range of values again.
sns.violinplot(x="country", y="GDP Rate", hue="Era", data=df[(df < 20) & (df > -10)], split=True,palette={"Before": "r", "After": "g"})
plt.show()
Now that we have covered violin plots, let’s get a quick bonus graph in, a simple bar graph.
Challenge