Search code examples
pythonpython-3.xgeometrygeometry-surface

How to plot pseudosphere surface in python


The objective is to generate a three-dimensional figure of a pseudosphere using meshgrid and numpy library, but the figure I generate using the code below is incomplete

u = np.linspace(0, np.pi, 50)
v = np.linspace(0, 2*np.pi, 100)
x, y = np.meshgrid(u, v)

X = np.arccos(x)*np.cos(y)
Y = np.arccos(x)*np.sin(y)
Z = x-np.tan(x)

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

surf = ax.plot_surface(X, Y, Z, cmap='viridis')

enter image description here

The expect to generate a figure like

enter image description here


Solution

  • You are muddling up trigonometric / inverse trigonometric /hyperbolic functions. In particular, arccos (inverse cos) is not sech (1/cosh, where cosh is hyperbolic cosine). Similarly, tan and tanh are completely different functions.

    The closest I could get to your original code is equations (1) to (3) in https://mathworld.wolfram.com/Pseudosphere.html

    import numpy as np
    import matplotlib.pyplot as plt
    
    u = np.linspace (-5, 5, 100 )
    v = np.linspace( 0, 2 * np.pi, 100 )
    x, y = np.meshgrid( u, v )
    
    X = 1.0 / np.cosh( x ) * np.cos( y )
    Y = 1.0 / np.cosh( x ) * np.sin( y )
    Z = x - np.tanh( x )
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    surf = ax.plot_surface(X, Y, Z, cmap='viridis')
    plt.show()
    

    enter image description here