Logarithmic Bar Chart

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, we will learn how to create a logarithmic bar chart using Python Matplotlib library. A logarithmic bar chart is useful when the values of the dataset are very different in size, and we want to visualize them in a more balanced way.

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/AdvancedPlottingGroup(["`Advanced Plotting`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/bar_charts("`Bar Charts`") matplotlib/AdvancedPlottingGroup -.-> matplotlib/log_scale("`Logarithmic Scale`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/list_comprehensions("`List Comprehensions`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48810{{"`Logarithmic Bar Chart`"}} matplotlib/figures_axes -.-> lab-48810{{"`Logarithmic Bar Chart`"}} matplotlib/bar_charts -.-> lab-48810{{"`Logarithmic Bar Chart`"}} matplotlib/log_scale -.-> lab-48810{{"`Logarithmic Bar Chart`"}} python/variables_data_types -.-> lab-48810{{"`Logarithmic Bar Chart`"}} python/strings -.-> lab-48810{{"`Logarithmic Bar Chart`"}} python/for_loops -.-> lab-48810{{"`Logarithmic Bar Chart`"}} python/list_comprehensions -.-> lab-48810{{"`Logarithmic Bar Chart`"}} python/lists -.-> lab-48810{{"`Logarithmic Bar Chart`"}} python/tuples -.-> lab-48810{{"`Logarithmic Bar Chart`"}} python/importing_modules -.-> lab-48810{{"`Logarithmic Bar Chart`"}} python/numerical_computing -.-> lab-48810{{"`Logarithmic Bar Chart`"}} python/data_visualization -.-> lab-48810{{"`Logarithmic Bar Chart`"}} python/build_in_functions -.-> lab-48810{{"`Logarithmic Bar Chart`"}} end

Importing Libraries

First, we need to import the necessary libraries. In this case, we will be using the matplotlib.pyplot and numpy libraries. The pyplot library will allow us to create our bar chart, and the numpy library will help us to manipulate the data.

import matplotlib.pyplot as plt
import numpy as np

Defining the Data

Next, we need to define the data that we want to use for our bar chart. In this example, we will be using a tuple of tuples, where each inner tuple contains two values. The first value represents the x-axis value, and the second value represents the y-axis value.

data = ((3, 1000), (10, 3), (100, 30), (500, 800), (50, 1))

Creating the Bar Chart

Now we are ready to create our bar chart. We will start by defining some variables that will help us to set the width of the bars and their position on the x-axis.

dim = len(data[0])
w = 0.75
dimw = w / dim

Next, we will create a figure and an axis object using the subplots() method. Then, we will use a for loop to iterate through each value in our dataset and create a bar for each one.

fig, ax = plt.subplots()
x = np.arange(len(data))
for i in range(len(data[0])):
    y = [d[i] for d in data]
    b = ax.bar(x + i * dimw, y, dimw, bottom=0.001)

We set the bottom parameter to 0.001 to avoid having any bars with a height of 0, which is not compatible with a logarithmic scale.

Customizing the Chart

We can customize the appearance of our chart by adding labels to the x-axis and y-axis, and by setting the scale of the y-axis to logarithmic.

ax.set_xticks(x + dimw / 2, labels=map(str, x))
ax.set_yscale('log')

ax.set_xlabel('x')
ax.set_ylabel('y')

Displaying the Chart

Finally, we can display our chart using the show() method.

plt.show()

Summary

In this lab, we learned how to create a logarithmic bar chart using Python Matplotlib library. We started by importing the necessary libraries, defining the data, creating the bar chart, customizing it, and displaying it. A logarithmic bar chart is a useful way to visualize data that has a wide range of values, and it can help us to see the differences between them more clearly.

Other Python Tutorials you may like