BOBOBK

Four Plotting Tips in Python

TECHNOLOGY

When visualizing data, it’s often necessary to plot multiple charts in a single figure. For example, if you want to visualize the same variable from different perspectives (e.g., side-by-side histograms and boxplots of numerical variables), multiple plots are useful. In this article, I share 4 simple but practical tips for plotting multiple charts.

Dataset 📦

Let’s import packages and update default settings to add some personal style to the charts. We’ll use Seaborn’s built-in dataset ’tips':

import seaborn as sns  # v0.11.2  
import matplotlib.pyplot as plt  # v3.4.2  
sns.set(style='darkgrid', context='talk', palette='rainbow')
df = sns.load_dataset('tips')  
df.head()


Tip 1: plt.subplots()

An easy way to plot multiple subplots is to use plt.subplots(). Here’s an example syntax for plotting 2 side-by-side subplots:

fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10,4))  
sns.histplot(data=df, x='tip', ax=ax[0])  
sns.boxplot(data=df, x='tip', ax=ax[1]);

You can further customize each subplot, for example by adding titles:

fig, ax = plt.subplots(1, 2, figsize=(10,4))  
sns.histplot(data=df, x='tip', ax=ax[0])  
ax[0].set_title("Histogram")  
sns.boxplot(data=df, x='tip', ax=ax[1])  
ax[1].set_title("Boxplot");

To plot the same pair of charts for all numerical variables in a loop:

numerical = df.select_dtypes('number').columns
for col in numerical:  
    fig, ax = plt.subplots(1, 2, figsize=(10,4))  
    sns.histplot(data=df, x=col, ax=ax[0])  
    sns.boxplot(data=df, x=col, ax=ax[1]);

Tip 2: plt.subplot()

Another way to visualize multiple plots is using plt.subplot() (note: no “s” at the end). The syntax differs slightly:

plt.figure(figsize=(10,4))  
ax1 = plt.subplot(1, 2, 1)  
sns.histplot(data=df, x='tip', ax=ax1)  
ax2 = plt.subplot(1, 2, 2)  
sns.boxplot(data=df, x='tip', ax=ax2);

This method is especially useful when plotting the same type of chart for multiple variables side-by-side in a single figure:

plt.figure(figsize=(14,4))  
for i, col in enumerate(numerical):  
    ax = plt.subplot(1, len(numerical), i+1)  
    sns.boxplot(data=df, x=col, ax=ax)

You can also add titles to each subplot:

plt.figure(figsize=(14,4))  
for i, col in enumerate(numerical):  
    ax = plt.subplot(1, len(numerical), i+1)  
    sns.boxplot(data=df, x=col, ax=ax)   
    ax.set_title(f"Boxplot of {col}")

Tip 3: plt.tight_layout()

When plotting multiple charts, labels sometimes overlap neighboring subplots, like this:

categorical = df.select_dtypes('category').columns
plt.figure(figsize=(8, 8))  
for i, col in enumerate(categorical):  
    ax = plt.subplot(2, 2, i+1)  
    sns.countplot(data=df, x=col, ax=ax)

The x-axis labels of the top plots get cut off, and y-axis labels overlap. Using plt.tight_layout() fixes this nicely:

plt.figure(figsize=(8, 8))  
for i, col in enumerate(categorical):  
    ax = plt.subplot(2, 2, i+1)  
    sns.countplot(data=df, x=col, ax=ax)   
plt.tight_layout()


Tip 4: plt.suptitle()

Add an overall title to the whole figure:

plt.figure(figsize=(8, 8))  
for i, col in enumerate(categorical):  
    ax = plt.subplot(2, 2, i+1)  
    sns.countplot(data=df, x=col, ax=ax)   
plt.suptitle('Category counts for all categorical variables')  
plt.tight_layout()

You can also customize individual subplots by adding titles, legends, or other features as needed.

Related

Google Advertisement

Python Script to Snatch Recently Expired Domains

TECHNOLOGY
Python Script to Snatch Recently Expired Domains

'Many domain enthusiasts scour forums and websites frantically searching for and snatching up suitable domains, even spending heavily to buy desired domains from their owners. International domain management bodies adopt a "first-to-apply, first-to-register, first-to-use" policy. Since domains only require a small annual registration fee, continuous registration grants you the right to use the domain. Because of this, many domain resellers (commonly known as "domaining pros") often spend heavily on short, easy-to-remember domains. I used to think about buying shorter domains for building scraping sites, but unfortunately, both snatching and buying from others were very expensive. Since it"s first-come, first-served, we can also acquire good domains by registering them before the current owner forgets to renew.'

Google Advertisement

Solving Expert-Level Sudoku Puzzles Quickly Using Python's Backtracking Algorithm

TECHNOLOGY
Solving Expert-Level Sudoku Puzzles Quickly Using Python's Backtracking Algorithm

I often play Sudoku in my leisure time as a form of relaxation. My usual method involves eliminating duplicates and filling in unique numbers first, then proceeding step by step. However, it's inevitable to guess numbers and adjust based on feedback. So, is there a better algorithm to solve Sudoku puzzles? Here, I will use the backtracking method in Python to solve 9x9 expert-level Sudoku puzzles.