|
9 | 9 |
|
10 | 10 | from . import vis
|
11 | 11 | import numpy as np
|
12 |
| -import plotly |
13 | 12 | from plotly import graph_objs
|
14 | 13 |
|
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 |
| - |
23 | 14 |
|
24 | 15 | class VisConfig(vis.VisConfigAbstract):
|
25 | 16 | """ Configuration class for Plotly visualization module.
|
@@ -68,24 +59,7 @@ def __init__(self, **kwargs):
|
68 | 59 | super(VisConfig, self).__init__(**kwargs)
|
69 | 60 | self.dtype = np.float
|
70 | 61 | # 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" |
89 | 63 |
|
90 | 64 | # Get keyword arguments
|
91 | 65 | self.display_ctrlpts = kwargs.get('ctrlpts', True)
|
@@ -203,22 +177,11 @@ def render(self, **kwargs):
|
203 | 177 | fig_filename = kwargs.get('fig_save_as', None)
|
204 | 178 | fig_display = kwargs.get('display_plot', True)
|
205 | 179 |
|
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 |
| - |
220 | 180 | # 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) |
222 | 185 |
|
223 | 186 |
|
224 | 187 | class VisCurve3D(vis.VisAbstract):
|
@@ -347,25 +310,11 @@ def render(self, **kwargs):
|
347 | 310 | fig_filename = kwargs.get('fig_save_as', None)
|
348 | 311 | fig_display = kwargs.get('display_plot', True)
|
349 | 312 |
|
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 |
| - |
364 | 313 | # 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) |
369 | 318 |
|
370 | 319 |
|
371 | 320 | class VisSurface(vis.VisAbstract):
|
@@ -519,25 +468,11 @@ def render(self, **kwargs):
|
519 | 468 | fig_filename = kwargs.get('fig_save_as', None)
|
520 | 469 | fig_display = kwargs.get('display_plot', True)
|
521 | 470 |
|
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 |
| - |
536 | 471 | # 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) |
541 | 476 |
|
542 | 477 |
|
543 | 478 | class VisVolume(vis.VisAbstract):
|
@@ -662,22 +597,8 @@ def render(self, **kwargs):
|
662 | 597 | fig_filename = kwargs.get('fig_save_as', None)
|
663 | 598 | fig_display = kwargs.get('display_plot', True)
|
664 | 599 |
|
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 |
| - |
679 | 600 | # 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