首页 > 其他分享 >2

2

时间:2024-10-27 21:47:05浏览次数:5  
标签: plot plt set ax np import

2.1

点击查看代码
#学号17
import numpy as np  
import matplotlib.pyplot as plt  
  
x = np.linspace(-5, 5, 400)  
y_cosh = np.cosh(x)  
y_sinh = np.sinh(x)  
y_half_exp = 0.5 * np.exp(x)    
plt.figure(figsize=(10, 6))  
ax = plt.gca()  
ax.plot(x, y_cosh, label=r'$y = \cosh(x)$', color='blue')  
ax.plot(x, y_sinh, label=r'$y = \sinh(x)$', color='green')  
ax.plot(x, y_half_exp, label=r'$y = \frac{1}{2}e^x$', color='red')  
plt.legend(loc='upper left')
plt.grid(True)    
plt.title('Plot of $\cosh(x)$, $\sinh(x)$, and $\frac{1}{2}e^x$')  
plt.xlabel('x')  
plt.ylabel('y')  
plt.show()

2.2

点击查看代码
#学号17
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import quad
 
def fun(t, x):
    return np.exp(-t) * (t ** (x - 1))
x = np.linspace(0, 10, 100)  # x 的范围
y = [quad(fun, 0, np.inf, args=i)[0] for i in x]  # 计算积分
 
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('$ y = \int_0^{\infty} e^{-t} \cdot t^{x-1} dt $')
plt.grid(True)
plt.show()

2.3

点击查看代码
#学号17
import numpy as np  
import matplotlib.pyplot as plt  

x = np.linspace(-10, 10, 400)  

plt.figure(figsize=(10, 6))  
ax = plt.gca()  

colors = ['r', 'g', 'b', 'c', 'm', 'y']   
for k, color in zip(range(1, 7), colors):  
    y = k * x**2 + 2*k  
    ax.plot(x, y, label=f'$y = {k}x^2 + 2{k}$', color=color)  

plt.legend(loc='upper left')  
 
plt.grid(True)  
  
plt.title('Plots of $y = kx^2 + 2k$ for $k = 1, 2, 3, ..., 6$')  
plt.xlabel('x')  
plt.ylabel('y')  

plt.show()

2.4

点击查看代码
#学号17
import numpy as np  
import matplotlib.pyplot as plt  
 
x = np.linspace(-10, 10, 400)  
fig, axs = plt.subplots(2, 3, figsize=(12, 8))  
for k, ax in enumerate(axs.flat, start=1):  
    y = k * x**2 + 2*k  
    ax.plot(x, y, label=f'$y = {k}x^2 + 2{k}$')  
    ax.set_title(f'k = {k}')  
    ax.legend()  
    ax.grid(True)  
plt.tight_layout()  
plt.show()


2.5

点击查看代码
#学号17
import numpy as np  
import matplotlib.pyplot as plt  
from mpl_toolkits.mplot3d import Axes3D   
u = np.linspace(-2, 2, 400)  
v = np.linspace(0, 2 * np.pi, 400)  
U, V = np.meshgrid(u, v)  
x = np.sqrt(1 + U**2 + V**2) * np.cos(V)  
y = np.sqrt(1 + U**2 + V**2) * np.sin(V)  
z = U  
fig = plt.figure()  
ax = fig.add_subplot(111, projection='3d')   
ax.plot_surface(x, y, z, cmap='viridis', edgecolor='none')  
ax.set_xlabel('X')  
ax.set_ylabel('Y')  
ax.set_zlabel('Z')   
plt.show()#第一个曲面
u = np.linspace(-2, 2, 400)  
v = np.linspace(-2, 2, 400)  
U, V = np.meshgrid(u, v)  
X, Y = U, V  
Z = X**2 + Y**2  
fig = plt.figure()  
ax = fig.add_subplot(111, projection='3d')  
ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='none')  
ax.set_xlabel('X')  
ax.set_ylabel('Y')  
ax.set_zlabel('Z')  
plt.show()#第二个曲面

2.6

点击查看代码
#学号17
import numpy as np  
import matplotlib.pyplot as plt  
from mpl_toolkits.mplot3d import Axes3D   
x = np.linspace(0, 43.65, 40)  
y = np.linspace(0, 58.2, 50)  
X, Y = np.meshgrid(x, y)  
Z = np.sin(np.sqrt(X**2 + Y**2)) * 100    
fig = plt.figure(figsize=(12, 8))  
ax = fig.add_subplot(121, projection='3d')  
ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='none')  
ax.set_xlabel('X (km)')  
ax.set_ylabel('Y (km)')  
ax.set_zlabel('Elevation (m)')  
ax.set_title('3D Surface Plot of Elevation Data')   
plt.subplot(122)  
CS = plt.contour(X, Y, Z, colors='k')  
plt.clabel(CS, inline=1, fontsize=10)   
idx_a_x = np.argmin(np.abs(x - 30))  
idx_a_y = np.argmin(np.abs(y - 0))  
idx_b_x = np.argmin(np.abs(x - 43))  
idx_b_y = np.argmin(np.abs(y - 30))  
  
plt.plot(x[idx_a_x], y[idx_a_y], 'ro', markersize=5, label='A(30,0)')  
plt.plot(x[idx_b_x], y[idx_b_y], 'go', markersize=5, label='B(43,30)')  
plt.xlabel('X (km)')  
plt.ylabel('Y (km)')  
plt.title('Contour Plot of Elevation Data with Points A and B')  
plt.legend()   
real_area = 43.65 * 58.2  
print(f"Actual Surface Area (ignoring elevation changes): {real_area} km^2")  
plt.tight_layout()  
plt.show()


标签:,plot,plt,set,ax,np,import
From: https://www.cnblogs.com/xinji0117/p/18509084

相关文章