Plotting charts with Seaborn. Create attractive, intriguing charts... | by Jenny Dcruz | Towards Data Science

Seaborn is a powerful Python library which was created for enhancing data visualizations. It provides a large number of high-level interfaces to Matplotlib. Seaborn works well with dataframes while Matplotlib doesn’t. It lets you plot striking charts in a much simpler way.
For better understanding of this article, you will need to know the basics of pandas as well as matplotlib. If not, you can refer to the following articles on the same:
Pandas for data analysisVisualizations with Matplotlib
Pandas for data analysis
Visualizations with Matplotlib
Make sure you have the necessary libraries installed in your system:
Using conda:
conda install pandasconda install matplotlibconda install seaborn
Using pip:
pip install pandaspip install matplotlibpip install seaborn
Let’s first import the required Python libraries and our dataset.
You can find the CSV file to this tutorial here.
import pandas as pdfrom matplotlib import pyplot as pltimport seaborn as snsdf = pd.read_csv('Pokemon.csv', index_col = 0, encoding='unicode-escape')df.head()
In the above code, we set index_col to 0 which indicates that we are treating the first column as the index.
Using seaborn and the attributes from our Pokemon dataset we shall create some very interesting visualizations. The first thing that we’ll look at are scatter plots.
Scatter plots use dots to represent values for different numeric variables. The position of each dot on the horizontal and vertical axis indicates values for an individual data point. They are used to observe relationships between variables. Making Scatterplots in seaborn takes just one line of code using the ‘lmplot’ function. To do so, we pass in the dataframe to the data argument and then pass in the column names for the x and y axes.
By default, scatterplot also displays a regression line which simply is a line that best fits the data.
sns.lmplot(x=’Attack’, y=’Defense’, data=df)plt.show()
Here you can see our scatter plot which shows the attack scores compared to the defense scores.
The regression line basically shows us the correlation between the two axes. In this case it is sloping upwards. Which means when attack scores get higher, so do defense scores. To remove the regression line, we can set the ‘fitreg’ argument to false.
Furthermore, we can set the hue argument to color the individual plots by the Pokemon’s evolution stage. This hue argument is very useful because it allows you to express a third dimension of information with color.
sns.lmplot(x=’Attack’, y=’Defense’, data=df, fit_reg=False, hue=’Stage’)plt.show()
The scatter plot looks the same as the one before except that now it doesn’t have the regression line in the middle and it also has different colors for each point. The colors just show the stage of each individual pokemon. Stage is just another attribute in the data which we saw before.
Looking at this plot, we can make the conclusion that the pokemon in stage 1 or the blue dots usually have lower scores than the pokemons in the higher stages.
Boxplots are one of the important plots that are often used for displaying the distribution of data. It only takes a line of code in seaborn to display a boxplot using its boxplot function. In this example, we’ll use the whole dataframe except for the total, stage and legendary attributes.
df_copy = df.drop([‘Total’, ‘Stage’, ‘Legendary’], axis=1)sns.boxplot(data=df_copy)
Here we can see that each attribute has its individual boxplot.
A box plot is based on a 5 number summary which are each displayed as different lines. The middle line is the median value and is the point where the data is centered around. The bottom and top most lines towards the ends of the box plot are the medians of quartile 1 and 4 which basically show the minimum and maximum of the distribution. The other two lines in the middle are medians of quartile 2 and 3 which show how much the values vary from the median. The single points outside this range indicate any outliers in the data.
Violin plots are similar to boxplots. Violin plots are very useful alternatives to boxplots. They show the distribution through the thickness of the violin instead of only the summary statistics. Violin plots are known to be very handy while analyzing and visualizing the distribution of different attributes in a dataset.
In this example, we’ll use the same dataframe copy from the last example.
sns.violinplot(data=df_copy)plt.show()
We can observe the distribution of values for each attribute of the pokemon. The areas where the violin is thicker means that there is a higher density of values. The middle of the violin plot is typically thicker meaning that there’s a high density of values there. Next we’ll visualize the distribution of the attack scores compared the pokemons primary type. To do this, lets use the same violin plot method.
plt.figure(figsize=(10,6))\sns.violinplot(x='Type 1', y='Attack', data=df)plt.show()
This plot shows the distribution of attack scores for each pokemon’s primary type. As you can see, the ‘Dragon’ type pokemon has the highest attack scores but they also have a higher variance which means that they also have attack scores that are very low. The ‘Ghost’ primary type has a very low variance which means most of their data values are concentrated in the center.
Heatmaps help you visualize matrix type data. For example, we can visualize all the correlations between different attributes of the pokemons.
Let us calculate the correlation for our dataframes by calling the ‘corr’ function and plot our heatmap using the ‘heatmap’ function.
corr = df_copy.corr()sns.heatmap(corr)
The above heatmap that displays the correlation of our dataframe.
The lighter the color of the box, the higher the correlation is between those two attributes. For example, the correlation between HP and the overall speed of a pokemon is very low. Hence, the dark color of the box. The correlation between the HP and the speed of defense is quite high therefore we can see a red block in the heatmap. We can see that when one attribute gets higher, so does the other attributes such as the speed of the defense.
Histograms allow you to plot the distribution of numeric values. If we were to use matplotlib to create histograms, it would require a bit more work as compared to creating the same with seaborn. With seaborn, it only takes one line to create a distribution.
For example, we can create a histogram to plot the distribution values with the attack attribute.
sns.distplot(df.Attack, color=’blue’)
We can see that most pokemon are within the 50–100 range. There are much less pokemons with attack values greater than 100 or less than 50 as we can see here.
Similar to bar graphs, calplots let you visualize the distribution of every category’s variables. We can use a calplot to see how many pokemon there are in each primary type.
sns.countplot(x=’Type 1', data=df)plt.xticks(rotation=-45)
We can see that the ‘Water’ type has the most pokemon where as the ‘Fairy’ and the ‘Ice’ categories has the least.
Density plot display the distribution between two variables. For instance, we can use the density plot to compare two attributes of a pokemon: attack values, defense values. We’ll use the ‘jointplot’ function to do this.
sns.jointplot(df.Attack, df.Defense, kind=’kde’, color=’lightblue’)
‘kde’ indicates that we want a density plot.
As you can see, the plot area varies in darkness depending on how many values are in the area. The dark areas signal a very strong relationship. We can see from this plot, when the attack values are between 50 and 75, defense values are around 50.
That’s about it for this article. I hope you enjoyed visualizing data using seaborn.
You can find the code as well as the dataset to this article here.
Thank you for giving it a read!
[1] Seaborn documentation: https://seaborn.pydata.org/