Solid of Revolution from Python to Julia

Hi all,

I have this Python code that can plot solid of revolution of function with input of the region then become the 3-dimensional object.

# R is a region in the xy plane bounded by y = 4 - 2x 
# Plot A solid of revolution that is formed by rotating R around the x axis. 
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d
from matplotlib import cm

np.seterr(divide='ignore', invalid='ignore')

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ll, ul = 0, 1 
u = np.linspace(ll, ul, 100)
v = np.linspace(0, 2*np.pi, 60)
U, V = np.meshgrid(u, v)

Y = 4 - 2*U 
X = U*np.cos(V)
Z = U*np.sin(V)

ax.set_xlabel('Y axis')
ax.set_ylabel('X axis')
ax.set_zlabel('Z axis')

ax.plot_surface(X, Y, Z, cmap=plt.cm.YlGnBu_r)

plt.show()

How to make this code works in Julia with PyCall or Conda, or anything?

What have you tried so far?

2 Likes

Since you asked, I made it shown up with this code only using PyCall, just made it this evening:

using PyCall

ENV["PYTHON"] = "/home/browni/.julia/conda/3/bin/python3"
# the path is from the command 'which python3'

py"""

import matplotlib.pyplot as plt
import numpy as np
import sympy as sy
  
def r1(x):
    return 6*x

def r2(x):
    return 6*(x**2)

def r3(x):
    return x/6

def r4(x):
    return (x/6)**(1/2)

def vx(x):
    return np.pi*(r1(x)**2 - r2(x)**2)

def vy(x):
    return np.pi*(r4(x)**2 - r3(x)**2)
  
x = sy.Symbol("x")
vx = sy.integrate(vx(x), (x, 0, 1))
vy = sy.integrate(vy(x), (x, 0, 6))

n = 200

fig = plt.figure(figsize=(14, 7))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222, projection='3d')
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224, projection='3d')

y = np.linspace(0, 6, n)
x1 = (y / 6)
x2 = (y / 6) ** (1 / 2)
t = np.linspace(0, np.pi * 2, n)

u = np.linspace(0, 1, n)
v = np.linspace(0, 2 * np.pi, n)
U, V = np.meshgrid(u, v)

X = U
Y1 = (6 * U ** 2) * np.cos(V)
Z1 = (6 * U ** 2) * np.sin(V)

Y2 = (6 * U) * np.cos(V)
Z2 = (6 * U) * np.sin(V)

Y3 = ((1 / 6) * U ** (1 / 2)) * np.cos(V)
Z3 = ((1 / 6) * U ** (1 / 2)) * np.sin(V)

Y4 = (U / 6) * np.cos(V)
Z4 = (U / 6) * np.sin(V)

ax1.plot(x1, y, label='$y=6x$')
ax1.plot(x2, y, label='$y=6x^{2}$')
ax1.legend()

ax1.set_title('$f(x)$')
ax2.plot_surface(X, Y3, Z3, alpha=0.3, color='red', rstride=6, cstride=12)
ax2.plot_surface(X, Y4, Z4, alpha=0.3, color='blue', rstride=6, cstride=12)
ax2.set_title("$f(x)$: Revolution around $y$ \n Volume = {}".format(vy))

# find the inverse of the function
x_inverse = np.linspace(0, 6, n)
y1_inverse = np.power(x_inverse/6, 1)
y2_inverse = np.power(x_inverse/6, 1 / 2)

ax3.plot(x_inverse, y1_inverse, label='Inverse of $y=6x$')
ax3.plot(x_inverse, y2_inverse, label='Inverse of $y=6x^{2}$')
ax3.set_title('Inverse of $f(x)$')
ax3.legend()

ax4.plot_surface(X, Y1, Z1, alpha=0.3, color='red', rstride=6, cstride=12)
ax4.plot_surface(X, Y2, Z2, alpha=0.3, color='blue', rstride=6, cstride=12)
ax4.set_title("$f(x)$: Revolution around $x$ \n Volume = {}".format(vx))

plt.tight_layout()
plt.show()


"""

If anyone wants to improve how to call Python code from Julia, or give me advice. You are all welcome