Skip to content

Commit 6ce35ff

Browse files
committed
Visualizers: add working default for ax parameter
1 parent 3b53292 commit 6ce35ff

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

lib/python/picongpu/plugins/plot_mpl/base_visualizer.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,24 @@ def _update_plt_obj(self):
2929
"""
3030
raise NotImplementedError
3131

32-
def visualize(self, ax, **kwargs):
32+
def _ax_or_gca(self, ax):
33+
"""
34+
Returns the passed ax if it is not None or the current
35+
matplotlib axes object otherwise.
36+
"""
37+
38+
return ax or plt.gca()
39+
40+
def visualize(self, ax=None, **kwargs):
3341
"""
3442
1. Creates the 'plt_obj' if it does not exist
3543
2. Fills the 'data' parameter by using the reader
3644
3. Updates the 'plt_obj' with the new data.
3745
"""
38-
39-
self.data = self.data_reader.get(**kwargs)
46+
if ax is None:
47+
raise ValueError("A matplotlib axes object needs to be passed!")
4048

49+
self.data = self.data_reader.get(**kwargs)
4150
if self.plt_obj is None:
4251
self._create_plt_obj(ax)
4352
else:

lib/python/picongpu/plugins/plot_mpl/energy_histogram_visualizer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def _update_plt_obj(self):
3030
bins, counts = self.data
3131
self.plt_obj.set_data(bins, counts)
3232

33-
def visualize(self, ax, **kwargs):
33+
def visualize(self, ax=None, **kwargs):
3434
"""
3535
Creates a semilogy plot on the provided axes object for
3636
the data of the given iteration using matpotlib.
@@ -52,7 +52,7 @@ def visualize(self, ax, **kwargs):
5252
(defined in ``particleFilters.param``)
5353
5454
"""
55-
55+
ax=self._ax_or_gca(ax)
5656
# this already throws error if no species or iteration in kwargs
5757
super(Visualizer, self).visualize(ax, **kwargs)
5858
iteration = kwargs.get('iteration')

lib/python/picongpu/plugins/plot_mpl/phase_space_visualizer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def _update_plt_obj(self):
5050
dat, meta = self.data
5151
self.plt_obj.set_data(np.abs(dat).T * meta.dV)
5252

53-
def visualize(self, ax, **kwargs):
53+
def visualize(self, ax=None, **kwargs):
5454
"""
5555
Creates a phase space plot on the provided axes object for
5656
the data of the given iteration using matpotlib.
@@ -72,6 +72,7 @@ def visualize(self, ax, **kwargs):
7272
phase space selection in order: spatial, momentum component,
7373
e.g. 'ypy' or 'ypx'
7474
"""
75+
ax = self._ax_or_gca(ax)
7576
super(Visualizer, self).visualize(ax, **kwargs)
7677

7778
_, meta = self.data

lib/python/picongpu/plugins/plot_mpl/png_visualizer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def _create_plt_obj(self, ax):
3737
def _update_plt_obj(self):
3838
self.plt_obj.set_data(self.data)
3939

40-
def visualize(self, ax, **kwargs):
40+
def visualize(self, ax=None, **kwargs):
4141
"""
4242
Creates a plot on the provided axes object for
4343
the PNG file of the given iteration using matpotlib.
@@ -65,6 +65,7 @@ def visualize(self, ax, **kwargs):
6565
if set to 'None', then return images for all available\
6666
iterations
6767
"""
68+
ax = self._ax_or_gca(ax)
6869
super(Visualizer, self).visualize(ax, **kwargs)
6970

7071

0 commit comments

Comments
 (0)