# How to plot a bar graph in matplotlib

## Understanding Bar Graphs

Before we dive into the code, let's understand what a bar graph is. A bar graph, or bar chart, is a visual representation of data that uses bars to compare different categories of data. Each bar's height or length represents a value, which makes it easy to see which categories have higher or lower values just by looking at the graph.

Think of a bar graph like a city skyline, where each building represents a different category, and the height of the building shows how much or how many of that category there are. Just like you can easily tell which buildings are tallest and which are shortest, you can quickly see which categories in your data are the largest and smallest.

Before plotting a bar graph, you need to set up your environment. This means you'll need Python installed on your computer, as well as the Matplotlib library, which is a collection of functions that make it easy to create a variety of plots and charts with Python.

You can install Matplotlib by running the following command in your terminal or command prompt:

``````pip install matplotlib
``````

## Importing Matplotlib

Once Matplotlib is installed, you'll need to import it into your Python script or notebook. To plot a bar graph, you'll specifically need to import the `pyplot` module from Matplotlib, which is commonly imported as `plt` for short.

``````import matplotlib.pyplot as plt
``````

## Creating a Simple Bar Graph

Let's start with a simple example. Suppose you want to visualize how many apples, bananas, and cherries you have. Here's how you could create a bar graph for this data:

``````# Data
fruits = ['Apples', 'Bananas', 'Cherries']
quantities = [20, 35, 15]

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

# Adding the title and labels
plt.title('Fruit Quantities')
plt.xlabel('Fruit')
plt.ylabel('Quantity')

# Displaying the graph
plt.show()
``````

In this code, `fruits` is a list of categories (our types of fruit), and `quantities` is a list of values that correspond to each category. The `plt.bar()` function is then used to create the bar graph. We also add a title and labels for the x-axis and y-axis to make the graph more informative. Finally, `plt.show()` displays the graph.

Matplotlib allows you to customize your graphs in many ways. For instance, you can change the color of the bars, add patterns, or adjust the width. Let's enhance our simple bar graph with some customizations.

``````# Plotting the bar graph with customizations
plt.bar(fruits, quantities, color='skyblue', edgecolor='black', hatch='/', width=0.5)

# Adding the title and labels with custom font sizes
plt.title('Fruit Quantities', fontsize=16)
plt.xlabel('Fruit', fontsize=14)
plt.ylabel('Quantity', fontsize=14)

# Displaying the graph
plt.show()
``````

Here, we've changed the color of the bars to sky blue, added a black edge to make them stand out, and used a hatch pattern of slashes. We've also made the bars narrower with the `width` parameter and increased the font size of the title and labels.

## Horizontal Bar Graphs

Sometimes, a horizontal bar graph can be more readable, especially if you have long category names. To create a horizontal bar graph, you use the `plt.barh()` function.

``````# Plotting a horizontal bar graph
plt.barh(fruits, quantities, color='lightgreen', edgecolor='gray')

# Adding the title and labels
plt.title('Fruit Quantities')
plt.xlabel('Quantity')
plt.ylabel('Fruit')

# Displaying the graph
plt.show()
``````

Notice that we've swapped the x and y-axis labels because the axes themselves have swapped roles.

In some cases, you might have an estimate of the potential error or uncertainty in your data. You can represent this on a bar graph with error bars, which are the little lines that cap the top of each bar.

``````# Assuming some example errors
errors = [2, 3, 1]

# Plotting the bar graph with error bars
plt.bar(fruits, quantities, yerr=errors, capsize=5)

# Adding the title and labels
plt.title('Fruit Quantities with Errors')
plt.xlabel('Fruit')
plt.ylabel('Quantity')

# Displaying the graph
plt.show()
``````

The `yerr` parameter is used to specify the error, and `capsize` determines the width of the horizontal caps on the error bars.

## Stacked Bar Graphs

Stacked bar graphs allow you to show how different segments make up a total. Imagine you have apples of different colors and want to visualize the total number of apples as well as the number of each color.

``````# Data
colors = ['Red', 'Green', 'Yellow']
red_apples = [12, 0, 0]
green_apples = [0, 15, 0]
yellow_apples = [0, 0, 10]

# Plotting the stacked bar graph
plt.bar(colors, red_apples, label='Red Apples')
plt.bar(colors, green_apples, bottom=red_apples, label='Green Apples')
plt.bar(colors, yellow_apples, bottom=[i+j for i, j in zip(red_apples, green_apples)], label='Yellow Apples')

# Adding the title, labels, and legend
plt.title('Apples by Color')
plt.xlabel('Color')
plt.ylabel('Quantity')
plt.legend()

# Displaying the graph
plt.show()
``````

The `bottom` parameter tells Matplotlib where the bottom of each segment should start. We add the quantities of the previous segments to stack them on top of each other. The `label` parameter is used to create a legend for the graph.

## Grouped Bar Graphs

What if you want to compare categories side-by-side? You can do this with grouped bar graphs. Let's compare two fruit stores:

``````# Data
store1_sales = [20, 35, 15]
store2_sales = [25, 30, 20]
bar_width = 0.35
index = range(len(fruits))

# Plotting the grouped bar graph
plt.bar(index, store1_sales, width=bar_width, label='Store 1')
plt.bar([i + bar_width for i in index], store2_sales, width=bar_width, label='Store 2')

# Adding the title, labels, and legend
plt.title('Fruit Sales Comparison')
plt.xlabel('Fruit')
plt.ylabel('Quantity Sold')
plt.xticks([i + bar_width / 2 for i in index], fruits)
plt.legend()

# Displaying the graph
plt.show()
``````

In this code, we offset the x positions of the second set of bars by the width of a bar so that they appear next to the first set. The `plt.xticks()` function is then used to align the x-axis labels correctly.

## Conclusion

By now, you should have a good grasp of how to create and customize bar graphs using Matplotlib. Remember, bar graphs are like the city skylines of the data world, providing a clear and immediate visual comparison of different categories. Whether you're stacking them high or grouping them side by side, Matplotlib gives you the tools to convey your data's story in a simple yet compelling way.

And just as a city's skyline can be lit up in different colors, your bar graph can be adorned with a variety of customizations to make it informative and appealing. Keep experimenting with different styles and configurations to best suit your data. Happy plotting!

## Learn to code in our 100% online programs

Altcademy coding bootcamp offers beginner-friendly, online programs designed by industry experts to help you become a coder. 85%+ of Altcademy alumni are hired within 6 months after graduation. See how we teach, or click on one of the following programs to find out more.