简体   繁体   中英

How do I selectively erase lines on a 2 y-axis subplot using matplotlib.(Python)

Here's the code that I'm using so far:

class Main:
    app = QtGui.QApplication(sys.argv)

    QtCore.QObject.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))
    mainWidget = Gui.PointPredictorGui()
    app.setActiveWindow(mainWidget)
    mainWidget.show()
    app.exec()

class PointPredictorGui(QtGui.QMainWindow):
def __init__(self, parent = None):
    QtGui.QMainWindow.__init__(self)
    frame = QtGui.QFrame()
    hobo = QtGui.QHBoxLayout()
    self.graphFigure = plt.Figure()
    self.graphCanvas = FigureCanvas(self.graphFigure)

    self.ax1 = self.graphFigure.add_subplot(111)
    self.ax2 = self.ax1.twinx()

    self.check1 = QtGui.QCheckBox("1")
    self.check2 = QtGui.QCheckBox("2")

    hobo.addWidget(self.graphCanvas)
    hobo.addWidget(self.check1)
    hobo.addWidget(self.check2)

    frame.setLayout(hobo)
    self.setCentralWidget(frame)
    self.check1.stateChanged.connect(self.updateGraph)
    self.check2.stateChanged.connect(self.updateGraph)

def updateGraph(self):

    if self.check1.isChecked(): self.ax1.plot([1,2,3,4,5], [1,2,3,4,5], '--')
    elif not self.check1.isChecked(): self.eraseMethod()
    if self.check2.isChecked(): self.ax2.plot([1,2,3,4,5], [500,400,300,200,100], '--')
    elif not self.check2.isChecked(): self.eraseMethod()
    self.graphCanvas.draw()

def eraseMethod(self):
    self.graphFigure.clear()

What I am trying to get to happen is that I want line attributed to axis 1 or 2 to display dependent on whether or not the corresponding checkbox is, well, checked.

When both are checked:

在此处输入图片说明

When only the second one is checked:

在此处输入图片说明

You get the idea.

The problem I'm having is that I can't seem to either implement this the right way to facilitate what I want to do, or figure out which method to use from which class. Pretty much whatever I've tried results in no change at all or the graph portion disappearing entirely.

To recap, this is what I want to do: Lines appear when checkboxes get checked (done) Lines disappear when checkboxes get unchecked (not done)

Also, redrawing the entire thing every time is fine if that's what I need to resort to.

I figured it out.

self.graphFigure = plt.figure()

fig, self.ax1 = plt.subplots()
self.ax2 = self.ax1.twinx()

self.graphCanvas = FigureCanvas(fig)

This is the setup the graph widget needs to have in order to make what I was trying to do possible. Why? I'm not quite sure, maybe someone else could answer that. What I do know though is that redrawing the graph on an axis by axis basis is required. Here's my update method:

def updateGraph(self):
    plt.sca(self.ax2)
    plt.cla()
    plt.sca(self.ax1)
    plt.cla()
    if self.check1.isChecked():
        self.ax1.plot([1,2,3,4,5], [1,2,3,4,5], '--')

    if self.check2.isChecked():
        self.ax2.plot(self.line2.get_xdata(), self.line2.get_ydata(), '--')

    self.graphCanvas.draw_idle()

By the way, here's what self.line2 is: self.line1 = matplotlib.lines.Line2D([1,2,3,4,5], [1,2,3,4,5])

I tried both ways (the list and the line object) to see if any weird errors came up. Back to redrawing the graph, I had to clear one of the axes ( plt.cla() ), then plot to it ( self.axis.plot(stuffx, stuffy) ), then redraw the graph. To make sure both of the axes got updated each time the update method was called I had to switch between the axes( plt.sca(axis to be cleared) ) and repeat the process, redrawing afterwards.

I hope this helps anyone else that came across this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM