Seaborn - Part 1
Seaborn - Part 1
Importing Libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
# Ignore all warnings
warnings.filterwarnings("ignore")
Scatter Plots
A scatter plot is a type of plot used to visualize the relationship between two numerical variables. It displays individual data points as dots or
markers on a two-dimensional plane, with one variable represented on the x-axis and the other variable represented on the y-axis. The position
of each data point on the plot corresponds to its values for the respective variables.
Scatter plots are useful for understanding patterns, trends, and correlations between variables. They allow us to identify the presence or
absence of any relationship between the variables. When the points on the scatter plot form a discernible pattern, it suggests a relationship
between the variables. Different patterns can indicate positive or negative correlations, linear or nonlinear relationships, clusters or groups of
data, or the absence of any apparent relationship.
In addition to displaying the data points, scatter plots can be enhanced with additional visual cues. These include adding a regression line to
show the overall trend of the data, coloring the data points based on a categorical variable to differentiate groups or categories, and varying the
size of the markers to represent a third numerical variable.
Seaborn, a popular Python data visualization library, provides various functions, such as relplot and scatterplot, for creating scatter plots. These
functions offer options to customize the appearance of scatter plots, such as changing marker styles, adding trend lines, adjusting the color
palette, and incorporating additional variables through the use of hue, style, and size parameters.
Overall, scatter plots are a valuable tool for visualizing the relationship between two numerical variables and gaining insights into the underlying
data patterns and trends.
# Basic scatter plot using 'relplot' without specifying any additional parameters
sns.relplot(x="total_bill", y="tip", data=tips)
plt.show()
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 1/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
# Scatter plot with hue parameter to differentiate points by 'smoker' category
sns.relplot(x="total_bill", y="tip", data=tips, hue="smoker")
plt.show()
# Scatter plot with hue and style parameters to differentiate points by 'smoker' and 'time' categories respectively
sns.relplot(x="total_bill", y="tip", data=tips, hue="smoker", style="time")
plt.show()
# Scatter plot with hue parameter to map the 'size' category to different colors using a specified palette
sns.relplot(x="total_bill", y="tip", data=tips, hue="size", palette="ch:r=-5,l=.75")
plt.show()
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 2/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
# Scatter plot with size parameter to vary the size of the points based on the 'size' category
sns.relplot(x="total_bill", y="tip", data=tips, size="size")
plt.show()
# Scatter plot with size parameter and custom range for sizes
sns.relplot(x="total_bill", y="tip", data=tips, size="size", sizes=(15, 200))
plt.show()
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 3/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
# Scatter plot with hue and style parameters using explicit axes object
ax = sns.scatterplot(x="total_bill", y="tip", data=tips, hue="smoker", style="time")
plt.show()
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 4/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
# Create a figure and axes using subplots with 4 rows and 3 columns
fig, axes = plt.subplots(nrows=4, ncols=3, figsize=(15, 20))
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 5/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
Line Plot
A line plot is a type of graph that represents data points connected by straight lines. It is commonly used to display the trend or progression of a
variable over a continuous range, such as time or a numerical sequence. In a line plot, the x-axis typically represents the independent variable
(e.g., time) and the y-axis represents the dependent variable (e.g., measurement or value).
Line plots are effective for visualizing the overall pattern, trend, and variability of a variable. They allow us to observe how the values change
over the range of the independent variable and identify any relationships, trends, or patterns that may exist. Line plots can reveal information
such as upward or downward trends, fluctuations, seasonality, or the absence of any noticeable trend.
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 6/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
In addition to displaying the main line connecting the data points, line plots can include additional elements to enhance the visualization. These
elements may include markers at each data point to highlight individual values, error bars to represent uncertainty or variability, shading or
bands to indicate confidence intervals, and labels or annotations to provide additional information.
Seaborn, a popular Python data visualization library, provides functions such as relplot and lineplot for creating line plots. These functions offer
various customization options, allowing you to adjust the line style, marker style, color palette, and other visual aspects of the plot. You can also
incorporate additional variables using parameters like hue or style to differentiate groups or categories.
Line plots are widely used in many fields, including scientific research, finance, and data analysis, to visualize time series data, trends, and
patterns. They provide a concise and intuitive way to understand and communicate the behavior and evolution of variables over a continuous
range.
# Create a DataFrame with a time column ranging from 0 to 499 and a value column
df = pd.DataFrame(dict(time=np.arange(500), value=np.random.randn(500).cumsum()))
# Create a line plot using seaborn's relplot with x as "time" and y as "value"
g = sns.relplot(x="time", y="value", kind="line", data=df)
fmir= sns.load_dataset("fmri")
# Create a line plot with x as "timepoint" and y as "signal" from the fmri dataset
sns.relplot(x="timepoint", y="signal", data=fmir, kind="line")
<seaborn.axisgrid.FacetGrid at 0x7f6143f091e0>
# Create a line plot with x as "timepoint" and y as "signal", with no confidence interval (ci=None)
sns.relplot(x="timepoint", y="signal", data=fmir, kind="line", ci=None)
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 7/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
<seaborn.axisgrid.FacetGrid at 0x7f614267fe50>
# Create a line plot with x as "timepoint" and y as "signal", using the standard deviation as the confidence interval (ci="sd")
sns.relplot(x="timepoint", y="signal", data=fmir, kind="line", ci="sd")
<seaborn.axisgrid.FacetGrid at 0x7f614340e380>
# Create a line plot with x as "timepoint" and y as "signal", without using any estimator (estimator=None) and no confidence interval (ci=None)
sns.relplot(x="timepoint", y="signal", estimator=None, data=fmir, kind="line", ci=None)
<seaborn.axisgrid.FacetGrid at 0x7f6144893a00>
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 8/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
<seaborn.axisgrid.FacetGrid at 0x7f614243ceb0>
# Create a line plot with x as "timepoint" and y as "signal", colored by "region" and styled by "event"
sns.relplot(x="timepoint", y="signal", data=fmir, kind="line", hue="region", style="event")
<seaborn.axisgrid.FacetGrid at 0x7f6144fe6380>
# Create a line plot with x as "timepoint" and y as "signal", colored by "region" and styled by "event", without dashes and with markers
sns.relplot(x="timepoint", y="signal", data=fmir, kind="line", hue="region", style="event", dashes=False, markers=True)
<seaborn.axisgrid.FacetGrid at 0x7f6142a38280>
# Create a line plot with x as "timepoint" and y as "signal" for the subset of data where event is 'stim',
# colored by "region" and using "subject" as the unit of observation without using any estimator
sns.relplot(x="timepoint", y="signal", data=fmir.query("event == 'stim'"), kind="line", hue="region", units="subject", estimator=None)
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 9/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
<seaborn.axisgrid.FacetGrid at 0x7f6143172da0>
# Load the dots dataset from seaborn and filter it for align == 'dots'
dots = sns.load_dataset("dots").query("align == 'dots'")
# Create a line plot with x as "time" and y as "firing_rate", colored by "coherence" and styled by "choice"
sns.relplot(x="time", y="firing_rate", data=dots, kind="line", hue="coherence", style="choice")
<seaborn.axisgrid.FacetGrid at 0x7f6143d13400>
# Create a line plot with x as "time" and y as "firing_rate", colored by "coherence", styled by "choice" and using the custom palette
sns.relplot(x="time", y="firing_rate", data=dots, kind="line", hue="coherence", style="choice", palette=palette)
<seaborn.axisgrid.FacetGrid at 0x7f61431736a0>
# Create a DataFrame with a time column ranging from "2019-01-01" to "2019-05-15" and a value column
df = pd.DataFrame(dict(time=pd.date_range("2019-01-01", periods=500), value=np.random.randn(500).cumsum()))
# Create a line plot with x as "time" and y as "value" for the DataFrame with dates
g = sns.relplot(x="time", y="value", kind="line", data=df)
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 10/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
# Rotate the x-axis labels for better visibility
g.fig.autofmt_xdate()
Certainly! Here's a small description explaining how relplot with kind="line" can be replaced with lineplot:
Both relplot and lineplot are functions provided by the Seaborn library for creating line plots. However, there is a difference in how they are used.
relplot is a figure-level function in Seaborn that is designed to create a variety of plots, including line plots, scatter plots, and others. It provides
a convenient interface for creating complex visualizations by specifying the kind parameter. When kind is set to "line", relplot generates a line
plot.
On the other hand, lineplot is an axes-level function in Seaborn specifically designed for creating line plots. It offers more flexibility and control
over the plot's customization and behavior.
To replace relplot with lineplot for line plots, you can directly use lineplot to create the desired line plots. Instead of specifying kind="line", you
can pass the necessary arguments to lineplot function, such as x, y, and other optional parameters like hue, style, or ci.
By using lineplot, you can have more fine-grained control over the customization of your line plots, such as adjusting the linestyle, marker style,
adding error bars, or modifying the color palette.
Overall, if you need a more specialized and customizable line plot, it is recommended to use lineplot directly. However, if you prefer a simpler
interface and want to create a line plot along with other types of plots, relplot with kind="line" can be a convenient option.
# Create a figure and axes using subplots with 4 rows and 3 columns
fig, axes = plt.subplots(nrows=4, ncols=3, figsize=(15, 20))
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 11/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 12/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
# Line plot with multiple relationships based on subject, region, and event
sns.relplot(x="timepoint", y="signal", data=fmir, kind="line", hue="subject",col = "region",row="event",estimator = None)
<seaborn.axisgrid.FacetGrid at 0x7f614322baf0>
df= pd.DataFrame(np.random.randn(500,2).cumsum(axis=0),columns=["x","y"])
# Line plot with multiple relationships based on event, subject, and region
sns.relplot(x="timepoint", y="signal", data=fmir.query("region == 'frontal'"), kind="line", hue="event",style = "event",col = "subject",col_wrap=5,height=5,asp
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 13/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
<seaborn.axisgrid.FacetGrid at 0x7f61434d2c80>
Categorical Scatterplot
sns.set(style="ticks", color_codes=True)
<seaborn.axisgrid.FacetGrid at 0x7f417f2cba00>
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 14/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
<seaborn.axisgrid.FacetGrid at 0x7f417f237880>
# Swarm plot
sns.catplot(x="day", y="total_bill", data=tips, hue="day", kind="swarm") # Swarm plot of total_bill by day
<seaborn.axisgrid.FacetGrid at 0x7f417cfa3520>
<seaborn.axisgrid.FacetGrid at 0x7f417cfd48b0>
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 15/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
<seaborn.axisgrid.FacetGrid at 0x7f417d040730>
<seaborn.axisgrid.FacetGrid at 0x7f417ceea170>
<seaborn.axisgrid.FacetGrid at 0x7f417ce0fcd0>
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 16/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
<seaborn.axisgrid.FacetGrid at 0x7f417ce41060>
fig, axes = plt.subplots(2, 4, figsize=(16, 8)) # Create a figure with two rows and four columns
# Swarm plot
sns.swarmplot(x="day", y="total_bill", data=tips, hue="day", ax=axes[0, 2]) # Swarm plot of total_bill by day
axes[0, 2].set_title("Swarm plot") # Set subplot title
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 17/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
# Boxplot
sns.boxplot(x="day", y="total_bill", data=tips) # Boxplot of total_bill by day
<seaborn.axisgrid.FacetGrid at 0x7f41799e59c0>
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 18/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
<seaborn.axisgrid.FacetGrid at 0x7f417bce5630>
<seaborn.axisgrid.FacetGrid at 0x7f417937ca30>
<seaborn.axisgrid.FacetGrid at 0x7f417befff10>
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 19/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
<seaborn.axisgrid.FacetGrid at 0x7f417c006050>
# Violin plot with split violins, individual sticks, and "sex" category
sns.catplot(x="day", y="total_bill", data=tips, hue="sex", inner="stick", split=True, kind="violin") # Violin plot of total_bill by day (split by sex) with in
<seaborn.axisgrid.FacetGrid at 0x7f417c051e70>
# Boxplot
sns.boxplot(x="day", y="total_bill", data=tips, ax=axes[0, 0])
axes[0, 0].set_title("Boxplot")
# Violin plot with split violins, individual sticks, and "sex" category
sns.violinplot(x="day", y="total_bill", data=tips, hue="sex", split=True, inner="stick", ax=axes[1, 3])
axes[1, 3].set_title("Violin plot with split violins and individual sticks")
plt.tight_layout()
plt.show()
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 20/21
7/6/23, 11:31 AM Seaborn_Day1.ipynb - Colaboratory
https://colab.research.google.com/drive/1Cbs_JpUjjKbvFExhvH16VzkMnyr7OlvK#scrollTo=wB12tfbDoMkm&printMode=true 21/21