2301_77194728 2024-11-28 15:23 采纳率: 0%
浏览 6

关于#python#的问题:import matplotlib.pyplot as plt



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

# 设置圆柱参数
outer_radius = 1.0  # 外圆柱半径
inner_radius = 0.5  # 内圆柱半径(设置为0表示没有内圆柱)
height = 10.0  # 圆柱长度(这里代表高度,因为是水平放置)
resolution = 100  # 分辨率,值越高越平滑

# 创建角度数据
theta = np.linspace(0, 2 * np.pi, resolution)
y = np.linspace(-height/2, height/2, resolution)

# 创建网格
Theta, Y = np.meshgrid(theta, y)

# 计算X和Z坐标
X_outer = outer_radius * np.cos(Theta)
Z_outer = outer_radius * np.sin(Theta)
if inner_radius > 0:
    X_inner = inner_radius * np.cos(Theta)
    Z_inner = inner_radius * np.sin(Theta)

# 绘制3D图形
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 绘制外圆柱表面
ax.plot_surface(X_outer, Y, Z_outer, color='b', alpha=0.5, rstride=4, cstride=4)

# 如果定义了内圆柱,则绘制内圆柱表面
if inner_radius > 0:
    ax.plot_surface(X_inner, Y, Z_inner, color='w', alpha=0.5, rstride=4, cstride=4)

# 隐藏坐标轴
ax.axis('off')

# 设置视角距离
ax.dist = 50  # 将默认的10改为更大的数字,例如12,以拉远视角

# 如果需要,调整坐标轴的限制来扩大视野
ax.set_xlim(-outer_radius-0.5, outer_radius+0.5)
ax.set_ylim(-height/2-0.5, height/2+0.5)
ax.set_zlim(-outer_radius-0.5, outer_radius+0.5)

# 显示图形
plt.show()

想问下各位为什么我改变了圆柱长度但是显示出来的图像还是和没改变的时候一样啊

  • 写回答

1条回答 默认 最新

  • stormsha 优质创作者: 编程框架技术领域 2024-11-28 16:08
    关注

    本答案参考GPT。若问题解决,望采纳;如仍有疑问,请评论回复。

    在您提供的代码中,您已经正确地设置了圆柱的高度(height),但是您在创建角度数据时使用的是 thetay,其中 y 用于表示圆柱的高度。您需要确保 y 的范围与圆柱的高度相匹配。

    问题在于 y 的范围设置不正确。您应该使用 height 变量来设置 y 的范围,而不是直接使用 height/2。下面是修改后的代码:

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    
    # 设置圆柱参数
    outer_radius = 1.0  # 外圆柱半径
    inner_radius = 0.5  # 内圆柱半径(设置为0表示没有内圆柱)
    height = 10.0  # 圆柱长度(这里代表高度,因为是水平放置)
    resolution = 100  # 分辨率,值越高越平滑
    
    # 创建角度数据
    theta = np.linspace(0, 2 * np.pi, resolution)
    y = np.linspace(-height/2, height/2, resolution)  # 确保y的范围与height相匹配
    
    # 创建网格
    Theta, Y = np.meshgrid(theta, y)
    
    # 计算X和Z坐标
    X_outer = outer_radius * np.cos(Theta)
    Z_outer = outer_radius * np.sin(Theta)
    if inner_radius > 0:
        X_inner = inner_radius * np.cos(Theta)
        Z_inner = inner_radius * np.sin(Theta)
    
    # 绘制3D图形
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    # 绘制外圆柱表面
    ax.plot_surface(X_outer, Y, Z_outer, color='b', alpha=0.5, rstride=4, cstride=4)
    
    # 如果定义了内圆柱,则绘制内圆柱表面
    if inner_radius > 0:
        ax.plot_surface(X_inner, Y, Z_inner, color='w', alpha=0.5, rstride=4, cstride=4)
    
    # 隐藏坐标轴
    ax.axis('off')
    
    # 设置视角距离
    ax.dist = 50  # 将默认的10改为更大的数字,例如12,以拉远视角
    
    # 如果需要,调整坐标轴的限制来扩大视野
    ax.set_xlim(-outer_radius-0.5, outer_radius+0.5)
    ax.set_ylim(-height/2-0.5, height/2+0.5)
    ax.set_zlim(-outer_radius-0.5, outer_radius+0.5)
    
    # 显示图形
    plt.show()
    

    通过这种方式,您应该能够正确地改变圆柱的长度,并在图形中看到变化。

    评论

报告相同问题?

问题事件

  • 创建了问题 11月28日