3-D plots

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

How can we make 3-D Graphs using Plotly

Install Matplotlib:
If you haven't already installed Matplotlib, you can do so using pip →
pip install matplotlib
Plotting 3D Graph:
Here's a simple example of how to plot a 3D graph using
Matplotlib. This example assumes you have three lists of readings
representing the x, y, and z coordinates.

import matplotlib.pyplot as plt


from mpl_toolkits.mplot3d import Axes3D

# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 3, 4, 5, 6]
z = [5, 6, 7, 8, 9]

# Create a new figure


fig = plt.figure()

# Add a 3D subplot
ax = fig.add_subplot(111, projection='3d')

# Plot the data


ax.scatter(x, y, z, c='r', marker='o')

# Set labels
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

# Show the plot


plt.show()

• We import matplotlib.pyplot and Axes3D from


mpl_toolkits.mplot3d.
• We create sample data for x, y, and z.
• We create a new figure and add a 3D subplot.
• We use the scatter method to create a 3D scatter plot.
• We set labels for the x, y, and z axes.
• We display the plot using plt.show().

Example Using Plotly


Plotly is another powerful library for creating interactive plots.

Install Plotly:
If you haven't already installed Plotly, you can do so using pip: → pip
install plotly
import plotly.graph_objects as go
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 3, 4, 5, 6]
z = [5, 6, 7, 8, 9]

# Create a 3D scatter plot


fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z, mode='markers',
marker=dict(size=5, color='blue'))])

# Set labels
fig.update_layout(scene=dict(
xaxis_title='X Label',
yaxis_title='Y Label',
zaxis_title='Z Label'),
title='3D Scatter Plot')

# Show the plot


fig.show()

1. We import plotly.graph_objects.
2. We create sample data for x, y, and z.
3. We create a 3D scatter plot using go.Scatter3d.
4. We set labels for the x, y, and z axes and add a title.
5. We display the plot using fig.show().
6. Both Matplotlib and Plotly offer powerful tools for creating 3D
plots, and you can choose the one that best fits your needs

You might also like