-
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 2
Solution
sns.violinplot(data=df[(df < 20) & (df > -10)],inner=None)
sns.swarmplot(data=df[(df < 20) & (df > -10)],color='k')
plt.title("GDP Growth Rate")
plt.show()
Now, let’s do some more data work so that we can try to visualize the data differently. In reality, we could just call wbdata() and use it like we have before, but I want to show you how to use pd.melt().
First, reset the index.
df.reset_index(inplace=True)
df
Now, the way pd.melt() works is you need to give it three arguments and it melts down all the columns into rows. The first is the dataframe which you want to melt, the second is the id vars which means which means the variables you want to keep as identifiers in the melted dataframe and finally the value_names which is the name for the values in our melted dataframe.
df = pd.melt(df,id_vars=["date"],value_name="GDP Rate")
df
Now, what we are going to do is create a new column which describes if the row’s year is before or equal to/after 1980. We will do this so we can split our violin plots by time. Before we do this, check out what the type of the year column is.
print(df["date"])
The type is object, we don’t want that because we are going to compare the column to an integer. Let’s convert the column to a numerical type, pd.to_numeric() will do this.
df["date"] = pd.to_numeric(df["date"])
Challenge