Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deeply understand the Matplotlib3D drawing function plot_surface

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces you how to deeply understand the Matplotlib3D drawing function plot_surface, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Tonight I began to contact Matplotlib's 3D drawing function plot_surface, which is really very powerful, the picture quality can reach the publication level, and the 3D image can be rotated, and you can look at a 3D stereogram from different angles, but I found that the codes for 3D drawing in the major Chinese open source communities are all the same. now, apart from reading the source code description, I can hardly get any important parameter description about plot_surface. And I feel that the source code description in pure English is obscure, and there are no pictures. Beginners see it in the clouds. After a night of debugging, I fully understand the meaning of all the parameters. And how to change these parameters to control the display of graphics, now share some insights.

Talk is cheap, show the code

#-*-coding: utf-8-*-

# author: inspurer (month small water length)

# pc_type lenovo

# create_date: 2019-5-25

# file_name: 3DTest

# github https://github.com/inspurer

# official Wechat account month Xiaoshui Chang (ID: inspurer)

"

Draw 3D graphics

"

Import matplotlib.pyplot as plt

Import numpy as np

From mpl_toolkits.mplot3d import Axes3D

# define figure

Fig = plt.figure ()

# two ways to create 3D graphics

# change figure to 3D

Ax = Axes3D (fig)

# ax = fig.add_subplot (111111projection='3d')

# define x, y

X = np.arange (- 4,4,0.25)

Y = np.arange (- 4,4,0.25)

# generate grid data

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

# calculate the length of each pair of points

R = np.sqrt (X * * 2 + Y * * 2)

# calculate the height of the Z axis

Z = np.sin (R)

# draw 3D surfaces

# rstride: span between rows cstride: span between columns

# rcount: set the number of intervals. The default is 50. The number of intervals for ccount: columns cannot appear at the same time as the above two parameters.

# cmap is a color mapping table

# from matplotlib import cm

# ax.plot_surface (X, Y, Z, rstride = 1, cstride = 1, cmap = cm.coolwarm)

# cmap = "rainbow" is also available

# I understand that changing the cmap parameter can control the color combination of 3D surfaces. Generally speaking, the 3D surfaces we see are those of rainbow.

# you can also change rainbow to coolwarm to verify my conclusion

Ax.plot_surface (X, Y, Z, rstride = 1, cstride = 1, cmap = plt.get_cmap ('rainbow'))

# draw the projection from the 3D surface to the bottom. Zdir can choose'z'|'x'|'y'| represent the projection to the zmemxpriy plane, respectively

# zdir = 'zonal, offset =-2 means projection onto z =-2

Ax.contour (X, Y, Z, zdir = 'zonal, offset =-2, cmap = plt.get_cmap (' rainbow'))

# set the dimension of the z-axis, which is similar to that of xPowery.

Ax.set_zlim (- 2,2)

Plt.show ()

The running results are as follows:

one

Most of these parameters are already annotated and should be easy to understand, and the parameters rstride (row stride) and cstride (column stride) (stride means step size, row: row, column: column) parameters are also preliminarily introduced. The following focuses on how to understand them.

Preliminary study

Rotate 3D graphic

two

I found that the number of stripes is 32 in both x and y directions.

And this happens to be [4-(- 4)] / 0.25 = 32 in our x = np.arange (- 4,4,0.25).

While in ax.plot_surface (X, Y, Z, rstride = 1, cstride = 1, cmap = plt.get_cmap ('rainbow')), rstride = 1, cstride = 1 row corresponds to x direction, c (column) corresponds to y direction, rstride = 1 indicates that the stripe interval in x direction is 1 x unit length, and cstride = 1 indicates that the stripe interval in y direction is 1 x unit length. After removing rstride = 1 and cstride = 1, I find that the figure remains the same, indicating that 1 distance is the default value. And these two parameters can only be positive integers, and the minimum positive integer is 1 (the default value), so basically we don't need to modify this parameter in order to smooth the graph.

Verification

Modify the parameter so that rstride = 4

The results are as follows:

three

32 / 4 = 8 the number of stripes in the x direction becomes 8, which is in line with our expectations. It seems that we have to speculate that it is correct.

Modify the parameters to make both rstride and cstride = 4

The results are as follows:

four

You can see that the number of stripes becomes 8 in both x and y directions, and the pattern is rougher than before because of the larger spacing.

This once again proves that our conjecture is correct!

On how to in-depth understanding of the Matplotlib3D drawing function plot_surface to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report