1

I have managed to plot a multi-bar plot in the mainWindow of my application created with QtDesigner, the problem that I am having is that each time I try to plot I am having two identical graphs, one inside the application (which is what I wanted) but also another on an independent window (which I would like to not be shown).

Any help with this?? here is the code I am using to generate the plot inside the mainWindow:

Here the method to create any plot:

import numpy as np
import matplotlib.pyplot as plt

def drawPlot(x,y, y_O):

    n_groups = len(x)
    fig, ax = plt.subplots()
    index = np.arange(n_groups)
    bar_width = 0.35
    opacity = 0.4
    error_config = {'ecolor': '0.3'}
    rects1 = plt.bar(x, y, bar_width, alpha=opacity, color='b',label='label1')

    for i in range(len(x)):
        x[i] = x[i] + bar_width

    rects2 = plt.bar(x , y_O, bar_width, alpha=opacity, color='r',label='label2')

    plt.xlabel('Reading')
    plt.ylabel('Value (%)')

    plt.xticks(index + bar_width , (x))
    plt.legend()

    plt.tight_layout()

    return fig

And here a section of the mainWindow code where I try to include the generated plot in a dedicated space:

....
....
thePlot = tm.drawPlot(x,y,y_O)
MyCanvas = FigureCanvas(thePlot)
self.ui.horizontalLayoutGraph2_2.addWidget(MyCanvas)
...
...

As i said, I am getting two identical plots, one inside the horizontalLayout (OK) and the same plot in a pop-up independent window (NOT OK)

codeKiller
  • 5,493
  • 17
  • 60
  • 115
  • check out if you have unexpected `.draw()` or `.show()` somewhere in your code – Aleksandar Mar 18 '14 at 13:44
  • thanks! I will, but I would say that I am 95-99% sure that it is not. Actually, the code above is all that I have – codeKiller Mar 18 '14 at 13:49
  • No unexpected `.draw()` or `.show()` in the code, but still, two plots showing, must be something in the method `def drawPlot(x,y, y_O):` that is not written properly, but I don't have any idea about what is wrong... – codeKiller Mar 19 '14 at 09:05
  • The problem is not solved yet....I do not know why the plot is popping up after the end of the method if there is not any `.draw()` or any `.show()` in the code. – codeKiller Mar 27 '14 at 11:26
  • Since I don't know anything about `matplotlib`, I suggest you to check if FigureCanvas widget automatically show itself in this line `MyCanvas = FigureCanvas(thePlot)`. – Aleksandar Mar 27 '14 at 13:53
  • 1
    Seems like the pop-up window which I do not want is created at any point inside the method `def drawPlot(x,y, y_O):` because if I add `plt.close()` at the end of the method, the pop-up window is not showed, but it is still "flashig" each time I call the method...So, anybody would know how to avoid this?? – codeKiller Apr 02 '14 at 06:46

1 Answers1

1

Looking at your last comment, I've found this: "Turn off Interactive Mode, and only call plt.show() when you are ready to display the plots". Since you show your plot with FigureCanvas all you need to do is to add one line at beginning of your drawPlot method (in that case you don't even need plt.close() at the end)

import matplotlib.pyplot as plt

def drawPlot(x,y, y_O):
    # Turn interactive plotting off
    plt.ioff()
    ....

I hope this helps

Aleksandar
  • 3,541
  • 4
  • 34
  • 57
  • 1
    oh, lots of thanks, I will test it later and comment the result – codeKiller Apr 02 '14 at 07:52
  • FINALLY!!! It worked perfect!! the plots are not displayed in a pop-up window but the are in the mainWindow, just perfect!!. Thank you so much Aleksandar for helping me from the beginning!!. Oh God I spent so much time for a single line of code......sometimes python is terrible – codeKiller Apr 02 '14 at 08:02
  • Usually there is the lack of examples or poor documentation for libs like matplotlib. For example I've worked with SVGFig and I've read all code of the lib to learn all the options it offers – Aleksandar Apr 02 '14 at 09:17