Creating 2D Bar Graphs in Different Planes

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 3D plot with 2D bar graphs projected onto different planes. We will use the Matplotlib library in Python to generate the visualizations. This lab assumes a basic understanding of Python syntax and the Matplotlib library.

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`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) 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`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") 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-48582{{"`Creating 2D Bar Graphs in Different Planes`"}} matplotlib/figures_axes -.-> lab-48582{{"`Creating 2D Bar Graphs in Different Planes`"}} matplotlib/bar_charts -.-> lab-48582{{"`Creating 2D Bar Graphs in Different Planes`"}} python/for_loops -.-> lab-48582{{"`Creating 2D Bar Graphs in Different Planes`"}} python/lists -.-> lab-48582{{"`Creating 2D Bar Graphs in Different Planes`"}} python/tuples -.-> lab-48582{{"`Creating 2D Bar Graphs in Different Planes`"}} python/importing_modules -.-> lab-48582{{"`Creating 2D Bar Graphs in Different Planes`"}} python/standard_libraries -.-> lab-48582{{"`Creating 2D Bar Graphs in Different Planes`"}} python/math_random -.-> lab-48582{{"`Creating 2D Bar Graphs in Different Planes`"}} python/numerical_computing -.-> lab-48582{{"`Creating 2D Bar Graphs in Different Planes`"}} python/data_visualization -.-> lab-48582{{"`Creating 2D Bar Graphs in Different Planes`"}} python/build_in_functions -.-> lab-48582{{"`Creating 2D Bar Graphs in Different Planes`"}} end

Import Libraries

We will begin by importing the necessary libraries for this lab. We will use the NumPy library to generate random data and the Matplotlib library to create the 3D plot.

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)

Create a Figure and Subplot

Next, we will create a figure and subplot for our 3D plot. We will use the add_subplot() method to create a 3D projection.

fig = plt.figure()
ax = fig.add_subplot(projection='3d')

Generate Data for the Bar Graphs

We will now generate the data for the bar graphs. We will create four sets of data, each with 20 values. We will use the NumPy arange() method to create an array of 20 values and the NumPy random.rand() method to generate random values for each set of data.

colors = ['r', 'g', 'b', 'y']
yticks = [3, 2, 1, 0]
for c, k in zip(colors, yticks):
    xs = np.arange(20)
    ys = np.random.rand(20)

Customize the Bar Graphs

We will now customize the bar graphs. We will create an array of colors and use the bar() method to plot the bar graphs. We will set the zdir parameter to 'y' to project the bar graphs onto the y-axis planes. We will also set the alpha parameter to 0.8 to adjust the transparency of the bars.

    cs = [c] * len(xs)
    cs[0] = 'c'
    ax.bar(xs, ys, zs=k, zdir='y', color=cs, alpha=0.8)

Customize the Axes

We will now customize the axes of the 3D plot. We will set the labels for the x, y, and z axes using the set_xlabel(), set_ylabel(), and set_zlabel() methods, respectively. We will also set the ticks on the y-axis using the set_yticks() method.

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_yticks(yticks)

Display the Plot

We will use the show() method to display the 3D plot.

plt.show()

Summary

In this lab, we learned how to create a 3D plot with 2D bar graphs projected onto different planes using the Matplotlib library in Python. We generated random data and customized the bar graphs and axes of the plot. We then displayed the plot using the show() method.

Other Python Tutorials you may like