Bar charts are excellent tools for comparing the size or quantity of different items or categories. Imagine you want to compare the monthly sales figures for different products, the population of several cities, or the number of votes received by different candidates. In such scenarios, bar charts provide a clear visual representation of these comparisons. Matplotlib offers straightforward functions to create both vertical and horizontal bar charts.
plt.bar()
The most common type is the vertical bar chart, where the height of each bar represents the magnitude of the value for a specific category. We use the plt.bar()
function for this purpose.
The plt.bar()
function primarily requires two arguments:
Let's create a simple vertical bar chart showing the number of different fruits sold by a vendor in a day.
import matplotlib.pyplot as plt
import numpy as np
# Data: Categories and their corresponding values
fruits = ['Apples', 'Oranges', 'Bananas', 'Grapes']
quantity_sold = [45, 30, 60, 25]
# Create the figure and axes objects
fig, ax = plt.subplots()
# Create the vertical bar chart
ax.bar(fruits, quantity_sold, color='#339af0') # Using a blue color
# Add labels and title for clarity
ax.set_xlabel('Fruit Type')
ax.set_ylabel('Quantity Sold')
ax.set_title('Daily Fruit Sales')
# Display the plot
plt.show()
Quantity of different fruits sold, represented by the height of the blue bars.
In this example, the fruits
list provides the categorical labels for the x-axis, and quantity_sold
provides the numerical heights for the bars on the y-axis. We added labels and a title using methods on the ax
object, which we obtained from plt.subplots()
, consistent with the anatomy discussed previously. The color
argument allows us to specify the bar color; here we chose a shade of blue.
plt.barh()
Sometimes, especially when you have many categories or long category labels, a horizontal bar chart can be more readable. Matplotlib provides the plt.barh()
function (bar horizontal) for this.
The plt.barh()
function works similarly to plt.bar()
, but the roles of the axes are swapped:
Let's recreate the fruit sales chart horizontally.
import matplotlib.pyplot as plt
import numpy as np
# Data: Categories and their corresponding values
fruits = ['Apples', 'Oranges', 'Bananas', 'Grapes']
quantity_sold = [45, 30, 60, 25]
# Create the figure and axes objects
fig, ax = plt.subplots()
# Create the horizontal bar chart
ax.barh(fruits, quantity_sold, color='#51cf66') # Using a green color
# Add labels and title
# Note: The axes labels are swapped compared to the vertical chart
ax.set_xlabel('Quantity Sold')
ax.set_ylabel('Fruit Type')
ax.set_title('Daily Fruit Sales (Horizontal)')
# Display the plot
plt.show()
Quantity of different fruits sold, represented by the length of the green bars along the horizontal axis. Categories are listed vertically.
Notice how the inputs to barh
are the same lists (fruits
, quantity_sold
), but their interpretation changes. fruits
now define the positions on the y-axis, and quantity_sold
defines the length of the bars extending along the x-axis. Consequently, the axis labels (set_xlabel
, set_ylabel
) are also swapped compared to the vertical version. Horizontal bar charts are often useful when category names are long, preventing overlap issues that can occur on the x-axis of a vertical chart.
Both plt.bar()
and plt.barh()
are fundamental tools for comparing distinct quantities. You can customize their appearance further by changing colors, adding error bars (representing uncertainty), or adjusting bar widths, aspects we will touch upon later. For now, focus on understanding when to use each type and how to provide the basic categorical and numerical data to generate them.
© 2025 ApX Machine Learning