0
0
Matplotlibdata~5 mins

Spine charts concept in Matplotlib

Choose your learning style9 modes available
Introduction

Spine charts help compare two related data sets side by side with a clear connection between them. They make it easy to see differences and trends.

Comparing sales numbers of two products across months.
Showing before and after results of a test or experiment.
Visualizing changes in survey responses over time.
Comparing two groups' performance on the same tasks.
Syntax
Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_position(('outward', 10))
ax.spines['bottom'].set_position(('outward', 10))

plt.show()

Spines are the lines around the plot area (top, bottom, left, right).

You can move or hide spines to create a cleaner or customized look.

Examples
This hides the top and right spines for a cleaner look.
Matplotlib
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
This moves the left spine outward by 10 points for better spacing.
Matplotlib
ax.spines['left'].set_position(('outward', 10))
This moves the bottom spine to the zero position on the y-axis.
Matplotlib
ax.spines['bottom'].set_position(('zero'))
Sample Program

This code creates a spine chart comparing two groups side by side. One group's bars go left, the other's go right. The spines are adjusted for clarity.

Matplotlib
import matplotlib.pyplot as plt

# Sample data for two groups
categories = ['A', 'B', 'C', 'D']
values1 = [5, 7, 3, 4]
values2 = [6, 6, 4, 5]

fig, ax = plt.subplots(figsize=(6,4))

# Plot values1 on left side
ax.barh(categories, values1, color='skyblue', label='Group 1')

# Plot values2 on right side with negative values to mirror
ax.barh(categories, [-v for v in values2], color='salmon', label='Group 2')

# Customize spines to create spine chart effect
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_position(('outward', 10))
ax.spines['bottom'].set_position(('zero'))

# Set x-axis limits to show both sides
ax.set_xlim(-max(values2)-1, max(values1)+1)

# Add legend and labels
ax.set_xlabel('Values')
ax.set_title('Spine Chart Example')
ax.legend(loc='upper right')

plt.show()
OutputSuccess
Important Notes

Spine charts often use mirrored bars to compare two groups clearly.

Adjusting spines helps focus attention on the data, not the chart frame.

Use colors and labels to make the chart easy to understand.

Summary

Spine charts compare two related data sets side by side.

Spines are the plot borders you can move or hide for style.

Mirrored bars and spine adjustments make comparisons clear.