Create Visually Appealing Bar Charts with Matplotlib

MatplotlibMatplotlibBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will learn how to create bar chart using Python's Matplotlib library. We will focus on how to control the color of the bars and the legend entries. This will help us create a visually appealing chart that is easy to read and understand.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/bar_charts("`Bar Charts`") matplotlib/PlotCustomizationGroup -.-> matplotlib/legend_config("`Legend Configuration`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48571{{"`Create Visually Appealing Bar Charts with Matplotlib`"}} matplotlib/figures_axes -.-> lab-48571{{"`Create Visually Appealing Bar Charts with Matplotlib`"}} matplotlib/bar_charts -.-> lab-48571{{"`Create Visually Appealing Bar Charts with Matplotlib`"}} matplotlib/legend_config -.-> lab-48571{{"`Create Visually Appealing Bar Charts with Matplotlib`"}} python/lists -.-> lab-48571{{"`Create Visually Appealing Bar Charts with Matplotlib`"}} python/tuples -.-> lab-48571{{"`Create Visually Appealing Bar Charts with Matplotlib`"}} python/importing_modules -.-> lab-48571{{"`Create Visually Appealing Bar Charts with Matplotlib`"}} python/data_visualization -.-> lab-48571{{"`Create Visually Appealing Bar Charts with Matplotlib`"}} end

Import Matplotlib Library

First, we need to import the Matplotlib library. This can be done using the following code:

import matplotlib.pyplot as plt

Define Data for the Chart

Next, we need to define the data that we want to use to create the chart. In this example, we will be creating a chart that shows the supply of different types of fruits. We will define the fruit names, the supply counts, the bar colors, and the legend labels as follows:

fruits = ['apple', 'blueberry', 'cherry', 'orange']
counts = [40, 100, 30, 55]
bar_labels = ['red', 'blue', '_red', 'orange']
bar_colors = ['tab:red', 'tab:blue', 'tab:red', 'tab:orange']

Create the Bar Chart

Now, we can create the bar chart using the data that we defined in Step 2. We will use the bar() method of Matplotlib's pyplot module to create the chart. We will also pass in the label and color parameters to control the legend entries and the bar colors, respectively. The following code demonstrates how to create the bar chart:

fig, ax = plt.subplots()
ax.bar(fruits, counts, label=bar_labels, color=bar_colors)
ax.set_ylabel('fruit supply')
ax.set_title('Fruit supply by kind and color')
ax.legend(title='Fruit color')
plt.show()

Customize the Chart

We can customize the chart further by adding axis labels and a title. We can also change the color of the axis labels and the legend title. The following code demonstrates how to customize the chart:

fig, ax = plt.subplots()
ax.bar(fruits, counts, label=bar_labels, color=bar_colors)
ax.set_ylabel('fruit supply', color='blue')
ax.set_xlabel('fruit names', color='blue')
ax.set_title('Fruit supply by kind and color', color='purple')
ax.legend(title='Fruit color', title_color='red', labelcolor='green')
plt.show()

Summary

In this lab, we learned how to create bar chart using Python's Matplotlib library. We focused on how to control the color of the bars and the legend entries. We also learned how to customize the chart by adding axis labels and a title. By following these steps, we can create visually appealing charts that are easy to read and understand.

Other Matplotlib Tutorials you may like