Skip to content

Commit 8597724

Browse files
authored
Fixes the use of notebook argument for plot commands (#259)
* Fixes the use of notebook argument for plot commands going through Plotter.plot_contour * Fixing kwargs in plot_contour for call to pv.Plotter.show with cpos_return * Update the example to show "notebook=False" is required for screenshots on a Jupyter notebook. * Fix an error with pv.Plotter.__init__ * Fix support for cpos_return
1 parent 0495078 commit 8597724

File tree

4 files changed

+24
-58
lines changed

4 files changed

+24
-58
lines changed

ansys/dpf/core/field.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def to_nodal(self):
237237
op.inputs.connect(self)
238238
return op.outputs.field()
239239

240-
def plot(self, notebook=None, shell_layers=None, **kwargs):
240+
def plot(self, shell_layers=None, **kwargs):
241241
"""Plot the field or fields container on the mesh support if it exists.
242242
243243
Warning
@@ -259,10 +259,6 @@ def plot(self, notebook=None, shell_layers=None, **kwargs):
259259
260260
Parameters
261261
----------
262-
notebook : bool, optional
263-
Whether the plotting is in the notebook as
264-
a static image or is a dynamic plot outside of the
265-
notebook. The default is ``None``.
266262
shell_layers : shell_layers, optional
267263
Enum used to set the shell layers if the model to plot
268264
contains shell elements. The default is ``None``.
@@ -271,7 +267,7 @@ def plot(self, notebook=None, shell_layers=None, **kwargs):
271267
arguments, see ``help(pyvista.plot)``.
272268
"""
273269
pl = Plotter(self.meshed_region, **kwargs)
274-
return pl.plot_contour(self, notebook, shell_layers, **kwargs)
270+
return pl.plot_contour(self, shell_layers, **kwargs)
275271

276272
def resize(self, nentities, datasize):
277273
"""Allocate memory.

ansys/dpf/core/meshed_region.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,7 @@ def grid(self):
368368
def plot(
369369
self,
370370
field_or_fields_container=None,
371-
notebook=None,
372371
shell_layers=None,
373-
off_screen=None,
374-
show_axes=True,
375372
**kwargs
376373
):
377374
"""Plot the field or fields container on the mesh.
@@ -380,16 +377,8 @@ def plot(
380377
----------
381378
field_or_fields_container : dpf.core.Field or dpf.core.FieldsContainer
382379
Field or fields container to plot. The default is ``None``.
383-
notebook : bool, optional
384-
Whether the plotting in the notebook is 2D or 3D. The default is
385-
``None``, in which case the plotting is 2D.
386380
shell_layers : core.shell_layers, optional
387381
Enum used to set the shell layers if the model to plot contains shell elements.
388-
off_screen : bool, optional
389-
Whether to render the plot off screen, which is useful for automated screenshots.
390-
The default is "None", in which case the plot renders off screen.
391-
show_axes : bool, optional
392-
Whether to show a VTK axes widget. The default is ``True``.
393382
**kwargs : optional
394383
Additional keyword arguments for the plotter. For additional keyword
395384
arguments, see ``help(pyvista.plot)``.
@@ -408,15 +397,11 @@ def plot(
408397
"""
409398
if field_or_fields_container is not None:
410399
pl = Plotter(self, **kwargs)
411-
return pl.plot_contour(field_or_fields_container, notebook,
412-
shell_layers, off_screen, show_axes, **kwargs)
400+
return pl.plot_contour(field_or_fields_container, shell_layers, **kwargs)
413401

414402
# otherwise, simply plot the mesh
415-
kwargs["off_screen"] = off_screen
416-
kwargs["notebook"] = notebook
417403
pl = DpfPlotter(**kwargs)
418404
pl.add_mesh(self, **kwargs)
419-
kwargs["show_axes"] = show_axes
420405
return pl.show_figure(**kwargs)
421406

422407
def deep_copy(self, server=None):

ansys/dpf/core/plotter.py

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def __init__(self, **kwargs):
6767
)
6868
# Filter kwargs
6969
kwargs_in = _sort_supported_kwargs(
70-
bound_method=pv.Plotter,
70+
bound_method=pv.Plotter.__init__,
7171
**kwargs)
7272
# Initiate pyvista Plotter
7373
self._plotter = pv.Plotter(**kwargs_in)
@@ -601,10 +601,7 @@ def plot_chart(fields_container, off_screen=False, screenshot=None):
601601
def plot_contour(
602602
self,
603603
field_or_fields_container,
604-
notebook=None,
605604
shell_layers=None,
606-
off_screen=None,
607-
show_axes=True,
608605
meshed_region=None,
609606
**kwargs
610607
):
@@ -617,20 +614,9 @@ def plot_contour(
617614
----------
618615
field_or_fields_container : dpf.core.Field or dpf.core.FieldsContainer
619616
Field or field container that contains the result to plot.
620-
notebook : bool, optional
621-
Whether to plot a static image within an iPython notebook
622-
if available. The default is `None`, in which case an attempt is
623-
made to plot a static imaage within an iPython notebook. When ``False``,
624-
a plot external to the notebook is generated with an interactive window.
625-
When ``True``, a plot is always generated within a notebook.
626617
shell_layers : core.shell_layers, optional
627618
Enum used to set the shell layers if the model to plot
628619
contains shell elements.
629-
off_screen : bool, optional
630-
Whether to render off screen, which is useful for automated
631-
screenshots. The default is ``None``.
632-
show_axes : bool, optional
633-
Whether to show a VTK axes widget. The default is ``True``.
634620
**kwargs : optional
635621
Additional keyword arguments for the plotter. For more information,
636622
see ``help(pyvista.plot)``.
@@ -726,51 +712,44 @@ def plot_contour(
726712
overall_data[ind] = field.data[mask]
727713

728714
# create the plotter and add the meshes
729-
background = kwargs.pop("background", None)
730-
cpos = kwargs.pop("cpos", None)
731-
return_cpos = kwargs.pop("return_cpos", None)
732-
733-
# plotter = pv.Plotter(notebook=notebook, off_screen=off_screen)
734-
if notebook is not None:
735-
self._internal_plotter._plotter.notebook = notebook
736-
if off_screen is not None:
737-
self._internal_plotter._plotter.off_screen = off_screen
738715

739716
# add meshes
740717
kwargs.setdefault("show_edges", True)
741718
kwargs.setdefault("nan_color", "grey")
742719
kwargs.setdefault("stitle", name)
720+
743721
text = kwargs.pop('text', None)
744722
if text is not None:
745723
self._internal_plotter._plotter.add_text(text, position='lower_edge')
746-
kwargs.pop("title", None)
724+
747725
kwargs_in = _sort_supported_kwargs(
748726
bound_method=self._internal_plotter._plotter.add_mesh,
749727
**kwargs
750728
)
751729
self._internal_plotter._plotter.add_mesh(mesh.grid, scalars=overall_data, **kwargs_in)
752730

731+
background = kwargs.pop("background", None)
753732
if background is not None:
754733
self._internal_plotter._plotter.set_background(background)
755734

735+
cpos = kwargs.pop("cpos", None)
756736
if cpos is not None:
757737
self._internal_plotter._plotter.camera_position = cpos
758738

759739
# show result
760-
if show_axes:
761-
self._internal_plotter._plotter.add_axes()
740+
return_cpos = kwargs.pop("return_cpos", None)
741+
kwargs_in = _sort_supported_kwargs(
742+
bound_method=self._internal_plotter._plotter.show,
743+
**kwargs)
762744
if return_cpos is None:
763-
kwargs_in = _sort_supported_kwargs(
764-
bound_method=self._internal_plotter._plotter.show,
765-
**kwargs)
766745
return self._internal_plotter._plotter.show(**kwargs_in)
767746
else:
768747
import pyvista as pv
769748
pv_version = pv.__version__
770749
version_to_reach = '0.32.0'
771750
meet_ver = meets_version(pv_version, version_to_reach)
772751
if meet_ver:
773-
return self._internal_plotter._plotter.show(return_cpos=return_cpos)
752+
return self._internal_plotter._plotter.show(return_cpos=return_cpos, **kwargs_in)
774753
else:
775754
txt = """To use the return_cpos option, please upgrade
776755
your pyvista module with a version higher than """

examples/05-plotting/00-basic_plotting.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@
1010

1111
from ansys.dpf import core as dpf
1212
from ansys.dpf.core import examples
13-
# from ansys.dpf.core.plotter import plot_chart
1413

1514

1615
# Plot the bare mesh of a model
1716
model = dpf.Model(examples.multishells_rst)
1817
model.plot(color="w", show_edges=True, title='Model', text='Model plot')
1918
# # Additional PyVista kwargs are supported, such as:
20-
model.plot(off_screen=True, screenshot='model_plot.png', title='Model', text='Model plot')
19+
model.plot(off_screen=True, notebook=False, screenshot='model_plot.png',
20+
title='Model', text='Model plot off')
21+
22+
# Notes:
23+
# - To make screenshots, use "screenshot" as well as "notebook=False" if on a Jupyter notebook.
24+
# - The "off_screen" keyword only works when "notebook=False" to prevent the GUI from appearing.
2125

2226

2327
# Plot a field on its supporting mesh (field location must be Elemental or Nodal)
@@ -27,14 +31,16 @@
2731
field = fc[0]
2832
field.plot(notebook=False, shell_layers=None, show_axes=True, title='Field', text='Field plot')
2933
# # Additional PyVista kwargs are supported, such as:
30-
field.plot(off_screen=True, screenshot='field_plot.png', title='Field', text='Field plot off')
34+
field.plot(off_screen=True, notebook=False, screenshot='field_plot.png',
35+
title='Field', text='Field plot off')
3136
#
3237
# # Alternatively one can plot the MeshedRegion associated to the model
3338
mesh = model.metadata.meshed_region
3439
mesh.plot(field_or_fields_container=None, shell_layers=None, show_axes=True,
3540
title='Mesh fc None', text='Mesh plot')
3641
# Additional PyVista kwargs are supported, such as:
37-
mesh.plot(off_screen=True, screenshot='mesh_plot.png', title='Mesh', text='Mesh plot off')
42+
mesh.plot(off_screen=True, notebook=False, screenshot='mesh_plot.png',
43+
title='Mesh', text='Mesh plot off')
3844
# A fields_container or a specific field can be given to plot on the mesh.
3945
mesh.plot(field_or_fields_container=fc, title='Mesh with fields container', text='Mesh fc plot')
4046
mesh.plot(field_or_fields_container=field, title='Mesh with field', text='Mesh field plot')
@@ -53,5 +59,5 @@
5359
disp_fc = disp_op.outputs.fields_container()
5460
meshes_cont.plot(disp_fc, title='Meshes Container disp_fc', text='Meshes Container disp_fc plot')
5561
# Additional PyVista kwargs are supported, such as:
56-
meshes_cont.plot(off_screen=True, screenshot='meshes_cont_plot.png',
62+
meshes_cont.plot(off_screen=True, notebook=False, screenshot='meshes_cont_plot.png',
5763
title='Meshes Container', text='Meshes Container plot')

0 commit comments

Comments
 (0)