3D Stem Plot

PythonPythonBeginner
Practice Now

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

Introduction

This lab demonstrates how to create a 3D stem plot using the Matplotlib library in Python. A stem plot is a way of plotting data points by drawing vertical lines from a baseline to the data point and placing a marker at the tip.

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`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) 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`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") 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-48959{{"`3D Stem Plot`"}} matplotlib/figures_axes -.-> lab-48959{{"`3D Stem Plot`"}} python/variables_data_types -.-> lab-48959{{"`3D Stem Plot`"}} python/tuples -.-> lab-48959{{"`3D Stem Plot`"}} python/importing_modules -.-> lab-48959{{"`3D Stem Plot`"}} python/data_collections -.-> lab-48959{{"`3D Stem Plot`"}} python/numerical_computing -.-> lab-48959{{"`3D Stem Plot`"}} python/data_visualization -.-> lab-48959{{"`3D Stem Plot`"}} python/build_in_functions -.-> lab-48959{{"`3D Stem Plot`"}} end

Import the necessary libraries

In this step, we will import the Matplotlib and Numpy libraries using the import statement.

import matplotlib.pyplot as plt
import numpy as np

Define the data

In this step, we will define the data that we will use to create the 3D stem plot. We will create a linspace array for the angle, and use sine and cosine functions to calculate the x and y coordinates. We will also define the z coordinate as the angle.

theta = np.linspace(0, 2*np.pi)
x = np.cos(theta - np.pi/2)
y = np.sin(theta - np.pi/2)
z = theta

Create the 3D stem plot

In this step, we will create the 3D stem plot using the stem function from Matplotlib. We will pass the x, y, and z coordinates as arguments to the stem function.

fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
ax.stem(x, y, z)

plt.show()

Customize the plot

In this step, we will customize the 3D stem plot by changing the baseline using the bottom parameter and changing the format using the linefmt, markerfmt, and basefmt parameters.

fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
markerline, stemlines, baseline = ax.stem(
    x, y, z, linefmt='grey', markerfmt='D', bottom=np.pi)
markerline.set_markerfacecolor('none')

plt.show()

Change the orientation of the plot

In this step, we will change the orientation of the plot using the orientation parameter. We will set the orientation to 'x' so that the stems are projected along the x-direction and the baseline is in the yz-plane.

fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
markerline, stemlines, baseline = ax.stem(x, y, z, bottom=-1, orientation='x')
ax.set(xlabel='x', ylabel='y', zlabel='z')

plt.show()

Summary

In this lab, we learned how to create a 3D stem plot using the Matplotlib library in Python. We started by defining the data, then created the plot using the stem function. We also customized the plot by changing the format and orientation.

Other Python Tutorials you may like