Skip to content

Commit

Permalink
Explicitly delete and replace self.plot when update_plot is called (#363
Browse files Browse the repository at this point in the history
)

* Explicitly delete and replace self.plot when update_plot is called

* Explicitly clear the figure before deleting the object
  • Loading branch information
dgdekoning authored Feb 18, 2020
1 parent 1b4a711 commit 51ba701
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions activity_browser/app/ui/tabs/LCA_results_tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,16 @@ def __init__(self, parent=None):
self.export_table: Optional[ExportTable] = None

self.scenario_box = QComboBox()
self.pt_layout = QVBoxLayout()
self.layout = QVBoxLayout()
self.setLayout(self.layout)

def build_main_space(self) -> QScrollArea:
"""Assemble main space where plots, tables and relevant options are shown."""
space = QScrollArea()
widget = QWidget()
layout = QVBoxLayout()
layout.setAlignment(QtCore.Qt.AlignTop)
widget.setLayout(layout)
self.pt_layout.setAlignment(QtCore.Qt.AlignTop)
widget.setLayout(self.pt_layout)
space.setWidget(widget)
space.setWidgetResizable(True)

Expand All @@ -233,12 +233,12 @@ def build_main_space(self) -> QScrollArea:

# Assemble Table and Plot area
if self.table and self.plot:
layout.addLayout(row)
self.pt_layout.addLayout(row)
if self.plot:
layout.addWidget(self.plot, 1)
self.pt_layout.addWidget(self.plot, 1)
if self.table:
layout.addWidget(self.table)
layout.addStretch()
self.pt_layout.addWidget(self.table)
self.pt_layout.addStretch()
return space

@QtCore.Slot(name="checkboxChanges")
Expand Down Expand Up @@ -561,7 +561,13 @@ def update_plot(self, method_index: int = 0):
bc.format_activity_label(next(iter(fu.keys())), style='pnld')
for fu in self.parent.mlca.func_units
]
idx = self.layout.indexOf(self.plot)
self.plot.figure.clf()
self.plot.deleteLater()
self.plot = LCAResultsBarChart(self.parent)
self.layout.insertWidget(idx, self.plot)
self.plot.plot(df, method=method, labels=labels)
self.updateGeometry()
self.plot.plot_name = '_'.join([self.parent.cs_name, 'LCA scores', str(method)])


Expand All @@ -585,10 +591,15 @@ def __init__(self, parent, **kwargs):

def update_plot(self):
"""Update the plot."""
if not isinstance(self.plot, LCAResultsPlot):
self.plot = LCAResultsPlot(self.parent)
idx = self.pt_layout.indexOf(self.plot)
self.plot.figure.clf()
self.plot.deleteLater()
self.plot = LCAResultsPlot(self.parent)
self.pt_layout.insertWidget(idx, self.plot)
self.df = self.parent.contributions.lca_scores_df(normalized=self.relative)
self.plot.plot(self.df)
if self.pt_layout.parentWidget():
self.pt_layout.parentWidget().updateGeometry()

def update_table(self):
"""Update the table."""
Expand Down Expand Up @@ -763,7 +774,14 @@ def update_dataframe(self, *args, **kwargs):

def update_plot(self):
"""Update the plot."""
idx = self.pt_layout.indexOf(self.plot)
self.plot.figure.clf()
self.plot.deleteLater()
self.plot = ContributionPlot()
self.pt_layout.insertWidget(idx, self.plot)
self.plot.plot(self.df, unit=self.unit)
if self.pt_layout.parentWidget():
self.pt_layout.parentWidget().updateGeometry()


class ElementaryFlowContributionTab(ContributionTab):
Expand Down Expand Up @@ -881,10 +899,15 @@ def __init__(self, parent):

def update_plot(self):
"""Update the plot."""
if self.plot is None:
self.plot = CorrelationPlot(self.parent)
idx = self.pt_layout.indexOf(self.plot)
self.plot.figure.clf()
self.plot.deleteLater()
self.plot = CorrelationPlot(self.parent)
self.pt_layout.insertWidget(idx, self.plot)
df = self.parent.mlca.get_normalized_scores_df()
self.plot.plot(df)
if self.pt_layout.parentWidget():
self.pt_layout.parentWidget().updateGeometry()


class SankeyTab(QWidget):
Expand Down Expand Up @@ -1122,8 +1145,15 @@ def update_mc(self, cs_name=None):
self.plot.plot_name, self.table.table_name = filename, filename

def update_plot(self, method):
idx = self.layout.indexOf(self.plot)
self.plot.figure.clf()
self.plot.deleteLater()
self.plot = MonteCarloPlot(self.parent)
self.layout.insertWidget(idx, self.plot)
self.plot.plot(self.df, method=method)
self.plot.show()
if self.layout.parentWidget():
self.layout.parentWidget().updateGeometry()

def update_table(self):
self.table.sync(self.df)
Expand Down

0 comments on commit 51ba701

Please sign in to comment.