More Plots
So far we have been only looking at scatter plots. In this lesson let us cover additional plots that can be made with additional plotting functions.
Line Plot
In the below code editor we add the argument kind = 'line'
to the relplot
function. This will change the graph
from a scatter plot to a line plot.
How does this differ from a scatter plot? The scatter plot presents the values of two variables as points on a two-dimensional graph. In contrast, the line plot presented here draws a "best fit" line connecting the data points to provide a general trend. Accompanying this line, we notice a shaded region referred to as the confidence interval. This area visually communicates the reliability of the trend portrayed, encapsulating the possible variability and giving viewers a sense of the spread of data points around the predicted best fit line.
In cases where we prefer a cleaner graph with only the trend line,
it is possible to remove the confidence interval. To achieve this, let's
add the argument errorbar = None
when calling the
relplot
function.
A different line we may add to enhance the visualization, is that we
can overlay a line representing the
average Petal.Length on the coloured scatter plot.
The relplot
function is not so well suited
to making the horizontal line we would like to add, so we will
instead use the Matplotlib package again. As we learned in the
More Seaborn lesson we can add
to a Seaborn plot with Matplotlib functions.
In this case, we will use the plt.axhline
function.
The first argument is the y-value we would like
to plot the line at. We do not need to specify the x-value
or the dataframe, as this line will inherit the same x-axis
and data as in the Seaborn plot.
Bar Plot
Next, we will explore another useful visualization tool: the bar plot.
The bar plot excellently depict differences in magnitude across
various categories. We will create one using the sns.barplot
function, where we will look at the total sum Petal.Lengths for
each Species.
To construct this, we'll designate the Species category as our
x-axis, while the total sum of the Petal.Lengths will determine
the height of the bars on the y-axis. Before embarking on this, we
first need to calculate these total sums. We will do so, by employing a
groupby
method followed by a sum
method to
obtain the necessary data.
We note, we added the reset_index
method after
the sum
method to make the Species column a
column again, following the groupby
,
so that we can use it as the x-axis values in the plot.
Violin plot
The final type of visualization we will explore is known as a violin plot. A violin plot merges the characteristics of a box plot with a density plot, thereby providing a comprehensive view of the distribution of our data.
In this kind of plot, just as in the box plot, the x-axis typically represents different categories, while the y-axis demonstrates the continuous data values corresponding to those categories. Wherever the data is more dense, will create a bulge and create a "violin" shape visualization.
We can make a violin plot with the sns.violinplot
function. Let's
take a look at it!
There is a vast array of plots available for crafting detailed and insightful visualizations. To explore the extensive selection and find the one that best suits your data storytelling needs, visit the Seaborn gallary . Here, you will find a comprehensive reference guide to aid you in creating the most effective and illustrative plots.