A Guide to Data Visualization with Python: Part 2 – Seaborn and Venn Diagrams

Welcome back to our series on data visualization with Python! In part 1, we covered the basics of creating plots using Matplotlib. While Matplotlib is an extremely powerful and flexible library, creating polished statistical graphics can take a fair amount of code. In part 2, we‘ll level up our plotting capabilities using Seaborn, a library built on top of Matplotlib that provides a high-level interface for drawing attractive and informative statistical graphics. We‘ll also learn about Venn diagrams, a less common but useful plot type for visualizing set membership.

Why Data Visualization Matters

Before diving in, let‘s briefly revisit why data visualization is such an important skill for data scientists and analysts. As the saying goes, "a picture is worth a thousand words." Visualizing your data allows you to:

  • Explore datasets to spot patterns, trends, outliers, and relationships between variables
  • Communicate insights to stakeholders and decision-makers in an engaging and intuitive way
  • Support or refute hypotheses about your data
  • Highlight key takeaways from your analysis

Simply put, data visualization helps bring your data to life and makes it easier to extract meaning from the never-ending streams of numbers we work with. With the rise of big data, knowing how to effectively visualize data is more critical than ever.

Seaborn: Statistical Data Visualization

Seaborn is a Python data visualization library based on Matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics.

Some key features and advantages of Seaborn include:

  • Dataset-oriented API where you only need to specify the dataset and variables to plot
  • Automatic estimation and plotting of linear regression models
  • Convenient functions for visualizing univariate, bivariate, and multivariate relationships
  • Built-in themes for styling Matplotlib graphics
  • Ability to visualize entire dataframes at once
  • Specialized support for using categorical variables to show observations or aggregate statistics

Here‘s a quick example to give you a taste of how simple it is to create a nice-looking plot with Seaborn:

import seaborn as sns
tips = sns.load_dataset("tips")
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="day")

Seaborn scatterplot example

With just 3 lines of code, we loaded a built-in dataset of restaurant tips, created a scatterplot showing the relationship between total bill amount and tip amount, and added a categorical "day" variable represented by different hues.

Seaborn has many different plot types for various purposes, including:

  • Relational plots:
    — scatterplot
    — lineplot

  • Categorical plots:
    — barplot
    — countplot
    — boxplot
    — violinplot
    — stripplot
    — swarmplot

  • Distribution plots:
    — histplot
    — kdeplot
    — rugplot

  • Regression plots:
    — regplot
    — residplot

  • Matrix plots:
    — heatmap

  • Multi-plot grids:
    — pairplot
    — jointplot
    — FacetGrid

Let‘s take a deeper look at a few of these key plot types and how to customize them.

Scatterplots

Scatterplots are one of the most common ways to visualize the relationship between two continuous variables. We can easily add a third categorical variable by color-coding the points with a hue parameter:

import seaborn as sns
iris = sns.load_dataset("iris") 
sns.scatterplot(data=iris, x="sepal_length", y="sepal_width", hue="species")

Seaborn scatterplot with hue

We can see that the iris species are relatively well separated by sepal length and width, with setosa having the smallest sepal size.

Faceted Categorical Plots

Let‘s say we want to visualize the distribution of a continuous variable for different categories, like the total bill amount by day of the week. We could create individual barplots or box plots, but that‘s repetitive. Seaborn lets us easily create faceted plots showing the same plot type for different subsets of our data:

import seaborn as sns
tips = sns.load_dataset("tips")
sns.catplot(data=tips, x="day", y="total_bill", kind="bar", col="time")

Seaborn faceted barplot

The col parameter split the data into different subplots based on time of day, allowing us to compare the total bill distribution for lunch and dinner on each day of the week.

Pair Plots

When we have a dataset with multiple continuous variables, it‘s useful to be able to visualize all their pairwise relationships at once. That‘s where Seaborn‘s pairplot comes in:

import seaborn as sns
iris = sns.load_dataset("iris")
sns.pairplot(iris, hue="species")  

Seaborn pairplot

In one line of code, we can create a matrix of plots showing the bivariate relationships between all pairs of variables, along with the univariate distribution of each on the diagonal. The hue parameter color-codes the points by species. Pairplots are a great way to quickly explore your dataset.

Customizing Seaborn Plots

Seaborn comes with several built-in themes that affect all the plots in a session. We can set the theme with a simple function call:

import seaborn as sns
sns.set_theme(style="darkgrid")

Other themes include "whitegrid", "dark", "white", and "ticks". We can also set the color palette:

import seaborn as sns  
sns.set_palette("husl")

Seaborn offers several different color palettes to choose from.

Beyond these top-level theme settings, each plot also returns a matplotlib Figure and Axes, which we can use to further customize our plots with titles, labels, ticks, legends, etc. See the official tutorial for more.

Venn Diagrams

A Venn diagram shows all possible logical relationships between a finite collection of different sets. They depict elements as points in the plane, and sets as regions inside closed curves.

Venn diagram example

Venn diagrams are a popular way to visualize:

  • Unions and intersections between sets
  • Survey data (respondents who selected different combinations of options)
  • Overlapping between different groups
  • Shared versus distinct characteristics

While Venn diagrams may look simple, they are quite tricky to create using Matplotlib alone. Luckily, there is a convenient Matplotlib-Venn package we can use:

from matplotlib_venn import venn2, venn3

# 2-set Venn diagram
venn2(subsets = (10, 5, 2), set_labels = (‘Group A‘, ‘Group B‘))

# 3-set Venn diagram
venn3(subsets = (10, 8, 22, 6, 9, 4, 2), set_labels = (‘Group A‘, ‘Group B‘, ‘Group C‘))

Matplotlib-Venn examples

The venn2 function takes three arguments (the size of the two sets and their intersection), while venn3 takes seven arguments (the sizes of the three sets, their three pairwise intersections, and the intersection of all three).

With Matplotlib-Venn, it only takes a few lines of code to create an informative Venn diagram to visualize set membership and overlaps.

Other Plot Types

While we‘ve covered several of the most widely used plot types for statistical visualization, there are many other kinds of plots that may be useful depending on your specific data and use case. A few others to be aware of:

  • Network charts for visualizing connections between entities
  • Treemaps for displaying hierarchical data as nested rectangles
  • 3D plots for plotting three-dimensional points, lines, or surfaces
  • Geographic maps for plotting values on a map

We may explore some of these more specialized plots in a future article.

Conclusion

In this article, we‘ve seen how Seaborn makes it easy to create attractive, informative statistical graphics in Python with minimal code. Some of the key plots we covered include:

  • Scatterplots for visualizing relationships between two continuous variables, optionally grouped by a categorical variable
  • Faceted categorical plots for comparing the distribution of a variable across different subsets
  • Pair plots for visualizing all pairwise relationships in a dataset
  • Joint plots for combining univariate and bivariate visualizations

We also learned how to customize our Seaborn plots by setting different themes and color palettes.

Finally, we saw how to create Venn diagrams using Matplotlib-Venn to visualize logical relationships and overlaps between sets.

The key to becoming a data visualization pro is to familiarize yourself with the strengths and weaknesses of different plot types. Remember, the goal is to choose the plot that best fits your specific data and question.

As you continue on your data science journey, I encourage you to think critically about your visualizations. Strive to create plots that are not only beautiful, but also insightful. When designed well, your visualizations can uncover hidden patterns, challenge assumptions, and drive better decision-making.

I hope this article has expanded your toolkit and inspired you to go out and create awesome visualizations. Stay tuned for the next post in our data visualization series!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts