One of the most common tasks in data analysis is comparing quantities across different groups or categories. For instance, you might want to compare the average monthly rainfall in different cities, the sales figures for various product lines, or the number of users engaging with different features of an application. Bar charts are an excellent tool for precisely this type of comparison. They use rectangular bars whose lengths are proportional to the values they represent, making visual comparisons straightforward.
Matplotlib provides the plt.bar()
function to create vertical bar charts. Its basic usage requires two primary inputs: the positions (or labels) for the bars on one axis (typically the x-axis for vertical bars) and the heights of the bars representing the values on the other axis (typically the y-axis).
Let's start with a basic example. Suppose we have data on the number of different types of fruits in a basket:
import matplotlib.pyplot as plt
import numpy as np # Often useful, though not strictly needed for this simple example
# Data
fruit_types = ['Apples', 'Oranges', 'Bananas', 'Grapes']
counts = [15, 12, 20, 8]
# Create the figure and axes objects
fig, ax = plt.subplots()
# Create the bar chart
ax.bar(fruit_types, counts)
# Add labels and title for clarity
ax.set_xlabel('Fruit Type')
ax.set_ylabel('Quantity')
ax.set_title('Number of Fruits in the Basket')
# Display the plot
plt.show()
This script first defines our categories (fruit_types
) and their corresponding numerical values (counts
). We then create a figure and an axes object using plt.subplots()
. The core of the visualization is ax.bar(fruit_types, counts)
, which instructs Matplotlib to draw bars. The first argument provides the labels for the x-axis ticks, and the second argument provides the height for each corresponding bar. Finally, we add labels and a title, similar to how we customized line and scatter plots in the previous chapter, and display the plot.
The resulting chart clearly shows that Bananas are the most numerous fruit, while Grapes are the least numerous. The visual height difference makes this comparison immediate.
A simple bar chart comparing the quantity of different fruit types.
Like other Matplotlib plots, bar charts can be customized. A common customization is changing the color of the bars using the color
argument within the bar()
function.
import matplotlib.pyplot as plt
# Data
fruit_types = ['Apples', 'Oranges', 'Bananas', 'Grapes']
counts = [15, 12, 20, 8]
# Create the figure and axes objects
fig, ax = plt.subplots()
# Create the bar chart with a different color
ax.bar(fruit_types, counts, color='#74c0fc') # Using a light blue color
# Add labels and title
ax.set_xlabel('Fruit Type')
ax.set_ylabel('Quantity')
ax.set_title('Number of Fruits in the Basket (Custom Color)')
# Display the plot
plt.show()
This modification simply changes the visual appearance without altering the data representation. You can specify colors using names (like 'red', 'green'), hexadecimal codes (like '#74c0fc'), or other formats supported by Matplotlib.
Bar charts are fundamental for visualizing categorical comparisons. By representing values as bar lengths, they provide an intuitive way to see differences and similarities between groups. The plt.bar()
function offers a straightforward way to generate these essential visualizations. In the next section, we will look at creating horizontal bar charts and discuss when they might be preferred.
© 2025 ApX Machine Learning