Macaulay2 » Documentation
Packages » Python » Python tutorial: plotting the twisted cubic with Matplotlib
next | previous | forward | backward | up | index | toc

Python tutorial: plotting the twisted cubic with Matplotlib

In this tutorial, we use Matplotlib to plot the twisted cubic in three different ways. We are assuming that Matplotlib is already installed. See pipInstall to learn how to install Python modules.

This example is heavily inspired by the Parametric curve example in the matplotlib documentation.

Running Python code directly

If we just want to run some Python code directly, then the simplest way is with the pythonRunScript method.

For this example, we have our code saved as a .py file.

i1 : pycode = get(Python#"auxiliary files" | "doc/matplotlib-example.py")

o1 = import matplotlib.pyplot as plt
     import numpy as np

     fig = plt.figure()
     ax = fig.add_subplot(projection='3d')
     t = np.linspace(-10, 10, 100)
     ax.plot(t, t**2, t**3)
     plt.show()
i2 : pythonRunScript pycode

Using a PythonContext

PythonContext objects allow us to run Python code one line at a time, just like working in the Python REPL.

i3 : matplotlib = PythonContext "import matplotlib.pyplot as plt"

o3 = matplotlib

o3 : PythonContext
i4 : matplotlib "import numpy as np"
i5 : matplotlib "fig = plt.figure()"
i6 : matplotlib "ax = fig.add_subplot(projection='3d')"
i7 : matplotlib "t = np.linspace(-10, 10, 100)"
i8 : matplotlib "ax.plot(t, t**2, t**3)"

o8 = [<mpl_toolkits.mplot3d.art3d.Line3D object at 0x7b8243b7e5f0>]

o8 : PythonObject of class list
i9 : matplotlib "plt.show()"

o9 = None

o9 : PythonObject of class NoneType

Using PythonObjects directly

Finally, we can write our code in the Macaulay2 language, but making calls to Python as needed.

First, we import the necessary modules using import. Note that we can essentially replace the Python import foo as bar with bar = import "foo".

i10 : plt = import "matplotlib.pyplot"

o10 = <module 'matplotlib.pyplot' from
      '/usr/lib/python3/dist-packages/matplotlib/pyplot.py'>

o10 : PythonObject of class module
i11 : np = import "numpy"

o11 = <module 'numpy' from '/usr/lib/python3/dist-packages/numpy/__init__.py'>

o11 : PythonObject of class module

Next, we begin to create the various Python objects needed for our plot.

Note that we replace the Python foo.bar with foo@@bar (see PythonObject @@ Thing). We need to be careful for attributes that include underscores. They must given as strings, i.e., delimited using quotes.

i12 : fig = plt@@figure()

o12 = Figure(640x480)

o12 : PythonObject of class matplotlib.figure.Figure
i13 : ax = fig@@"add_subplot"(projection => "3d")

o13 = Axes3DSubplot(0.125,0.11;0.775x0.77)

o13 : PythonObject of class matplotlib.axes._subplots.Axes3DSubplot
i14 : t = np@@linspace(-10, 10, 100);

Now we construct the twisted cubic. Note that even though Python itself uses ** for exponentiation, we may use ^ for consistency with the rest of Macaulay2.

i15 : ax@@plot(t, t^2, t^3)

o15 = [<mpl_toolkits.mplot3d.art3d.Line3D object at 0x73a51970eb00>]

o15 : PythonObject of class list

Finally, we show our plot.

i16 : plt@@show()

All three of the above methods should have resulted in a window appearing containing the following image.

parametric plot of the twisted cubic


The source of this document is in Python/doc/tutorials.m2:288:0.