Skip to content

Instantly share code, notes, and snippets.

@fomightez
Last active August 13, 2025 16:31
Show Gist options
  • Save fomightez/036b320e317015beb7f030f2ec36a7fe to your computer and use it in GitHub Desktop.
Save fomightez/036b320e317015beb7f030f2ec36a7fe to your computer and use it in GitHub Desktop.
3D surface plots working in SageMath 10.7 kernel in Jupyter August 2025

Pertinent Stackoverflow post with SageMath code that plots 3D surfaces using SageMath 10.7 kernel in Jupyter at present: https://stackoverflow.com/q/79733626/8508004
See my comments there on how to run the demonstration.





But in case that goes away here is the code that worked and what I said to run demo of it working at the time....

OP-provided code block #1:

x, y = var('x y')

W = plot3d(sin(pi*((x)^2 + (y)^2))/2, (x, -1, 1), (y, -1, 1),

        frame=False, color='purple', opacity=0.8)

S = sphere((0, 0, 0), size=0.3, color='red', aspect_ratio=[1,1,1])

show(W + S, figsize=8)

OP-provided code block #2:

f(x, y) = sin(x*y)

G = plot3d(f, (x, -2, 2), (y, -2, 2))

G.save("graphic3d.html", viewer='threejs', online=True)

print("Graphic saved as graphic3d.html.")

My reply as comments text: Both of these examples work fine with SageMath 10.7 running in JupyterLab on a remote, virtual machine on the web. You should be able to see this in action by going here where the SageMath folks offer a recipe for running this in sessions served by the MyBinder. Click the 'launch binder ' badge you'll see there. That will bring up a new Jupyter session. On the launcher pane click on the SageMath 10.7 tile under 'Notebook' & you'll get a new Jupyter .ipynb file opening with the SageMath 10.7 kernel. Then paste in one code cell the first code block you provide and run it. After a number of seconds of processing, it should work and display the 3D plot & you should be able to click on the plot and twirl tit around. In another code cell, do the same with the second code block you provided and then in the file browser pane on the left, double click on the produced file graphic3d.html and it will open the HTML in a viewer in JupyterLab. At present, you won't see the plot though . You first need to click on the text 'Trust HTML' in the upper left side of the HTML viewer pane to tell the viewer to proceed. Then it should display & you can click on the plot to drag around the plot interactively.


Other comment by me on that page at present:
Since you bring up Jupyter, I added a series of detailed comments below the original post on how one could try it in Jupyter without installing anything on your own machine or creating any account. Plus, because the Sage folks provided the recipe defining the virtual machine that will launch, it works without needing to concern what libraries are available. It appears using the SageMath 10.7 kernel at present no additional code/magic commands in the cell are needed to make that provided example code work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment