mobius strip plot in python

How to Plot a Mobius Strip in Python?

In this tutorial, we’ll try to understand how to plot a Mobius strip in Python using the matplotlib library.

What is a Mobius Strip?

A Möbius strip, Möbius band, or Möbius loop is a surface that can be formed by attaching the ends of a strip of paper together with a half-twist.

We can plot a Mobius strip (or) Mobius triangulation using the matplotlib Axes.plot_trisurf method.

Using the matpltolib.Axes.plot_trisurf() method

To plot a Mobius strip (or) Mobius triangulation, use the matplotlib.Axes.plot_trisurf() method. This method is used to plot a triangulation plot. The following is the syntax –

BasicSyntax:

Axes.plot_trisurf(*args, color=None, norm=None, vmin=None, vmax=None, lightsource=None, **kwargs)

The (optional) triangulation can be specified in one of two ways; either:

plot_trisurf(triangulation, ...)

where triangulation is a Triangulation object, or:

plot_trisurf(X, Y, ...)
plot_trisurf(X, Y, triangles, ...)
plot_trisurf(X, Y, triangles=triangles, ...)

Parameters:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

  • X, Y, Z: Data values as 1D arrays.
  • color: Color of the surface patches.

For more details about the parameters refer this.

Now let us understand the above method with some examples.

Example 1

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as mtri

# Creating figure and 3D axes
fig = plt.figure(figsize =(9, 5))
ax = plt.axes(projection ='3d')

a = np.linspace(0, 2.0 * np.pi,30)
b = np.linspace(-0.5, 0.5, 5)
a, b = np.meshgrid(a, b)
a, b = a.flatten(), b.flatten()

# This is the Mobius mapping, taking a, b pair and returning an x, y, z
x = (1 + 0.5 * b * np.cos(a / 2.0)) * np.cos(a)
y = (1 + 0.5 * b * np.cos(a / 2.0)) * np.sin(a)
z = 0.5 * b * np.sin(a / 2.0)

# Triangulate parameter space to determine the triangles
tri = mtri.Triangulation(a, b)

#Plotting the Mobius strip (or) Mobius triangulation
ax.plot_trisurf(x, y, z, triangles=tri.triangles)
plt.show()

Output:

the resulting plot with a mobius strip

The steps followed in the above example are:

  • Import the required modules
  • Create the figure and 3D axes
  • Define the variables to plot.
    • List a and b contains 30 & 5 evenly spaced number generated using numpy.linspace().
    • Then create a mesh grid of a, and b using numpy.meshgrid().
    • List x,y and z are the outer product of the vectors ((1+(bcos(a/2)/2)cosa) , ((1+(bcos(a/2)/2)sina) and (bcos(a/2)/2) generated
  • Plot the Mobius strip (or) Mobius triangulation

Example 2

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as mtri

# Creating figure and 3D axes
fig = plt.figure(figsize =(9, 5))
ax = plt.axes(projection ='3d')

a = np.linspace(0, 2.0 * np.pi,30)
b = np.linspace(-0.5, 0.5, 5)
a, b = np.meshgrid(a, b)
a, b = a.flatten(), b.flatten()

# This is the Mobius mapping, taking a, b pair and returning an x, y, z
x = (1 + 0.5 * b * np.cos(a / 2.0)) * np.cos(a)
y = (1 + 0.5 * b * np.cos(a / 2.0)) * np.sin(a)
z = 0.5 * b * np.sin(a / 2.0)

# Triangulate parameter space to determine the triangles
tri = mtri.Triangulation(a, b)

#Plotting the Mobius strip (or) Mobius triangulation
ax.plot_trisurf(x, y, z, triangles=tri.triangles,linewidth=2, cmap=plt.cm.viridis)
plt.show()

Output:

the resulting plot with a mobius strip

The steps followed in the above example are:

  • Import the required modules
  • Create the figure and 3D axes
  • Define the variables to plot.
    • List a and b contains 30 & 5 evenly spaced number generated using numpy.linspace().
    • Then create a mesh grid of a, and b using numpy.meshgrid().
    • List x, y, and z is the outer product of the vectors ((1+(bcos(a/2)/2)cosa) , ((1+(bcos(a/2)/2)sina) and (bcos(a/2)/2) generated.
  • Plot the Mobius strip (or) Mobius triangulation

Example 3

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as mtri

# Creating figure and 3D axes
fig = plt.figure(figsize =(9, 5))
ax = plt.axes(projection ='3d')

#defining parameters
n_angles = 36
n_radii = 8
min_radius = 0.25
radii = np.linspace(min_radius, 0.95, n_radii)

angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] += np.pi/n_angles

# Map radius, angle pairs to x, y, z points.
x = (radii*np.cos(angles)).flatten()
y = (radii*np.sin(angles)).flatten()
z = (np.cos(radii)*np.cos(3*angles)).flatten()

# Create the Triangulation; no triangles so Delaunay triangulation created.
triang = mtri.Triangulation(x, y)

# Mask off unwanted triangles.
xmid = x[triang.triangles].mean(axis=1)
ymid = y[triang.triangles].mean(axis=1)
mask = xmid**2 + ymid**2 < min_radius**2
triang.set_mask(mask)
# Plotting the Mobius strip (or) Mobius triangulation
ax.plot_trisurf(triang, z, cmap=plt.cm.CMRmap)
plt.show()

Output:

the resulting plot with a mobius strip

The steps followed in the above example are:

  • Import the required modules
  • Create the figure and 3D axes
  • Define the variables to plot
  • Plot the Mobius strip (or) Mobius triangulation

Example 4

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.tri import Triangulation

# Creating figure and 3D axes
fig = plt.figure(figsize =(9, 5))
ax = plt.axes(projection ='3d')

#defining parameters
theta = np.linspace(0, 2 * np.pi, 30)
w = np.linspace(-0.25, 0.25, 8)
w, theta = np.meshgrid(w, theta)

phi = 0.5 * theta

# radius in x-y plane
r = 1 + w * np.cos(phi)

x = np.ravel(r * np.cos(theta))
y = np.ravel(r * np.sin(theta))
z = np.ravel(w * np.sin(phi))

tri = Triangulation(np.ravel(w), np.ravel(theta))

# Plotting the Mobius strip (or) Mobius triangulation
ax.plot_trisurf(x, y, z, triangles=tri.triangles,
                cmap='viridis', linewidths=0.2)

ax.set_xlim(-1, 1); ax.set_ylim(-1, 1); ax.set_zlim(-1, 1);

plt.show()

Output:

the resulting plot with a mobius strip

The steps followed in the above example are:

  • Import the required modules
  • Create the figure and 3D axes
  • Define the variables to plot
    • List θ and w contain 30 & 8 evenly spaced numbers generated using numpy.linspace().
    • Then create a mesh grid of θ,w using numpy.meshgrid().
    • Generate the phi value where ϕ=θ/2
    • Generate radius r values where r=1+wcosϕ.
    • List the x, y, and z values where x=rcosθ, y = rsinθ & z = wsinϕ using numpy.ravel.
    • Then generate the triangles using matplotlib.tri.Triangulation.
  • Plot the Mobius strip (or) Mobius triangulation

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Chaitanya Betha

    I'm an undergrad student at IIT Madras interested in exploring new technologies. I have worked on various projects related to Data science, Machine learning & Neural Networks, including image classification using Convolutional Neural Networks, Stock prediction using Recurrent Neural Networks, and many more machine learning model training. I write blog articles in which I would try to provide a complete guide on a particular topic and try to cover as many different examples as possible with all the edge cases to understand the topic better and have a complete glance over the topic.

Scroll to Top