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.
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.
|
|
PythonContext objects allow us to run Python code one line at a time, just like working in the Python REPL.
|
|
|
|
|
|
|
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".
|
|
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.
|
|
|
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.
|
Finally, we show our plot.
|
All three of the above methods should have resulted in a window appearing containing the following image.
The source of this document is in Python/doc/tutorials.m2:288:0.