5 Best Ways to Create Multiple Bars with Python Matplotlib

πŸ’‘ Problem Formulation: When working with data visualization in Python, it’s not uncommon to need to compare different datasets side-by-side using bar charts. A specific scenario might involve comparing monthly sales figures for several products across the same period. The desired output is a clear and easy-to-read bar chart that can display these datasets simultaneously for effective comparison.

Method 1: Use the bar() function with position adjustments

Matplotlib’s bar() function can be utilized to plot multiple bar charts by adjusting the bar positions. To avoid bars overlapping each other, their x-coordinates need to be modified. We can achieve this by defining a bar width and then adding or subtracting this width from the x-coordinates of the bars for each new dataset.

Here’s an example:

import matplotlib.pyplot as plt
import numpy as np

labels = ['January', 'February', 'March', 'April']
a_values = [10, 15, 17, 14]
b_values = [12, 11, 15, 13]

x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, a_values, width, label='A')
rects2 = ax.bar(x + width/2, b_values, width, label='B')

ax.set_ylabel('Sales')
ax.set_title('Monthly Sales by Product')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()

plt.show()

This code creates a bar chart with two bars for each month. The bars for each product A and B are placed side by side for direct comparison.

Method 2: Stacked Bars with the bar() function

Another way to visualize multiple datasets in a bar chart is to stack them on top of each other. This method is suitable when you want to show the total value made up of different components. The initial dataset provides the bottom position for the next set of bars using the `bottom` parameter in the bar() function.

Here’s an example:

fig, ax = plt.subplots()

rects1 = ax.bar(x, a_values, width, label='A')
rects2 = ax.bar(x, b_values, width, bottom=a_values, label='B')

ax.set_ylabel('Cumulative Sales')
ax.set_title('Cumulative Monthly Sales by Product')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()

plt.show()

The output is a chart with each month represented by a stacked bar, showing how product B’s sales stack on top of product A’s.

Method 3: Grouped Bar Charts by Iterating Over a Dataset

In more complex scenarios, where we have multiple groups and subcategories, we might need to iterate over a dataset and plot bars dynamically. This method is highly adaptable and can be utilized to plot arbitrarily large datasets by modifying the bar positions within a loop.

Here’s an example:

data = {'January': [10, 12], 'February': [15, 11], 'March': [17, 15], 'April': [14, 13]}
categories = ['A', 'B']
colors = ['red', 'blue']

fig, ax = plt.subplots()
bar_width = 0.35
for i, (month, values) in enumerate(data.items()):
    for j, (category, color) in enumerate(zip(categories, colors)):
        ax.bar(i + j * bar_width, values[j], bar_width, label=category if i == 0 else "", color=color)

ax.set_ylabel('Sales')
ax.set_title('Monthly Sales by Category')
ax.set_xticks(np.arange(len(data)) + bar_width / 2)
ax.set_xticklabels(data.keys())
ax.legend()

plt.show()

This script will iterate through the data and plot individual bars for each category within each month. It leverages the loop index to dynamically calculate bar positions.

Method 4: Horizontal Bars Using barh()

Sometimes data is better represented horizontally, especially when dealing with long category names or when comparing a large number of categories. In such a case, horizontal bars can be created with the barh() function, using the same concept of position adjustments that are used with vertical bars.

Here’s an example:

fig, ax = plt.subplots()

rects1 = ax.barh(x - width/2, a_values, width, label='A')
rects2 = ax.barh(x + width/2, b_values, width, label='B')

ax.set_xlabel('Sales')
ax.set_title('Monthly Sales by Product')
ax.set_yticks(x)
ax.set_yticklabels(labels)
ax.legend()

plt.show()

This code snippet generates a horizontal bar chart with separate bars for each product placed side-by-side in each month.

Bonus One-Liner Method 5: Pandas Plot

Pandas can be used in conjunction with Matplotlib to create bar charts with a single line of code. This method is highly efficient when working with Pandas DataFrames. Utilizing the DataFrame.plot() method and the kind='bar' argument, you can swiftly create multiple bar charts.

Here’s an example:

import pandas as pd

df = pd.DataFrame(np.c_[a_values, b_values], index=labels, columns=['A', 'B'])
df.plot(kind='bar', rot=0)

plt.ylabel('Sales')
plt.title('Monthly Sales by Product')
plt.show()

This one-liner generates a grouped bar chart directly from a DataFrame without having to manually handle the axes and bar positions.

Summary/Discussion

  • Method 1: Traditional Bar Positioning. Offers precision and customizability. Requires manual calculation of positions, which can be cumbersome for large datasets.
  • Method 2: Stacked Bars. Ideal for displaying parts of a whole. Not suitable for direct comparison of individual components.
  • Method 3: Dynamic Iteration Over Datasets. Highly flexible for complex datasets. Requires more sophisticated coding and logic setup.
  • Method 4: Horizontal Bars. Offers better readability for certain types of data. Lacks the vertical aspect which is sometimes more impactful for showing contrasts in data.
  • Bonus Method 5: Pandas Plot. Quick and efficient for DataFrame objects. Limited customization compared to native Matplotlib approaches.