Bar plots are a versatile and commonly used method of data visualization that can display categorical data with rectangular bars. This tutorial will guide you through creating and customizing various types of bar plots using the `matplotlib` library, including grouped, stacked, and horizontal bar plots. ### Basic Bar Plot Let's start with a basic bar plot to understand the fundamentals.
import matplotlib.pyplot as plt # Sample data categories = ['A', 'B', 'C', 'D'] values = [10, 15, 7, 20] plt.bar(categories, values, color='blue') plt.xlabel('Categories') plt.ylabel('Values') plt.title('Basic Bar Plot') plt.show()
- **`plt.bar(categories, values, color='blue')`** creates a bar plot with blue bars representing the values for each category. ### Grouped Bar Plot A grouped bar plot (also known as multiple bar plot or clustered bar plot) displays two or more sets of bars side by side for comparison. Grouped bar plots are useful when comparing multiple categories or groups across different variables. They allow for easy visualization of trends, differences, and patterns between groups, making it clear how each subgroup performs within a given category. This type of plot is especially effective for presenting data comparisons over time, across regions, or between product segments, while maintaining clarity across multiple data series.
import matplotlib.pyplot as plt import numpy as np # Sample data categories = ['A', 'B', 'C', 'D'] values1 = [10, 15, 7, 20] values2 = [12, 18, 6, 15] x = np.arange(len(categories)) width = 0.35 fig, ax = plt.subplots() bars1 = ax.bar(x - width/2, values1, width, label='Group 1') bars2 = ax.bar(x + width/2, values2, width, label='Group 2') ax.set_xlabel('Categories') ax.set_ylabel('Values') ax.set_title('Grouped Bar Plot') ax.set_xticks(x) ax.set_xticklabels(categories) ax.legend() plt.show()
- **`ax.bar(x - width/2, values1, width, label='Group 1')`** and **`ax.bar(x + width/2, values2, width, label='Group 2')`** create two sets of bars positioned side by side for each category. ### Stacked Bar Plot A stacked bar plot layers bars on top of one another for each category to show total values. Stacked bar plots are useful when you want to show the total value across categories while also displaying the composition of subgroups within each category. They help illustrate both the overall size and the contribution of individual parts, making them ideal for comparing cumulative values and understanding the breakdown of components over time, across locations, or for different products. Stacked bar plots are particularly effective for highlighting how individual segments contribute to a whole.
import matplotlib.pyplot as plt # Sample data categories = ['A', 'B', 'C', 'D'] values1 = [10, 15, 7, 20] values2 = [12, 18, 6, 15] fig, ax = plt.subplots() bars1 = ax.bar(categories, values1, label='Group 1') bars2 = ax.bar(categories, values2, bottom=values1, label='Group 2') ax.set_xlabel('Categories') ax.set_ylabel('Values') ax.set_title('Stacked Bar Plot') ax.legend() plt.show()
- **`ax.bar(categories, values2, bottom=values1, label='Group 2')`** stacks the second set of bars on top of the first set for each category. ### Horizontal Bar Plot A horizontal bar plot uses bars extending horizontally to visualize data. Horizontal bar plots are useful when you have long category labels that would be difficult to fit along the x-axis of a vertical bar plot, or when comparing categories with varying lengths of text. They are also ideal when you have many categories to compare, as the horizontal layout can prevent overcrowding. Additionally, horizontal bar plots are often used when the values being compared are not time-based or ordered, making it easier to compare categorical data side by side.
import matplotlib.pyplot as plt # Sample data with car models only categories = [ 'Toyota Camry', 'Honda Accord', 'Tesla Model 3', 'Ford F-150', 'Chevrolet Silverado', 'BMW 3 Series', 'Mercedes-Benz C-Class', 'Jeep Wrangler', 'Subaru Outback', 'Hyundai Sonata' ] values = [45, 38, 50, 80, 70, 30, 28, 35, 22, 25] # Plot horizontal bar plot plt.barh(categories, values, color='purple') plt.xlabel('Sales (in thousands)') plt.ylabel('Car Models') plt.title('Car Model Sales Performance') plt.show()
- **`plt.barh(categories, values, color='green')`** creates a horizontal bar plot with green bars representing the values for each category.