Add Textures to Seaborn Bar Plot in Python

In this tutorial, you’ll learn how to add textures to Seaborn bar plots using Matplotlib.

We’ll start by looking at different hatch patterns and how to apply them, then move on to combining these patterns for greater visual impact.

Finally, we’ll add an interactive and dynamic layer to the plots by using animation.

 

 

Exploring Different Hatch Patterns

First, ensure you have Seaborn installed. You can install it via pip:

!pip install seaborn

Now, let’s import the necessary libraries and prepare a sample dataset.

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
data = {
    'Category': ['A', 'B', 'C', 'D'],
    'Value': [23, 45, 56, 78]
}
df = pd.DataFrame(data)
sns.barplot(x='Category', y='Value', data=df)
plt.show()

Output:

Seaborn bar plot

Next, let’s add standard hatch patterns to these bars. Hatch patterns are simple yet effective in differentiating between categories or groups.

hatch_patterns = ['/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*']
plt.figure(figsize=(10, 6))
for i, bar in enumerate(sns.barplot(x='Category', y='Value', data=df).patches):
    bar.set_hatch(hatch_patterns[i % len(hatch_patterns)])
plt.show()

Output:

Different Hatch Patterns

 

Adjust Texture Properties

Adjusting texture properties like size, density, and angles improve the readability and appeal of your plots.

Let’s continue with our existing Seaborn bar plot and experiment with these texture properties.

sns.set(style="whitegrid")
ax = sns.barplot(x="Category", y="Value", data=df)
for i, bar in enumerate(ax.patches):

    # Adjust hatch size, density, and angle
    hatch = ['/', '|', '+', 'x']
    bar.set_hatch(3 * hatch[i % len(hatch)])  # Increasing density
    bar.set_edgecolor('black')  # Enhancing visibility
    bar.set_linewidth(1.5)  # Adjusting line width for clarity
plt.show()

Output:

Adjust Texture Properties

The set_linewidth and set_edgecolor methods are used to enhance the visibility of the textures, making them more pronounced and easier to differentiate.

 

Combine Multiple Hatch Patterns

Continue using the same sample dataset, but this time, let’s add a twist by combining hatch patterns for each bar.

data = {
    'Category': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
    'Value': [23, 45, 56, 78, 33, 44, 55, 66]
}
df = pd.DataFrame(data)
sns.set(style="whitegrid")
ax = sns.barplot(x='Category', y='Value', data=df)
hatch_patterns = ['/', '\\', '|', '-', '+', 'x', 'o', 'O']
combined_hatches = [h1 + h2 for h1 in hatch_patterns for h2 in hatch_patterns]
for bar, pattern in zip(ax.patches, combined_hatches):
    bar.set_hatch(pattern)
plt.show()

Output:

Combine Multiple Hatch Patterns

 

Animating Textures

For animation, we need to use Matplotlib’s animation module. Also, we’ll use interactive widgets for hover effects.

First, let’s import the necessary libraries:

import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.widgets import Cursor  # For hover effects

Let’s create a simple animation where the hatch patterns of the bars change over time.

fig, ax = plt.subplots()
sns.barplot(x="Category", y="Value", data=df, ax=ax)
hatch_patterns = ['/', '\\', '|', '-', '+', 'x']
def animate(i):
    for bar, pattern in zip(ax.patches, hatch_patterns):
        bar.set_hatch(pattern * (i % len(hatch_patterns)))
ani = animation.FuncAnimation(fig, animate, frames=10, interval=500, repeat=True)
plt.show()

Output:

Animating Textures

Leave a Reply

Your email address will not be published. Required fields are marked *