2

I've implemented a GUI that displays two dropdown menus in which you can choose two different set of graphs to be displayed. However when I create the graphs with the following code:

import matplotlib.pyplot as plt
from matplotlib.backends.backedn_qt4agg import FigureCanvasQTAgg as FigureCanvas


self.comparison_figure1 = plt.figure(figsize=(15,5))
self.comparison_canvas1 = FigureCanvas(self.comparison_figure1)

self.comparison_figure2 = plt.figure(figsize=15,5))
self.comparison_canvas2 = FigureCanvas(self.comparison_figure2)

And then I try to update the plots (plt.tight_layout() for example)

def on_resize(event):
    plt.tight_layout()
    self.comparison_canvas2.draw()
    self.comparison_canvas1.draw()   #this would do nothing 
cid = self.comparison_canvas2.mpl_connect('resize_event', on_resize)

only the last plot called with "plt." is updated. How do I write my code so that I can reference both plots.

I've also tried to create one plot where I have both graphs being displayed side by side but because of the need to update the graphs independently I encountered more problems. If you are able to make it work that way instead, great! I'm just thinking that fixing the previous problem may be simpler.

If you need more code I can post it!

# #

Solution (Thanks to ImportanceOfBeingErnest and Ash Sharma):

replace any "plt." with the specific figure

for example:

plt.tight_layout()      #replace with self.comparison_figure1.tight_layout()

So this is some of the fixed code:

def on_resize(event):
    self.comparison_figure1.tight_layout()
    self.comparison_figure2.tight_layout()
    self.comparison_canvas1.draw()
    self.comparison_canvas2.draw()
cid = self.comparison_canvas2.mpl_connect('resize_event', on_resize)

# #

Problem:

Previous solution hasn't fixed all plot updating issues. Though "plt" can be replaced with self.comparison_figure1 when using tight_layout(), the same cannot be done when using cla() to clear the plot.

Code where I'm using cla():

sns.set(style="whitegrid")
plt.cla()
ax = self.comparison_figure2.add_subplot(111)
.....
.....

I can post more code if you need it!

  • Tried defining the plots as `fig,ax` and then update the required variable.? This link : https://stackoverflow.com/questions/34162443/why-do-many-examples-use-fig-ax-plt-subplots-in-matplotlib-pyplot-python – Ash Sharma Jun 20 '18 at 08:22
  • 2
    Best don't use `pyplot` when embedding matplotlib into GUIs. This forces you to think on what objects you are actually working with. In this case you want to do something with two objects `fig1` and `fig2`, hence you need to call `fig1.tight_layout()` and `fig2.tight_layout()`. – ImportanceOfBeingErnest Jun 20 '18 at 08:46

1 Answers1

1

Solution (Thanks to ImportanceOfBeingErnest and Ash Sharma):

replace any plt with the specific figure

for example:

plt.tight_layout()      #replace with self.comparison_figure1.tight_layout()

So this is some of the fixed code:

def on_resize(event):
    self.comparison_figure1.tight_layout()
    self.comparison_figure2.tight_layout()
    self.comparison_canvas1.draw()
    self.comparison_canvas2.draw()
cid = self.comparison_canvas2.mpl_connect('resize_event', on_resize)
Mr. T
  • 11,960
  • 10
  • 32
  • 54
  • Problem: Previous solution hasn't fixed all plot updating issues. Though "plt" can be replaced with self.comparison_figure1 when using tight_layout(), the same cannot be done when using cla() to clear the plot. Code where I'm using cla(): sns.set(style="whitegrid") plt.cla() ax = self.comparison_figure2.add_subplot(111) ..... ..... – Daniel Gros Jun 20 '18 at 09:48
  • Pro tip: Nobody will see this problem, hidden in a comment. Ask a new question instead and link to this one for reference. – Mr. T Jun 20 '18 at 09:51