Python can actually draw 3D images!

Python, a programming language that has been around for many years (measured in technology years) and has almost all functional libraries. Want to send an email? Python can help you. Want to perform some complex math calculations? Python can do the job too. I also use it daily for image drawing and data visualization. But most friends probably have never used its 3D function. So, put on your 3D glasses (joke face)! ~

Essential libraries

  • Matplotlib : It is built on the solid foundation of NumPy arrays and is designed to work with the SciPy stack.

  • Numpy : The preferred package for array processing, providing high-performance multi-dimensional arrays and matrices. Python is a good partner for data processing.

  • mpl_toolkits : This is where the 3D magic happens, it provides basic 3D drawing tools.

Example 1: Sine wave dance in 3D

Using np.arange and np.sin, we create an array of points. Then, we use matplotlib's scatter() method to help us plot this dance.

# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D


# Set up the figure and axis
fig = plt.figure(figsize=(10, 10))
ax = plt.axes(projection='3d')


# Generating data points for the 3D sine wave
x = np.arange(0, 20, 0.1)
y = np.sin(x)
z = y * np.sin(x)
c = x + y


# Plotting the scatter graph
ax.scatter(x, y, z, c=c)


# Turning off the axis
plt.axis('off')


# Display the graph
plt.show()

0b30ef85b9f28f0b06aced6fb45396c4.png       1288ec5972142f97fa49d37fb1e1718c.png

Display of running results

Example 2: Colorful cubes

We establish a 3D coordinate axis with X=5, Y=5, Z=5 dimensions, use np.ones() to construct the dimensions of the cube, and then use the alpha parameter to control the transparency of the color.

# Set up the figure and axis
fig = plt.figure(figsize=(10, 10))
ax = plt.axes(projection='3d')


# Data points for the cube
x = np.ones(5)
y = np.ones(5)
z = np.ones(5)


# Plotting the cube with different opacities
for i in range(5):
    for j in range(5):
        for k in range(5):
            ax.scatter(i, j, k, alpha=(i+j+k)/15)


# Turning off the axis
plt.axis('off')


# Display the graph
plt.show()

7f8ec8bc1b9463d67984ceb47048800b.png      ea67b67971ed98bd5107ddf482d4e690.png

Display of running results

Example 3: Green wireframe

Using numpy.linspace(), we create an array of linearly placed elements and then visualize a 3D wireframe.

# Set up the figure and axis
fig = plt.figure(figsize=(10, 10))
ax = plt.axes(projection='3d')


# Generating data points
x = np.linspace(-5, 5, 50)
y = np.linspace(-5, 5, 50)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))


# Plotting the wireframe
ax.plot_wireframe(x, y, z, color='green')


# Turning off the axis
plt.axis('off')


# Display the graph
plt.show()

58c5e5d6a913ef38a51382e4092b8ea5.png     b10b774fbd9d77f1cd8b9a405d873ddb.png

Display of running results

Example 4: 360 degree spiral

We draw a spiral shape and use loops to view it in a 360 degree rotation.

# Set up the figure and axis
fig = plt.figure(figsize=(10, 10))
ax = plt.axes(projection='3d')


# Generating the spiral data points
theta = np.linspace(-8 * np.pi, 8 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)


# Plotting the spiral
ax.plot(x, y, z, 'b-')


# Turning off the axis
plt.axis('off')


# Display the graph
plt.show()

af8fc879e47f5abe82449fb63b0efefc.png     f3e5bce9749ebba583bb41914eda461d.png

Display of running results

While Python can't make you a cup of coffee yet (but would like to), it can certainly make your data dance in 3D!

·  END  ·

HAPPY LIFE

6b06622d85bfc6f7f07f7a239438325e.png

This article is for learning and communication only. If there is any infringement, please contact the author to delete it.

Guess you like

Origin blog.csdn.net/weixin_38739735/article/details/132913818