Dumbbell charts help compare two related values side by side for each item. They make it easy to see differences and changes.
0
0
Dumbbell charts in Matplotlib
Introduction
Comparing sales numbers for two years for different products.
Showing before and after results of a test for multiple students.
Visualizing changes in population between two time points for cities.
Comparing two measurements like temperature in morning and evening for several days.
Syntax
Matplotlib
import matplotlib.pyplot as plt # Data categories = ['A', 'B', 'C'] value1 = [10, 20, 15] value2 = [15, 18, 20] # Plot plt.hlines(y=categories, xmin=value1, xmax=value2, color='gray') plt.scatter(value1, categories, color='blue', label='Value 1') plt.scatter(value2, categories, color='red', label='Value 2') plt.legend() plt.show()
Use hlines to draw horizontal lines between two values.
Use scatter to plot points at each value.
Examples
Simple dumbbell chart with two categories and two values each.
Matplotlib
import matplotlib.pyplot as plt categories = ['X', 'Y'] val1 = [5, 7] val2 = [8, 6] plt.hlines(y=categories, xmin=val1, xmax=val2, color='black') plt.scatter(val1, categories, color='green') plt.scatter(val2, categories, color='orange') plt.show()
Dumbbell chart with bigger points and a title.
Matplotlib
import matplotlib.pyplot as plt categories = ['Item1', 'Item2', 'Item3'] val1 = [12, 9, 14] val2 = [15, 11, 13] plt.hlines(categories, val1, val2, color='purple', linewidth=2) plt.scatter(val1, categories, color='purple', s=100) plt.scatter(val2, categories, color='orange', s=100) plt.title('Dumbbell Chart Example') plt.show()
Sample Program
This program creates a dumbbell chart comparing sales for 2023 and 2024 for four products. Lines connect the two sales values for each product, and points show exact sales.
Matplotlib
import matplotlib.pyplot as plt # Categories and two sets of values categories = ['Product A', 'Product B', 'Product C', 'Product D'] sales_2023 = [200, 340, 300, 280] sales_2024 = [220, 310, 330, 290] # Draw lines between sales of 2023 and 2024 plt.hlines(y=categories, xmin=sales_2023, xmax=sales_2024, color='gray', linewidth=2) # Plot sales points for each year plt.scatter(sales_2023, categories, color='blue', label='2023 Sales', s=100) plt.scatter(sales_2024, categories, color='red', label='2024 Sales', s=100) # Add labels and legend plt.xlabel('Sales') plt.title('Sales Comparison Dumbbell Chart') plt.legend() plt.show()
OutputSuccess
Important Notes
Make sure your two value lists have the same length as the categories list.
You can customize colors and sizes of points to improve clarity.
Dumbbell charts work best when comparing exactly two values per category.
Summary
Dumbbell charts show two values side by side for easy comparison.
Use horizontal lines and scatter points to build the chart in matplotlib.
They help visualize changes or differences clearly.