Home > Backend Development > Python Tutorial > Tips Revealed: Draw Beautiful 3D Charts with Python

Tips Revealed: Draw Beautiful 3D Charts with Python

王林
Release: 2023-09-28 13:01:50
Original
1189 people have browsed it

Tips Revealed: Draw Beautiful 3D Charts with Python

Tips Revealed: Use Python to draw beautiful 3D charts

Introduction:
In the field of data visualization, making beautiful 3D charts can display data more intuitively characteristics and trends. Python, as a powerful programming language, has many libraries and tools that can help us achieve this goal. This article will share some tips and specific code examples for drawing beautiful 3D charts in Python to help readers better understand and apply them.

1. Preparation:
Before starting, we need to install several necessary Python libraries, including matplotlib, numpy and mpl_toolkits.mplot3d. You can install it through the following code:

pip install matplotlib

pip install numpy

pip install mpl_toolkits.mplot3d
Copy after login

2. Draw a simple 3D scatter plot:
First, let’s draw a simple 3D scatter plot. The code is as follows:

import matplotlib.pyplot as plt
import numpy as np

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

x = np.random.standard_normal(100)
y = np.random.standard_normal(100)
z = np.random.standard_normal(100)

ax.scatter(x, y, z)

plt.show()
Copy after login

In this example, we first create a Figure object and an Axes3D object, and add # through the add_subplot method The ##Axes3D object is added to the Figure. Then, we generate 100 random numbers obeying the standard normal distribution as x, y, z coordinates, and use the scatter method to draw a scatter plot on the 3D coordinate system.

3. Draw a 3D surface diagram:

Next, we try to draw a 3D surface diagram. The code is as follows:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

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

x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)

X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))

ax.plot_surface(X, Y, Z, cmap='viridis')

plt.show()
Copy after login

In this example, we first generated a one-dimensional array of x and y coordinates, and used the

meshgrid method to generate a grid, and then calculated z according to the formula The value of the coordinate. Finally, the 3D surface plot was drawn using the plot_surface method.

4. Draw 3D histogram:

In addition to scatter plots and surface plots, we can also draw 3D histograms to show the distribution of data. The code is as follows:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

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

x = np.arange(10)
y = np.arange(10)
X, Y = np.meshgrid(x, y)
Z = np.random.randint(1, 10, (10, 10))

ax.bar3d(X.flatten(), Y.flatten(), np.zeros_like(Z).flatten(), 1, 1, Z.flatten())

plt.show()
Copy after login
In this example, we first generated a one-dimensional array of x and y coordinates, and used the

meshgrid method to generate a grid, and then used random The .randint method generates a 10x10 array of random integers as the z coordinate value. Finally, a 3D histogram was drawn using the bar3d method.

Conclusion:

Through the sharing of this article, we have learned some techniques and specific code examples for drawing beautiful 3D charts in Python, including drawing scatter plots, surface plots and histograms. These techniques can help us better display the characteristics and trends of data and improve the effect of data visualization. It is hoped that readers can further master these skills through learning and practice, and use them flexibly in actual projects.

The above is the detailed content of Tips Revealed: Draw Beautiful 3D Charts with Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template