Skip to content

Commit fa9ccbb

Browse files
author
Onur Rauf Bingol
committed
Use the new rendering API that comes with Plotly v4 orbingol#90
1 parent ac162e1 commit fa9ccbb

File tree

1 file changed

+17
-96
lines changed

1 file changed

+17
-96
lines changed

geomdl/visualization/VisPlotly.py

Lines changed: 17 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,8 @@
99

1010
from . import vis
1111
import numpy as np
12-
import plotly
1312
from plotly import graph_objs
1413

15-
# Migrate to Plotly v4.x
16-
try:
17-
# Online plotting functionality is moved to chart-studio package
18-
from chart_studio.plotly import plot, iplot
19-
except ImportError:
20-
# Load offline plotting functionality in case of an import error
21-
from plotly.offline import plot, iplot
22-
2314

2415
class VisConfig(vis.VisConfigAbstract):
2516
""" Configuration class for Plotly visualization module.
@@ -68,24 +59,7 @@ def __init__(self, **kwargs):
6859
super(VisConfig, self).__init__(**kwargs)
6960
self.dtype = np.float
7061
# Set Plotly custom variables
71-
self.figure_image_filename = "temp-figure"
72-
self.figure_image_format = "png"
73-
self.figure_filename = "temp-plot.html"
74-
75-
# Enable online plotting (default is offline plotting as it works perfectly without any issues)
76-
# @see: https://plot.ly/python/getting-started/#initialization-for-online-plotting
77-
online_plotting = kwargs.get('online', False)
78-
79-
# Detect jupyter and/or ipython environment
80-
try:
81-
get_ipython
82-
from plotly.offline import download_plotlyjs, init_notebook_mode
83-
init_notebook_mode(connected=True)
84-
self.plotfn = iplot if online_plotting else plotly.offline.iplot
85-
self.no_ipython = False
86-
except NameError:
87-
self.plotfn = plot if online_plotting else plotly.offline.plot
88-
self.no_ipython = True
62+
self.figure_image_filename = "temp-plot.html"
8963

9064
# Get keyword arguments
9165
self.display_ctrlpts = kwargs.get('ctrlpts', True)
@@ -203,22 +177,11 @@ def render(self, **kwargs):
203177
fig_filename = kwargs.get('fig_save_as', None)
204178
fig_display = kwargs.get('display_plot', True)
205179

206-
# Prepare plot configuration
207-
plotfn_dict = {
208-
'show_link': False,
209-
'filename': self.vconf.figure_filename,
210-
'image': None if fig_display else self.vconf.figure_image_format,
211-
}
212-
if self.vconf.no_ipython:
213-
plotfn_dict_extra = {
214-
'image_filename': self.vconf.figure_image_filename if fig_filename is None else fig_filename,
215-
'auto_open': fig_display,
216-
}
217-
# Python < 3.5 does not support starred expressions inside dicts
218-
plotfn_dict.update(plotfn_dict_extra)
219-
220180
# Display the plot
221-
self.vconf.plotfn(fig, **plotfn_dict)
181+
if fig_display:
182+
fig.write_html(file=self.vconf.figure_image_filename if fig_filename is None else fig_filename)
183+
else:
184+
fig.write_image(file=self.vconf.figure_image_filename if fig_filename is None else fig_filename)
222185

223186

224187
class VisCurve3D(vis.VisAbstract):
@@ -347,25 +310,11 @@ def render(self, **kwargs):
347310
fig_filename = kwargs.get('fig_save_as', None)
348311
fig_display = kwargs.get('display_plot', True)
349312

350-
# Prepare plot configuration
351-
plotfn_dict = {
352-
'show_link': False,
353-
'filename': self.vconf.figure_filename,
354-
'image': None if fig_display else self.vconf.figure_image_format,
355-
}
356-
if self.vconf.no_ipython:
357-
plotfn_dict_extra = {
358-
'image_filename': self.vconf.figure_image_filename if fig_filename is None else fig_filename,
359-
'auto_open': fig_display,
360-
}
361-
# Python < 3.5 does not support starred expressions inside dicts
362-
plotfn_dict.update(plotfn_dict_extra)
363-
364313
# Display the plot
365-
self.vconf.plotfn(fig, **plotfn_dict)
366-
367-
# Return the figure object
368-
return fig
314+
if fig_display:
315+
fig.write_html(file=self.vconf.figure_image_filename if fig_filename is None else fig_filename)
316+
else:
317+
fig.write_image(file=self.vconf.figure_image_filename if fig_filename is None else fig_filename)
369318

370319

371320
class VisSurface(vis.VisAbstract):
@@ -519,25 +468,11 @@ def render(self, **kwargs):
519468
fig_filename = kwargs.get('fig_save_as', None)
520469
fig_display = kwargs.get('display_plot', True)
521470

522-
# Prepare plot configuration
523-
plotfn_dict = {
524-
'show_link': False,
525-
'filename': self.vconf.figure_filename,
526-
'image': None if fig_display else self.vconf.figure_image_format,
527-
}
528-
if self.vconf.no_ipython:
529-
plotfn_dict_extra = {
530-
'image_filename': self.vconf.figure_image_filename if fig_filename is None else fig_filename,
531-
'auto_open': fig_display,
532-
}
533-
# Python < 3.5 does not support starred expressions inside dicts
534-
plotfn_dict.update(plotfn_dict_extra)
535-
536471
# Display the plot
537-
self.vconf.plotfn(fig, **plotfn_dict)
538-
539-
# Return the figure object
540-
return fig
472+
if fig_display:
473+
fig.write_html(file=self.vconf.figure_image_filename if fig_filename is None else fig_filename)
474+
else:
475+
fig.write_image(file=self.vconf.figure_image_filename if fig_filename is None else fig_filename)
541476

542477

543478
class VisVolume(vis.VisAbstract):
@@ -662,22 +597,8 @@ def render(self, **kwargs):
662597
fig_filename = kwargs.get('fig_save_as', None)
663598
fig_display = kwargs.get('display_plot', True)
664599

665-
# Prepare plot configuration
666-
plotfn_dict = {
667-
'show_link': False,
668-
'filename': self.vconf.figure_filename,
669-
'image': None if fig_display else self.vconf.figure_image_format,
670-
}
671-
if self.vconf.no_ipython:
672-
plotfn_dict_extra = {
673-
'image_filename': self.vconf.figure_image_filename if fig_filename is None else fig_filename,
674-
'auto_open': fig_display,
675-
}
676-
# Python < 3.5 does not support starred expressions inside dicts
677-
plotfn_dict.update(plotfn_dict_extra)
678-
679600
# Display the plot
680-
self.vconf.plotfn(fig, **plotfn_dict)
681-
682-
# Return the figure object
683-
return fig
601+
if fig_display:
602+
fig.write_html(file=self.vconf.figure_image_filename if fig_filename is None else fig_filename)
603+
else:
604+
fig.write_image(file=self.vconf.figure_image_filename if fig_filename is None else fig_filename)

0 commit comments

Comments
 (0)