Last active
November 17, 2016 10:36
-
-
Save ilciavo/f93426527cb1fc1a4da3b87627557bab to your computer and use it in GitHub Desktop.
Drawing a sliced sphere
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
from matplotlib import cm, colors | |
from mpl_toolkits.mplot3d import Axes3D | |
import numpy as np | |
from matplotlib.patches import FancyArrowPatch | |
from mpl_toolkits.mplot3d import proj3d | |
class Arrow3D(FancyArrowPatch): | |
def __init__(self, xs, ys, zs, *args, **kwargs): | |
FancyArrowPatch.__init__(self, (0,0), (0,0), *args, **kwargs) | |
self._verts3d = xs, ys, zs | |
def draw(self, renderer): | |
xs3d, ys3d, zs3d = self._verts3d | |
xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M) | |
self.set_positions((xs[0],ys[0]),(xs[1],ys[1])) | |
FancyArrowPatch.draw(self, renderer) | |
plt.style.use('ggplot') | |
gg_blue=plt.rcParams['axes.color_cycle'][1] | |
gg_color=plt.rcParams['axes.color_cycle'][2] | |
# Create a sphere | |
r = 1 | |
pi = np.pi | |
cos = np.cos | |
sin = np.sin | |
theta, phi = np.mgrid[0.0:pi:17j, pi/2:2.0*pi:25j] | |
theta1, phi1 = np.mgrid[pi/2:pi:9j, 0:pi/2:9j] | |
x = r*sin(theta)*cos(phi) | |
y = r*sin(theta)*sin(phi) | |
z = r*cos(theta) | |
x1 = r*sin(theta1)*cos(phi1) | |
y1 = r*sin(theta1)*sin(phi1) | |
z1 = r*cos(theta1) | |
theta_=np.pi/6 | |
phi_=np.pi/3 | |
xx=np.array([r*sin(theta_)*cos(phi_),r*sin(theta_)*cos(phi_),0]) | |
yy=np.array([r*sin(theta_)*sin(phi_),r*sin(theta_)*sin(phi_),0]) | |
zz=np.array([r*cos(theta_),0,0]) | |
a_x = Arrow3D([0, 1.15], [0, 0], [0, 0], mutation_scale=20, lw=2, arrowstyle="-|>", color="k") | |
a_y = Arrow3D([0, 0], [0, 1.15], [0, 0], mutation_scale=20, lw=2, arrowstyle="-|>", color="k") | |
a_z = Arrow3D([0, 0], [0, 0], [0, 1.15], mutation_scale=20, lw=2, arrowstyle="-|>", color="k") | |
w_0 = Arrow3D([0, 0], [0, 0], [0, 1], mutation_scale=20, lw=2, arrowstyle="-|>", color="k") | |
w_f = Arrow3D([0, r*sin(theta_)*cos(phi_)], [0, r*sin(theta_)*sin(phi_)], [0, r*cos(theta_)], mutation_scale=20, lw=2, arrowstyle="-|>", color=gg_blue) | |
r_a = 0.2 | |
angle_theta = Arrow3D([0, r_a*sin(theta_)*cos(phi_)], [0, r_a*sin(theta_)*sin(phi_)], [r_a, r_a*cos(theta_)], mutation_scale=20, lw=2, arrowstyle="-|>", color="k") | |
angle_phi = Arrow3D([0.5*r_a, r_a*sin(theta_)*cos(phi_)], [0, r_a*sin(theta_)*sin(phi_)], [0, 0], mutation_scale=20, lw=2, arrowstyle="-|>", color="k") | |
#Set colours and render | |
fig = plt.figure(figsize=(20, 20)) | |
ax = fig.add_subplot(111, projection='3d') | |
ax.view_init(50,20) | |
ax.set_axis_bgcolor('white') | |
ax.plot(xx,yy,zz, linestyle='--', color='k', linewidth=0.5) | |
ax.plot_surface(x, y, z, rstride=1, cstride=1, color=gg_color, alpha=0.5, linewidth=0.2) | |
ax.plot_surface(x1, y1, z1, rstride=1, cstride=1, color=gg_color, alpha=0.5, linewidth=0.2) | |
ax.set_axis_off() | |
ax.text(0.2, 0.03, 0, "$\phi_0$", color='k',fontsize=20) | |
ax.text(0, 0.02, 0.2, "$\\theta_0$", color='k',fontsize=20) | |
ax.text(0, 0.02, 0.85, "$\Omega_0$", color='k',fontsize=20) | |
ax.text(r*sin(theta_)*cos(phi_), r*sin(theta_)*sin(phi_), r*cos(theta_), "$\Omega_f$", color=gg_blue,fontsize=20) | |
ax.add_artist(a_x) | |
ax.add_artist(a_y) | |
ax.add_artist(a_z) | |
ax.add_artist(w_0) | |
ax.add_artist(w_f) | |
ax.add_artist(angle_theta) | |
ax.add_artist(angle_phi) | |
ax.text(1.25, 0, 0, "$x$", color='k',fontsize=30) | |
ax.text(0, 1.15, 0, "$y$", color='k',fontsize=30) | |
ax.text(0, 0, 1.2, "$z$", color='k',fontsize=30) | |
ax.set_xlim([-1,1]) | |
ax.set_ylim([-1,1]) | |
ax.set_zlim([-1,1]) | |
ax.set_aspect("equal") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment