Hướng dẫn python 3d plot

Note

Click here to download the full example code

Demonstration of a basic scatterplot in 3D.

Hướng dẫn python 3d plot

import matplotlib.pyplot as plt
import numpy as np

# Fixing random state for reproducibility
np.random.seed(19680801)


def randrange(n, vmin, vmax):
    """
    Helper function to make an array of random numbers having shape (n, )
    with each number distributed Uniform(vmin, vmax).
    """
    return (vmax - vmin)*np.random.rand(n) + vmin

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

n = 100

# For each set of style and range settings, plot n random points in the box
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
for m, zlow, zhigh in [('o', -50, -25), ('^', -30, -5)]:
    xs = randrange(n, 23, 32)
    ys = randrange(n, 0, 100)
    zs = randrange(n, zlow, zhigh)
    ax.scatter(xs, ys, zs, marker=m)

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

plt.show()

Gallery generated by Sphinx-Gallery

Use the following code it worked for me:

# Create the figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Generate the values
x_vals = X_iso[:, 0:1]
y_vals = X_iso[:, 1:2]
z_vals = X_iso[:, 2:3]

# Plot the values
ax.scatter(x_vals, y_vals, z_vals, c = 'b', marker='o')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')

plt.show()

while X_iso is my 3-D array and for X_vals, Y_vals, Z_vals I copied/used 1 column/axis from that array and assigned to those variables/arrays respectively.


To get a 3D plot, we can use fig.add_subplot(111, projection='3d') method to instantiate the axis. After that, we can use the scatter method to draw different data points on the x, y, and z axes.

Steps

  • Create a new figure, or activate an existing figure.

  • Add an `~.axes.Axes` to the figure as part of a subplot arrangement, where nrows = 1, ncols = 1, index = 1 and projection is ‘3d’.

  • Iterate a list of marks, xs, ys and zs, to make scatter points.

  • Set x, y, and z labels using set_xlabel, y_label, and z_label methods.

  • Use plt.show() method to plot the figure.

Example

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(1000)

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

n = 100

for m, zl, zh in [('o', -50, -25), ('^', -30, -5)]:
   xs = (32 - 23) * np.random.rand(n) + 23
   ys = (100 - 0) * np.random.rand(n)
   zs = (zh - zl) * np.random.rand(n) + zl
   ax.scatter(xs, ys, zs, marker=m)

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

plt.show()

Output

Updated on 19-Sep-2021 07:08:45

  • Related Questions & Answers
  • How to make a scatter plot for clustering in Python?
  • Connecting two points on a 3D scatter plot in Python and Matplotlib
  • How to plot scatter points in a 3D figure with a colorbar in Matplotlib?
  • Plot a 3D surface from {x,y,z}-scatter data in Python Matplotlib
  • How to make a discrete colorbar for a scatter plot in matplotlib?
  • How to turn off transparency in Matplotlib's 3D Scatter plot?
  • Controlling the alpha value on a 3D scatter plot using Python and Matplotlib
  • Plot scatter points on 3d plot without axes and grids in Matplotlib
  • Make 3D plot interactive in Jupyter Notebook (Python & Matplotlib)
  • How can I make a scatter plot colored by density in Matplotlib?
  • Saving a 3D-plot in a PDF 3D with Python
  • How to overplot a line on a scatter plot in Python?
  • Plot scatter points on a 3D projection with varying marker size in Matplotlib
  • How to animate a scatter plot in Matplotlib?
  • How to plot 3D graphs using Python Matplotlib?

Can Matplotlib be used for 3D plotting?

3D plotting in Matplotlib starts by enabling the utility toolkit. We can enable this toolkit by importing the mplot3d library, which comes with your standard Matplotlib installation via pip. Just be sure that your Matplotlib version is over 1.0. Now that our axes are created we can start plotting in 3D.

How do you plot a XYZ plot in Python?

“matplotlib xyz plot” Code Answer.

from mpl_toolkits. mplot3d import Axes3D..

import matplotlib. pyplot as plt..

fig = plt. figure().

ax = fig. add_subplot(111, projection='3d').

How do you plot a 3D graph in Python?

Plot a single point in a 3D space.

Step 1: Import the libraries. import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D. ... .

Step 2: Create figure and axes. fig = plt.figure(figsize=(4,4)) ax = fig.add_subplot(111, projection='3d') ... .

Step 3: Plot the point..