如何在3D绘图中绘制圆?

2024-05-19 01:48:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经在2D绘图中绘制了一个圆,但现在我想在3D绘图中绘制同一个圆(即绘制半径和中心已知的球体)

import matplotlib.pyplot as plt

x = 50
y = 50
radius = 10
#The x and y values are examples. They are pre-defined in the original program.

fig, px = plt.subplots(1)
plt.xlim(100)
plt.ylim(100)

circle1 = plt.Circle((x,y), radius, color="r")    
px.set_aspect(1)
px.add_artist(circle1)

plt.show()

Tags: theimport绘图matplotlibas半径绘制plt
1条回答
网友
1楼 · 发布于 2024-05-19 01:48:47

以下是一种将球体参数化曲面绘制为线框的方法:

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


def sphere():
    # Parameters
    R = 1
    tht = np.linspace(0, 2* np.pi, 10)
    phi = np.linspace(0, 2* np.pi, 10)
    # Meshgrid to create mesh
    tht, phi = np.meshgrid(tht, phi)
    # Parametric equations in cartesian co-ordinates
    x = (R * np.cos(tht))* np.cos(phi)
    y = (R * np.cos(tht))* np.sin(phi)
    z = R * np.sin(tht)

    limx, limy, limz = (-2*R, 2*R), (-2*R, 2*R), (-2*R, 2*R)
    return x, y, z, limx, limy, limz


# Adjustment for figure
fig = plt.figure('Parametric Surfaces')
ax  = fig.add_subplot(111, projection='3d')
x, y, z, limx, limy, limz = sphere()

# Plot the data
h = ax.plot_wireframe(x, y, z, edgecolor='b')

# Add labels & title
ax.set_xlabel('X', fontweight = 'bold', fontsize = 14)
ax.set_ylabel('Y', fontweight = 'bold', fontsize = 14)
ax.set_zlabel('Z', fontweight = 'bold', fontsize = 14)
ax.set_title('Parametric Surface', fontweight = 'bold', fontsize = 16)

# Adjust figure axes
ax.set_xlim(*limx)
ax.set_ylim(*limy)
ax.set_zlim(*limz)

plt.show()

enter image description here

相关问题 更多 >

    热门问题