Cara menggunakan visualize 3d array python


To create a 3D plot from a 3D numpy array, we can create a 3D array using numpy and extract the x, y, and z points.

  • Create a new figure or activate an existing figure using figure() method.
  • Add an '~.axes.Axes' to the figure as part of a subplot arrangement using add_subplot() method.
  • Create a random data of size=(3, 3, 3).
  • Extract x, y, and z data from the 3D array.
  • Plot 3D scattered points on the created axis
  • To display the figure, use show() method.

Example

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
data = np.random.random(size=(3, 3, 3))
z, x, y = data.nonzero()
ax.scatter(x, y, z, c=z, alpha=1)
plt.show()

Output

Cara menggunakan visualize 3d array python

Cara menggunakan visualize 3d array python

Updated on 15-May-2021 12:02:05

  • Related Questions & Answers
  • How to surface plot/3D plot from a dataframe (Matplotlib)?
  • Creating 3D animation using matplotlib
  • How to display a 3D plot of a 3D array isosurface in matplotlib mplot3D or similar?
  • Saving a 3D-plot in a PDF 3D with Python
  • How to plot a 3D continuous line in Matplotlib?
  • How to plot a 3D patch collection in matplotlib?
  • Plot Matplotlib 3D plot_surface with contour plot projection
  • Plot 3D bars without axes in Matplotlib
  • Setting the aspect ratio of a 3D plot in Matplotlib
  • How to plot a point on 3D axes in Matplotlib?
  • Plot a 3D surface from {x,y,z}-scatter data in Python Matplotlib
  • How to plot a 3D density map in Python with Matplotlib?
  • How to plot 3D graphs using Python Matplotlib?
  • Animate a rotating 3D graph in Matplotlib
  • Plotting a 3D surface from a list of tuples in matplotlib?

Note

Table of Contents

  • 3D Scatter and Line Plots
  • Surface Plots
  • 3D Bar Plots
  • Can you plot in 3D in Python?
  • Can Matplotlib be used for 3D plotting?
  • How do you plot a 3D array in Python?
  • Is Matplotlib 2D or 3D?

Click here to download the full example code

Demonstrates plotting a 3D surface colored with the coolwarm colormap. The surface is made opaque by using antialiased=False.

Also demonstrates using the LinearLocator and custom formatting for the z axis tick labels.

import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator
import numpy as np

fig, ax = plt.subplots(subplot_kw={"projection": "3d"})

# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)

# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
# A StrMethodFormatter is used automatically
ax.zaxis.set_major_formatter('{x:.02f}')

# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

Gallery generated by Sphinx-Gallery

Want to be inspired? Come join my Super Quotes newsletter. 😎

Every Data Scientist should know how to create effective data visualisations. Without visualisation, you’ll be stuck trying to crunch numbers and imagine thousands of data points in your head!

Beyond that, it’s also a crucial tool for communicating effectively with non-technical business stake holders who’ll more easily understand your results with a picture rather than just words.

Most of the data visualisation tutorials out there show the same basic things: scatter plots, line plots, box plots, bar charts, and heat maps. These are all fantastic for gaining quick, high-level insight into a dataset.

But what if we took things a step further. A 2D plot can only show the relationships between a single pair of axes x-y; a 3D plot on the other hand allows us to explore relationships of 3 pairs of axes: x-y, x-z, and y-z.

In this article, I’ll give you an easy introduction into the world of 3D data visualisation using Matplotlib. At the end of it all, you’ll be able to add 3D plotting to your Data Science tool kit!

Just before we jump in, check out the AI Smart Newsletterto read the latest and greatest on AI, Machine Learning, and Data Science!

3D Scatter and Line Plots

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.

Once this sub-module is imported, 3D plots can be created by passing the keyword projection="3d" to any of the regular axes creation functions in Matplotlib:

from mpl_toolkits import mplot3d

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = plt.axes(projection="3d")

plt.show()

Now that our axes are created we can start plotting in 3D. The 3D plotting functions are quite intuitive: instead of just scatter we call scatter3D , and instead of passing only x and y data, we pass over x, y, and z. All of the other function settings such as colour and line type remain the same as with the 2D plotting functions.

Here’s an example of plotting a 3D line and 3D points.

fig = plt.figure()
ax = plt.axes(projection="3d")

z_line = np.linspace(0, 15, 1000)
x_line = np.cos(z_line)
y_line = np.sin(z_line)
ax.plot3D(x_line, y_line, z_line, 'gray')

z_points = 15 * np.random.random(100)
x_points = np.cos(z_points) + 0.1 * np.random.randn(100)
y_points = np.sin(z_points) + 0.1 * np.random.randn(100)
ax.scatter3D(x_points, y_points, z_points, c=z_points, cmap='hsv');

plt.show()

Here’s the most awesome part about plotting in 3D: interactivity. The interactivity of plots becomes extremely useful for exploring your visualised data once you’ve plotted in 3D. Check out some of the different views I created by doing a simple click-and-drag of the plot!

Surface Plots

Surface plots can be great for visualising the relationships among 3 variables across the entire 3D landscape. They give a full structure and view as to how the value of each variable changes across the axes of the 2 others.

Constructing a surface plot in Matplotlib is a 3-step process.

(1) First we need to generate the actual points that will make up the surface plot. Now, generating all the points of the 3D surface is impossible since there are an infinite number of them! So instead, we’ll generate just enough to be able to estimate the surface and then extrapolate the rest of the points. We’ll define the x and y points and then compute the z points using a function.

fig = plt.figure()
ax = plt.axes(projection="3d")
def z_function(x, y):
return np.sin(np.sqrt(x ** 2 + y ** 2))

x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)

X, Y = np.meshgrid(x, y)
Z = z_function(X, Y)

(2) The second step is to plot a wire-frame — this is our estimate of the surface.

fig = plt.figure()
ax = plt.axes(projection="3d")
ax.plot_wireframe(X, Y, Z, color='green')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

plt.show()

(3) Finally, we’ll project our surface onto our wire-frame estimate and extrapolate all of the points.

ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
cmap='winter', edgecolor='none')
ax.set_title('surface');

Beauty! There’s our colourful 3D surface!

3D Bar Plots

Bar plots are used quite frequently in data visualisation projects since they’re able to convey information, usually some type of comparison, in a simple and intuitive way. The beauty of 3D bar plots is that they maintain the simplicity of 2D bar plots while extending their capacity to represent comparative information.

Each bar in a bar plot always needs 2 things: a position and a size. With 3D bar plots, we’re going to supply that information for all three variables x, y, z.

We’ll select the z axis to encode the height of each bar; therefore, each bar will start at z = 0 and have a size that is proportional to the value we are trying to visualise. The x and y positions will represent the coordinates of the bar across the 2D plane of z = 0. We’ll set the x and y size of each bar to a value of 1 so that all the bars have the same shape.

Check out the code and 3D plots below for an example!

fig = plt.figure()
ax = plt.axes(projection="3d")

num_bars = 15
x_pos = random.sample(xrange(20), num_bars)
y_pos = random.sample(xrange(20), num_bars)
z_pos = [0] * num_bars
x_size = np.ones(num_bars)
y_size = np.ones(num_bars)
z_size = random.sample(xrange(20), num_bars)

ax.bar3d(x_pos, y_pos, z_pos, x_size, y_size, z_size, color='aqua')
plt.show()

Can you plot in 3D in Python?

We could plot 3D surfaces in Python too, the function to plot the 3D surfaces is plot_surface(X,Y,Z), where X and Y are the output arrays from meshgrid, and Z=f(X,Y) or Z(i,j)=f(X(i,j),Y(i,j)). The most common surface plotting functions are surf and contour. TRY IT!

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 3D array in Python?

MatPlotLib with Python.

Create a new figure or activate an existing figure using figure() method..

Add an '~. axes. ... .

Create a random data of size=(3, 3, 3)..

Extract x, y, and z data from the 3D array..

Plot 3D scattered points on the created axis..

To display the figure, use show() method..

Is Matplotlib 2D or 3D?

Matplotlib is an excellent 2D and 3D graphics library for generating scientific figures.