Skip to content

Commit

Permalink
Figure properties are only set before the actual plotting is done if …
Browse files Browse the repository at this point in the history
…figure is not set externally.
  • Loading branch information
tillbiskup committed Dec 8, 2024
1 parent e1f980b commit b40b7cc
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .prospector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pep257:

pylint:
options:
max-attributes: 16
max-attributes: 18
max-module-lines: 5000

pycodestyle:
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.12.0.dev44
0.12.0.dev45
37 changes: 30 additions & 7 deletions aspecd/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,8 +1072,8 @@ def __init__(self):
}
self.properties = PlotProperties()
self.description = "Abstract plotting step"
self.figure = None
self.axes = None
self._figure = None
self._axes = None
self.filename = ""
self.caption = Caption()
self.legend = None
Expand All @@ -1091,16 +1091,38 @@ def __init__(self):
"axes",
"legend",
]
self._figure_set_externally = False
self._axes_set_externally = False

@property
def figure(self):
"""Reference to figure object."""
return self._figure

@figure.setter
def figure(self, figure=None):
self._figure_set_externally = True
self._figure = figure

@property
def fig(self):
"""Shorthand for :attr:`figure`."""
return self.figure
return self._figure

@property
def axes(self):
"""Reference to axes object used for actual plotting."""
return self._axes

@axes.setter
def axes(self, axes=None):
self._axes_set_externally = True
self._axes = axes

@property
def ax(self): # pylint: disable=invalid-name
"""Short hand for :attr:`axes`."""
return self.axes
"""Shorthand for :attr:`axes`."""
return self._axes

def plot(self):
"""Perform the actual plotting.
Expand Down Expand Up @@ -1253,7 +1275,7 @@ def _create_figure_and_axes(self):
mpl.interactive(
False
) # Mac OS X: prevent plot window from opening
self.figure, self.axes = plt.subplots()
self._figure, self._axes = plt.subplots()

def _apply_figure_properties(self):
"""
Expand All @@ -1266,7 +1288,8 @@ def _apply_figure_properties(self):
itself relies on the actual figure size.
"""
self.properties.figure.apply(figure=self.figure)
if not self._figure_set_externally:
self.properties.figure.apply(figure=self.figure)

def _create_plot(self):
"""Perform the actual plotting of the data of the dataset(s).
Expand Down
22 changes: 22 additions & 0 deletions tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,28 @@ def _create_plot(self):
self.assertListEqual(figure_size, list(plotter.figure_size))
plt.close(plotter.figure)

def test_fig_props_are_not_applied_before_plotting_if_fig_set_extern(
self,
):

class MockPlotter(plotting.Plotter):
def __init__(self):
super().__init__()
self.figure_size = None

def _create_plot(self):
self.figure_size = self.figure.get_size_inches()

plotter = MockPlotter()
figure_size = [3, 6]
plotter.properties.figure.size = figure_size
fig, ax = plt.subplots()
plotter.figure = fig
plotter.axes = ax
plotter.plot()
self.assertNotEqual(figure_size[0], plotter.figure_size[0])
plt.close(plotter.figure)


class TestSinglePlotter(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit b40b7cc

Please sign in to comment.