Ask Your Question

How to import implicit_plot3d into .py file?

asked 2020-03-14 18:28:26 +0200

RyanG gravatar image

updated 2020-03-14 18:43:15 +0200

I'm working on some code for creating 3D molecular structures and everything seems to be working well aside from the fact that I have no idea how to import the 'implicit_plot3d' function into my script. I want to run the code as '' directly inside of sage. When I try to run it right now I get the following error:

NameError: name 'implicit_plot3d' is not defined

Can anyone help me identify the issue? I'm not very experienced with either Python or Sage.

import sys
from sage.plot.plot3d.shapes import LineSegment, Sphere
from sage.all import rainbow

BC = ['white','black','blue','red','grey','red','pink','cyan']

Radii = {'O': 0.73, 'N': 0.75, 'C': 0.77, 'He': 0.32, 'H': 0.37, 'S': 1.02, 'Cl': 0.99, 
'F': 0.71, 'Xe': 1.30, 'Si': 1.11, 'B': 0.82, 'P': 1.06, 'Br': 1.14}

Valency = {'O': 6, 'N': 5, 'C': 4, 'He': 0, 'H': 1, 'S': 6, 'Cl': 7, 
'F': 7, 'Xe': 8, 'Si': 4, 'B': 3, 'P': 5, 'Br': 7}

Colors = {'O': 'red', 'N': 'green', 'C': 'black', 'He': 'cyan', 'H': 'white', 'S': 'yellow',
'Si': 'purple', 'Xe': 'pink', 'F': 'blue', 'Cl': 'green', 'B': 'pink', 'P': 'orange',
'Br': 'red'}

Here's a sample function

def bent_geometry():

    x = input("Enter bent molecule A: ")
    y = input("Enter bent molecule B: ")
    X =(0,0,0)
    Y1 = (0, 0.75, -0.70)
    Y2 = (0, -0.75,-0.70)

    C2_a = (0, 0, -1.5)
    C2_b = (0, 0, 1.5)

    XY2  = Sphere(Normalize(Radii[x]), color=Colors[x]).translate(X)
    XY2 += Sphere(Normalize(Radii[y]), color=Colors[y]).translate(Y1)
    XY2 += Sphere(Normalize(Radii[y]), color=Colors[y]).translate(Y2)

    XY2 += LineSegment(X, Y1, 1, color=BC[0], axes=False, frame=False)
    XY2 += LineSegment(X, Y2, 1, color=BC[0], axes=False, frame=False)
    XY2 += LineSegment(C2_a, C2_b, 1, color = BC[2], axes=True, frame=True)

    XY2 += implicit_plot3d(lambda x,y,z: x, (-1,1), (-1,1), (-1,1),color='cyan',opacity=0.8)
    XY2 += implicit_plot3d(lambda x,y,z: y, (-1,1), (-1,1), (-1,1),color='white',opacity=0.8)

    XY2 += text3d('C2',(0,0,1.575))
    return XY2
edit retag flag offensive close merge delete


I assume Normalize is a function you've defined?

John Palmieri gravatar imageJohn Palmieri ( 2020-03-14 18:48:53 +0200 )edit

And the command "pyflakes" (in the shell) helps you to find what names are still missing in your py file.

FrédéricC gravatar imageFrédéricC ( 2020-03-15 07:54:43 +0200 )edit

1 Answer

Sort by » oldest newest most voted

answered 2020-03-14 18:48:17 +0200

You're already importing LineSegment and Sphere, but you need to import implicit_plot3d and text3d. From where? Use the Sage function import_statements:

sage: import_statements('implicit_plot3d')
from sage.plot.plot3d.implicit_plot3d import implicit_plot3d
sage: import_statements('text3d')
from sage.plot.plot3d.shapes2 import text3d

and while we're at it, importing from sage.all is not ideal:

sage: import_statements('rainbow')
from sage.plot.colors import rainbow

So your import statements should be

from sage.plot.plot3d.shapes import LineSegment, Sphere
from sage.plot.colors import rainbow
from sage.plot.plot3d.implicit_plot3d import implicit_plot3d
from sage.plot.plot3d.shapes2 import text3d
edit flag offensive delete link more


Yes, Normalize adjusts all the radii relative to a fixed size for carbon. Thank you. This is exactly what I was looking for.

RyanG gravatar imageRyanG ( 2020-03-14 18:52:36 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2020-03-14 18:28:26 +0200

Seen: 399 times

Last updated: Mar 14 '20