A clustered bar plot (also known as a grouped bar plot) is a type of bar chart that displays multiple sets of data side-by-side for each category. It is useful for comparing different groups or sub-categories within the same main category.
Each cluster (group of bars) represents a category, and within each cluster, separate bars represent different sub-categories or data series. This allows you to visually compare values across both categories and sub-categories at once.
You can create a clustered bar plot in Python using matplotlib.pyplot.bar()
, by adjusting the bar positions manually for each group.
import matplotlib.pyplot as plt
import numpy as np
# Categories and data for two groups
categories = ['A', 'B', 'C', 'D', 'E']
group1 = [10, 20, 15, 25, 18]
group2 = [12, 18, 22, 20, 25]
# Define the position of the bars on the x-axis
x = np.arange(len(categories)) # the label locations
bar_width = 0.35 # width of the bars
# Create the bar plot
plt.bar(x - bar_width/2, group1, width=bar_width, label='Group 1')
plt.bar(x + bar_width/2, group2, width=bar_width, label='Group 2')
# Add labels and title
plt.xlabel('Category')
plt.ylabel('Value')
plt.title('Clustered Bar Plot')
plt.xticks(x, categories) # Add category labels to the x-axis
plt.legend()
plt.show()
Output: