Question

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)

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top