Seaborn

Introduction to Seaborn

  • Extension of Matplotlib for statistical data visualization.
  • Installed as: pip install seaborn.
  • Supports complex visualizations with less code.
  • Charts: scatterplot, lineplot, barplot, histplot, boxplot, heatmap etc.
  • Integrates with Matplotlib for further customization.
  • Example: import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset("tips") fig, ax = plt.subplots() sns.scatterplot(data=df, x="total_bill", y="tip", ax=ax) ax.set_title("Scatter Plot with Seaborn") plt.show()

Line Plot in Seaborn

  • Used to show trends over time or continuous data.
  • Created with sns.lineplot().
  • Matplotlib attributes like linestyle, color, linewidth are valid.
  • hue: Adds color encoding based on a categorical variable.
  • Example: import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset("fmri") fig, ax = plt.subplots() sns.lineplot(data=df, x="timepoint", y="signal", hue="event", ax=ax) ax.set_title("Line Plot with Seaborn") plt.show()

Bar Plot in Seaborn

  • Used to compare values across categories. categorical vs numeric.
  • Created with sns.barplot().
  • estimator: Function that computes the bar height. Default is mean.
  • orientation: vertical (default) or horizontal for horizontal bars.
  • Order and hue order can be controlled with order and hue_order parameters.
  • Adds standard error bars which can be customized with errorbar parameter.
  • Example: import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset("tips") fig, ax = plt.subplots() sns.barplot(data=df, x="day", y="total_bill", hue="sex", ax=ax) ax.set_title("Bar Plot with Seaborn") plt.show()

Scatter Plot in Seaborn

  • Used to show relationship between two continuous variables.
  • Created with sns.scatterplot().
  • Parameters: hue, size, markers, edgecolor, alpha, linewidth etc.
  • Example: import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset("tips") fig, ax = plt.subplots() sns.scatterplot(data=df, x="total_bill", y="tip", hue="sex", ax=ax) ax.set_title("Scatter Plot with Seaborn") plt.show()

Box Plot in Seaborn

  • Used to show the distribution of a continuous variable across categories.
  • Parameters: hue, order, hue_order, width, fliersize, linewidth, showcaps, boxprops, medianprops, etc.
  • Example: import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset("tips") fig, ax = plt.subplots() sns.boxplot(data=df, x="day", y="total_bill", ax=ax) ax.set_title("Box Plot with Seaborn") plt.show()

Histogram in Seaborn

  • Used to show the distribution of a single continuous variable.
  • Created with sns.histplot(). Customizations of Matplotlib valid.
  • Example: import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset("tips") fig, ax = plt.subplots() sns.histplot(data=df, x="total_bill", bins=30, kde=True, ax=ax) ax.set_title("Histogram with Seaborn") plt.show()

Heatmap in Seaborn

  • Used to show data as colors in a matrix format.
  • Created with sns.heatmap().
  • Parameters: annot, fmt, cmap, linewidths, linecolor, annot_kws, square etc.
  • Example: import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset("tips") fig, ax = plt.subplots() tabular_data = pd.crosstab(df["day"], df["sex"]) sns.heatmap(data=tabular_data, annot=True, ax=ax) ax.set_title("Heatmap with Seaborn") plt.show()

Count Plot in Seaborn

  • Used to show the count of observations in each categorical bin.
  • Created with sns.countplot().
  • Parameters: hue, order, hue_order, palette etc.
  • Example: import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset("tips") fig, ax = plt.subplots() sns.countplot(data=df, x="day", ax=ax) ax.set_title("Count Plot with Seaborn") plt.show()

Figure Level Plots in Seaborn

  • Figure-level plots are created with sns.relplot(), sns.catplot(), sns.displot().
  • They create their own figure and can be used for complex visualizations.
  • We can create sub-plots by specifying col and row parameters.
  • Example: import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset("tips") sns.relplot(data=df, x="total_bill", y="tip", hue="day", kind="scatter") plt.show()

Relational Plot in Seaborn

  • Used to show relationships between two variables.
  • Created with sns.relplot(). kind controls the type of plot (scatter, line).
  • Customizations of lineplot and scatterplot are valid for relplot.
  • Parameters: hue, col, row, col_wrap, alpha etc.
  • Example: import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset("tips") sns.relplot(data=df, x="total_bill", y="tip", kind="scatter", col="day") plt.show()

Customizing Figure Level Plots of Seaborn

  • Figure-level plots can be customized using the returned FacetGrid object.
  • suptitle, xlabel, ylim, xticks, xticklabels etc. can be set on the figure.
  • Example: import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset("tips") g = sns.relplot(data=df, x="total_bill", y="tip", kind="line", row="day") g.figure.suptitle("Line Plot with Seaborn") g.set(ylim=(0, 10)) g.set(xlabel="Total Bill", ylabel="Tip") plt.show()

Categorical Plot in Seaborn

  • Used to show the distribution of a categorical variable.
  • Created with sns.catplot(). kind controls the type of plot.
Categorical Plot Types in Seaborn:
KindDescriptionBest For
stripShows all data points with jitter to avoid overlap.Comparing distributions across categories
swarmSimilar to strip plot but adjusts points to avoid overlap.Showing all data points with minimal overlap
boxShows distribution with box plot.Displaying summary statistics and outliers
violinShows distribution with a kernel density estimate.Visualizing the shape of the distribution
barShows mean (or other estimator) with confidence intervals.Comparing central tendency across categories
pointSame as bar plot but shows individual data points.Comparing central tendency with individual data points
countShows count of observations in each categorical bin.Showing the frequency of each category
Different types of categorical plots available in Seaborn for visualizing categorical data

Figure Showing Different Categorical Plots

Figure showing different categorical plots in Seaborn with examples
Figure showing different categorical plots in Seaborn with examples

Sample Code for Categorical Plots in Seaborn

  • import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset("tips") g = sns.catplot(data=df, x="day", y="total_bill", kind="violin", col="sex") g.set(xlabel="Day of Week", ylabel="Total Bill", title="Total Bill by Day") plt.show()
  • Try different kind values to see various categorical plots and understand their differences.

Distribution Plot in Seaborn

  • Used to show the distribution of a single continuous variable.
  • Created with sns.displot(). kind controls the type of plot (hist, kde, ecdf).
  • Pass kde=True to show kernel density estimate on top of histogram.
  • Customizations of Matplotlib valid for displot.
  • Example: import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset("tips") sns.displot(data=df, x="total_bill", kind="hist", bins=30, kde=True) ax.set_title("Distribution Plot with Seaborn") plt.show()

lmplot in Seaborn

  • Shows scatter plot with a fitted regression line.
  • Created with sns.lmplot().
  • Combines regplot and FacetGrid for complex visualizations.
  • Order of polynomial regression can be set with order parameter.
  • Example: import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset("tips") g = sns.lmplot(data=df, x="total_bill", y="tip", col="day") g.fig.suptitle("Linear Relationship with Seaborn") plt.show()

Joint Plot in Seaborn

  • Shows scatter plot with marginal histograms or KDEs.
  • Created with sns.jointplot(). kind controls the type of plot.
  • Example: import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset("tips") sns.jointplot(data=df, x="total_bill", y="tip", kind="scatter") plt.show()

Pair Plot in Seaborn

  • Shows pairwise relationships in a dataset.
  • Create n x n grid of plots for n variables where, - diagonal shows univariate distribution - off-diagonal shows bivariate relationships.
  • Created with sns.pairplot().
  • Parameters: hue, vars, x_vars, y_vars, kind, diag_kind etc.
  • Example: import seaborn as sns import matplotlib.pyplot as plt df = sns.load_dataset("tips") sns.pairplot(data=df) plt.show()

Changing Context and Color Palette

    Context:
    1. Controls the scale of plot elements to suit different contexts.
    2. Options: paper (smallest), notebook (default), talk, poster (largest).
    3. Set context with sns.set_context("context_name").
    Color Palette:
    1. Controls the color scheme of plots.
    2. Options: deep, muted, bright, pastel, dark, colorblind.
    3. Set palette with sns.set_palette("palette_name").
  • Customization of style using Matplotlib is still valid when using Seaborn.
  • Adjust style, context, palette together to create visual for your purpose.