Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to make a bar graph in matplotlib

Understanding Bar Graphs

Before diving into the code, let's grasp what a bar graph is. Imagine you're a teacher with a bunch of students, and you want to visually represent how many students got A, B, C, or D grades. A bar graph would be perfect for this task. It uses rectangular bars, where the length of each bar is proportional to the value it represents. If ten students got A's, the 'A' bar would be longer than the 'B' bar if only five students got B's.

Bar graphs are useful because they make comparisons easy. You can quickly see which category is the largest or smallest and get a general sense of the distribution at a glance.

Setting Up Your Environment

To create a bar graph, you'll need to have Python installed on your computer, along with the Matplotlib library. Matplotlib is a plotting library for Python which provides a variety of graphs and plots to visualize data. You can install it using pip, which is the package installer for Python. Here's the command you'll need to type into your terminal:

pip install matplotlib

Starting with Matplotlib

Once Matplotlib is installed, you can start using it in your Python scripts. The first step is to import it. In Matplotlib, there's a sub-module called pyplot that provides a MATLAB-like interface, which is particularly suited for interactive plots and simple cases of programmatic plot generation:

import matplotlib.pyplot as plt

Creating Your First Bar Graph

Let's create a simple bar graph. We'll represent the number of fruits in a basket. Suppose you have 5 apples, 3 bananas, and 8 cherries. Here's how you can create a bar graph for this data:

# Import the pyplot module
import matplotlib.pyplot as plt

# Data for our bar graph
fruits = ['Apples', 'Bananas', 'Cherries']
quantities = [5, 3, 8]

# Create the bar graph
plt.bar(fruits, quantities)

# Show the bar graph
plt.show()

When you run this code, a window will pop up displaying your bar graph with three bars of different lengths according to the quantities of fruits.

Customizing Your Bar Graph

The graph we just made is quite basic. It's like a plain cake without frosting or decorations. Let's add some details to make it more informative and appealing.

Adding Titles and Labels

A good graph should communicate clearly. Adding a title, and labels for the x-axis and y-axis can help with that:

plt.bar(fruits, quantities)

# Add a title
plt.title('Quantity of Fruits in Basket')

# Add labels to the axes
plt.xlabel('Type of Fruit')
plt.ylabel('Quantity')

plt.show()

Changing Bar Colors and Patterns

To make our graph more colorful, we can change the color of the bars. We can also add patterns (like lines or dots) to the bars to make them distinct, especially if we're printing in black and white:

plt.bar(fruits, quantities, color=['red', 'yellow', 'purple'], hatch='//')

plt.title('Quantity of Fruits in Basket')
plt.xlabel('Type of Fruit')
plt.ylabel('Quantity')

plt.show()

Adjusting Bar Width and Alignment

Sometimes you might want to adjust the width of the bars to make your graph look better. You can also align the bars differently relative to the x-axis labels:

plt.bar(fruits, quantities, width=0.5, align='edge', color='green')

plt.title('Quantity of Fruits in Basket')
plt.xlabel('Type of Fruit')
plt.ylabel('Quantity')

plt.show()

Plotting Multiple Sets of Data

What if you want to compare two sets of data? For example, this year's fruit quantities versus last year's. You can plot two sets of bars side by side:

# Data for this year
quantities_this_year = [5, 3, 8]
# Data for last year
quantities_last_year = [7, 2, 5]

# The x position of bars
x = range(len(fruits))

# Plotting both sets of data
plt.bar(x, quantities_this_year, width=0.4, label='This Year', align='center')
plt.bar(x, quantities_last_year, width=0.4, label='Last Year', align='edge')

plt.title('Quantity of Fruits in Basket')
plt.xlabel('Type of Fruit')
plt.ylabel('Quantity')

# Adding the legend, which uses the labels we set in the plt.bar() function
plt.legend()

plt.show()

Adding Error Bars

In real-life data, there's often some uncertainty. Error bars are a way to visually represent the variability of the data. Let's say you're not sure about the exact quantity of each fruit, and you want to reflect that uncertainty:

# Define the error margin
errors = [0.5, 0.2, 0.4]

plt.bar(fruits, quantities, yerr=errors, capsize=5)

plt.title('Quantity of Fruits in Basket')
plt.xlabel('Type of Fruit')
plt.ylabel('Quantity')

plt.show()

Saving Your Graph

Instead of just showing the graph, you might want to save it to a file. You can do this with plt.savefig() before plt.show():

plt.bar(fruits, quantities)

plt.title('Quantity of Fruits in Basket')
plt.xlabel('Type of Fruit')
plt.ylabel('Quantity')

# Save the figure to a file
plt.savefig('fruit_basket.png')

plt.show()

Conclusion

Creating bar graphs with Matplotlib is like building with Lego blocks. You start with the basic structure and then keep adding pieces until it looks the way you want. We've covered a lot, from crafting a simple bar graph to customizing it with colors, labels, and error bars. Remember that the key to a great graph is clarity—it should tell a story at a glance.

As you continue your journey in programming and data visualization, think of each graph as a piece of art. The data is your paint, and Matplotlib is your brush. With practice, you'll be able to create visual masterpieces that can communicate complex information in an intuitive and engaging way. Happy plotting!