Seaborn Tutorial 🖼

Part 2

Mulbah Kallen
Analytics Vidhya

--

In Part 1 of our Seaborn Cheat Sheet we covered a few of Seaborn’s capabilities. Now we will go into further detail by first discussing our ability to modify color palettes.

It would be nice if we could change the colors of our violin plots in order to match the typical color of the associated Pokemon type. Let’s create an order list of color hex values that matches the appropriate sequence of Pokemon types displayed along the x-axis.

Now all we have to do is use the palette = argument within our sns.violinplot and we should be good to go.

sns.violinplot(x= ’Type 1' , y=’Attack’ , data=df, palette=pkmn_type_colors)

Now let’s take a look at swarm plots. Swarm plots are similar to to violin plots in their positioning and shape but give us a better idea of the amount of data we are working with if the number of data points is not overwhelming.

What’s great is that the code for a swarmplot is identical to the code for creating a violin plot except we exchange sns.violinplot to sns.swarmplot.

v_plot = sns.swarmplot(x = ’Type 1' , y=’Attack’ , data=df , palette=pkmn_type_colors )
v_plot.set_xticklabels(v_plot.get_xticklabels( ), rotation=45 )
plt.rcParams[ ‘figure.figsize’ ]=( 20 , 10 )
plt.show( )

Being that our swarm plot and violin plot are so similar let’s go ahead and overlap them. Other than adjusting the figure size and changing the color of the dots to black, overlapping our plots is as easy as placing our code for each plot within the same code box. Try and read through the code below and see if you can understand whats happening.

This is great. We can cow see how Attack stats are vary across Pokemon types. Now we could create 4 more of these plots in order to visualize Defense Sp.Atk, Sp.Def and Speed but we know how this type of plot works and in some cases it may be more beneficial to view all of this data against one another. In order to do this we are going to use pandas melt( ) function.

melt( ) takes three arguments.
1. The DataFrame to melt.
2. ID variables to keep (Pandas will ‘melt’ all of the other ones).
3. Finally, a name for the new, melted variable.

The melt ( ) function has taken each column that was not specified in the keep list (id_vars) and has added it to the bottom of the newly created data frame(melted_df). For example, although you can’t see it Bulbasaur now has 6 rows of data. 1 row for each of its battle stats.

Now we can plot our our data but this time we will set our x = ‘Stat’ and our y = ‘value’ . This will display a plot comparing all the different stats. In case you’r wondering where the value column came from, the melted ( ) function created this column in order to store the values that were associated with each battle stat.

This is great but we still need to do a bit of cleaning up that way the visualization is more intuitive.

We set our hue = to ‘Type 1’ in order to view the variation in battles stats among the different Pokemon types but at the moment all our Types are blended together. To get a better view of this idea we will separate our points by hue using the argument dodge= True.
-We are also going to move the legend right outside our plot as it resting place is right in the center of our plot.
-Finally we will use the custom Pokemon color palette and adjust the y axis to get read of the space below zero.

If your plot looks like the plot above then well done!

This is the end of the seaborn tutorial. I am going to display a few more plot types below just to show you what seaborn is capable of. To view even more of what seaborn has to offer visit the Seaborn Example Gallery.

Heatmap

Histogram

Bar Plot

Density Plot

Joint Distribution Plot

--

--