如何在3D图中绘制圆?

问题描述

我已经在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()

解决方法

这是一种将球面参数曲面绘制为线框的方法:

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,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)
    return x,y,z,limx,limz


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

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

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

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

plt.show()

enter image description here