-1

I have made a circle in a 2D plot but now I'd like to plot the same circle in a 3D plot (i.e. plotting a sphere with known radius and center)

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()
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80

1 Answers1

0

Here is one approach to plot a sphere parametric surface as a wireframe:

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

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • Thanks for the help but isn't there a way to make a smooth sphere provided we know its radius and centre and place it into a 3D plot? – Ibrahim Malik Oct 14 '20 at 10:08