Black Lives Matter. Please consider donating to Black Girls Code today.

Adding wireframe around a sphere

I have the following code,

    def plot_back(self):
        """back half of sphere"""
        u_angle = np.linspace(0, np.pi, 25)
        v_angle = np.linspace(0, np.pi, 25)
        x_dir = np.outer(np.cos(u_angle), np.sin(v_angle))
        y_dir = np.outer(np.sin(u_angle), np.sin(v_angle))
        z_dir = np.outer(np.ones(u_angle.shape[0]), np.cos(v_angle))
        self.plotly_figure.add_surface(z=z_dir, x=x_dir, y=y_dir, opacity=self.sphere_alpha,
                                       colorscale=[[0, self.sphere_color], [1, self.sphere_color]])

        # wireframe
        lines = []
        line_marker = dict(color=self.frame_color, width=2)
        for i, j, k in zip(x_dir[0::5], y_dir[0::5], z_dir[0::5]):
            lines.append(go.Scatter3d(x=i, y=j, z=k, mode='lines', line=line_marker, opacity=self.frame_alpha))
        self.plotly_figure.add_traces(lines)

 def plot_front(self):
        """front half of sphere"""
        u_angle = np.linspace(-np.pi, 0, 25)
        v_angle = np.linspace(0, np.pi, 25)
        x_dir = np.outer(np.cos(u_angle), np.sin(v_angle))
        y_dir = np.outer(np.sin(u_angle), np.sin(v_angle))
        z_dir = np.outer(np.ones(u_angle.shape[0]), np.cos(v_angle))
        self.plotly_figure.add_surface(z=z_dir, x=x_dir, y=y_dir, opacity=self.sphere_alpha,
                                       colorscale=[[0, self.sphere_color], [1, self.sphere_color]])
        # wireframe
        lines = []
        line_marker = dict(color=self.frame_color, width=2)
        for i, j, k in zip(x_dir[0::5], y_dir[0::5], z_dir[0::5]):
            lines.append(go.Scatter3d(x=i, y=j, z=k, mode='lines', line=line_marker, opacity=self.frame_alpha))

which produces a sphere as show below:
image

I want to add circles around the sphere so it will end up looking something like this:


but i can’t seem to get this right and i;ve been trying for hours now.

HELP!

thanks

Hi @avivazran,

I’m wondering why are you defining the sphere from two semi-spheres, because it is possible to define the whole surface at once. Here is the code to plot both the sphere and a number of meridians and parallels:

import numpy as np
import plotly.graph_objs as go
from numpy import pi, sin, cos

theta = np.linspace(0, 2*pi, 120)
phi = np.linspace(0, pi, 60)
u , v = np.meshgrid(theta, phi)
xs = cos(u)*sin(v)
ys = sin(u)*sin(v)
zs = cos(v)

x = []
y = []
z = []
for t in [theta[10*k] for k in range(12)]:  # meridians:
    x.extend(list(cos(t)*sin(phi))+[None])# None is inserted to mark the end of a meridian line
    y.extend(list(sin(t)*sin(phi))+[None]) 
    z.extend(list(cos(phi))+[None])
    
for s in [phi[6*k] for k in range(10)]:  # parallels
    x.extend(list(cos(theta)*sin(s))+[None]) # None is inserted to mark the end of a parallel line 
    y.extend(list(sin(theta)*sin(s))+[None]) 
    z.extend([cos(s)]*120+[None])

fig=go.Figure()
fig.add_surface(x=xs, y=ys, z=zs, 
                colorscale=[[0, '#ffffff' ], [1, '#ffffff']], 
                showscale=False, opacity=0.5)  # or opacity=1
fig.add_scatter3d(x=x, y=y, z=z, mode='lines', line_width=3, line_color='rgb(10,10,10)')
fig.update_layout(width=700, height=700)