编辑代码

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

# 圆桶参数
diameter = 37
radius = diameter / 2
height = 39

# 上层振子位置(俯视图)
x_top = np.array([18.5, 15.9, 9.25, 0, -9.25, -15.9])
y_top = np.array([0, 9.25, 15.9, 18.5, 15.9, 9.25])

# 上层振子位置(侧视图)
z_top = np.array([0, 7.8, 15.6, 23.4, 31.2, 39])

# 绘制俯视图
fig_top = plt.figure(figsize=(6, 6))
ax_top = fig_top.add_subplot(111)
ax_top.set_title('Top View')
ax_top.set_xlabel('X (cm)')
ax_top.set_ylabel('Y (cm)')
ax_top.axis('equal')

# 绘制圆桶轮廓
circle = plt.Circle((0, 0), radius, color='b', fill=False)
ax_top.add_artist(circle)

# 绘制上层振子
for i in range(len(x_top)):
    ax_top.scatter(x_top[i], y_top[i], color='r')
    ax_top.text(x_top[i], y_top[i], f'振子{i + 1}', ha='center', va='center')

# 设置坐标轴范围
ax_top.set_xlim(-radius - 1, radius + 1)
ax_top.set_ylim(-radius - 1, radius + 1)

# 绘制侧视图
fig_side = plt.figure(figsize=(6, 6))
ax_side = fig_side.add_subplot(111)
ax_side.set_title('Side View')
ax_side.set_xlabel('振子编号')
ax_side.set_ylabel('Z (cm)')

# 绘制上层振子
for i in range(len(z_top)):
    ax_side.scatter(i + 1, z_top[i], color='r')
    ax_side.text(i + 1, z_top[i], f'振子{i + 1}', ha='center', va='center')

# 设置坐标轴范围
ax_side.set_xlim(0, 7)
ax_side.set_ylim(-1, height + 1)

# 绘制三维视图
fig_3d = plt.figure(figsize=(10, 8))
ax_3d = fig_3d.add_subplot(111, projection='3d')
ax_3d.set_title('3D View')
ax_3d.set_xlabel('X (cm)')
ax_3d.set_ylabel('Y (cm)')
ax_3d.set_zlabel('Z (cm)')

# 绘制圆桶表面
theta = np.linspace(0, 2 * np.pi, 100)
z_cylinder = np.linspace(0, height, 100)
Theta, Z = np.meshgrid(theta, z_cylinder)
X = radius * np.cos(Theta)
Y = radius * np.sin(Theta)
ax_3d.plot_surface(X, Y, Z, alpha=0.2)

# 绘制上层振子
for i in range(len(x_top)):
    theta = np.arctan2(y_top[i], x_top[i])
    ax_3d.scatter(radius * np.cos(theta), radius * np.sin(theta), z_top[i], color='r')
    ax_3d.text(radius * np.cos(theta), radius * np.sin(theta), z_top[i], f'振子{i + 1}', color='r')

# 绘制下层振子
theta_shift = np.radians(15)
x_bottom = radius * np.cos(np.arctan2(y_top, x_top) + theta_shift)
y_bottom = radius * np.sin(np.arctan2(y_top, x_top) + theta_shift)
for i in range(len(x_bottom)):
    ax_3d.scatter(x_bottom[i], y_bottom[i], z_top[i], color='g')
    ax_3d.text(x_bottom[i], y_bottom[i], z_top[i], f'振子{i + 7}', color='g')

# 显示所有图形
plt.show()