Python uses Matplotlib to draw 2-dimensional + 3-dimensional (2D images and 3D images) to understand the postgraduate entrance examination points (or double points)

High numbers sometimes encounter unimaginable graphics, and may need to draw pictures to help learn

Draw a line chart (plt.plot)
set the picture size and resolution (plt.figure)
save the picture to the local (plt.savefig)
set the xy axis scale and string (xticks, yticks)
set the title, xy axis label (title, xlable , ylable)
other image types (scatter plot plt.scatter, bar chart plt.bar, horizontal plt.barh, histogram plt.hist(bin.width group distance, how many groups num_bins are divided into,))

Go directly to the code and draw a two-dimensional image

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


# 设置多个刻度范围
plt.figure(figsize=(20, 20))
ran = []
x = np.arange(-10, 10, 0.01)
y = 1
y = x + 1/x+5*np.cos(x)

ran.extend(range(-10, 10, 1))
plt.xticks(ran, rotation=20, fontsize=10)
plt.yticks(range(-10, 10, 1), fontsize=10)
plt.xlim(-10, 10)
plt.ylim(-10, 10)

plt.grid(alpha=1) #画出格子

plt.plot(x, y, color='green', marker='o', linestyle='solid', linewidth=1, markersize=6)
plt.legend(["y = x + 1/x + 5cosx "], loc="best")
plt.savefig('D:\ef2d.png')
plt.show()



insert image description here

Draw another 3D image

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

fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y)
#Z = np.sqrt(X**2 + Y**2)
# Z = np.sin(R)
Z = X**2 - Y**2


ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')

plt.savefig('D:\img3d.png') #保存图像
plt.show()

insert image description here

Example: For example, for this question of double integral of number 2 and number 3 in the 2005 postgraduate entrance examination, I didn't understand why a square should be divided into two areas at the beginning
(of course I know to use polar coordinates), and then draw a picture Take a look to understand
insert image description here
the code:

from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.ticker as ticker
from matplotlib.ticker import MultipleLocator
import sympy as sym

fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(0, 1, 0.01)
Y = np.arange(0, 1, 0.01)
X, Y = np.meshgrid(X, Y)
Z = abs(X**2 + Y**2 - 1)


ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')

plt.savefig('D:\img3d.png') #保存图像
plt.show()

insert image description here
As shown in the figure below, the integrand is divided into two areas for easy calculation because of the absolute value sign. In essence, it is to find the volume of two different areas. The two
areas are
D1 = x^2+y ^2<=1(x >=0,y>=0)

D2 = x^2+y ^2>1(x<=1,y<=1)

Guess you like

Origin blog.csdn.net/pz641/article/details/126286635