Skip to content

Instantly share code, notes, and snippets.

@BielStela
Last active December 21, 2017 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BielStela/b0e8e1d842db5dcf2c09bdc23bbd1736 to your computer and use it in GitHub Desktop.
Save BielStela/b0e8e1d842db5dcf2c09bdc23bbd1736 to your computer and use it in GitHub Desktop.
Show the molecular 2D structure in the hover tool of a Bokeh plot
from bokeh.plotting import figure, output_notebook, show
from bokeh.models import ColumnDataSource, HoverTool, CrosshairTool, WheelZoomTool
from bokeh.models import ColorBar, ResetTool, PanTool, LinearColorMapper
import bokeh.palettes as palettes
import matplotlib as mpl
output_notebook()
def get_structures(mol_df):
#mol_df["img_path"] = ""
img_path = []
for mol in range(len(mol_df)):
Draw.MolToFile(mol_df.ROMol[mol],
"static/imgs/"+mol_df.name[mol]+".svg",
imageType="svg",
fitImage=False,
size=(200, 200))
#mol_df["img_path"][mol] = "static/imgs/"+mol_df.name[mol]+".svg"
img_path.append("static/imgs/"+mol_df.name[mol]+".svg")
mol_df["img_path"] = img_path
return mol_df
def interactiveplot(df):
source = ColumnDataSource(mols_pr)
source.data['HCPSA'] = source.data['HCPSA']/6
color_maper = LinearColorMapper(palette='Viridis256', low=min(mols_pr.activity), high=max(mols_pr.activity))
#preparing the hover tooltip with HTML template
hover = HoverTool(tooltips = """
<div>
<img src="@img_path" width="170" height="170"></img>
</div>
<div>
<span style="font-size: 12px; font-weight: bold;">@name</span>
</div>
<div>
<span style="font-size: 12px; font-weight: bold;">x,y:</span>
<span style="font-size: 12px;">@x, @y</span>
</div>
"""
)
TOOLS=[hover, CrosshairTool(), WheelZoomTool(), ResetTool(), PanTool()]
p = figure(plot_width=800, plot_height=800,title="Mouse over the dots", tools=TOOLS, toolbar_location="above")
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None
p.scatter('x','y',size='HCPSA',alpha = 0.7,color='activity_color',source = source)
color_bar = ColorBar(color_mapper=color_maper, label_standoff=12,
border_line_color=None, location=(0,0))
p.add_layout(color_bar, 'right')
#output_file("test_mol.html")
show(p)
@BielStela
Copy link
Author

PD: Run it in a Jupyter Notebook

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