0

Following is the codes. It plots a line via pressing a button. However, when I pressed the button, it just printed

matplotlib.lines.Line2D object at 0x11371fcc0 ......

but could not show the line on the canvas. How do you fix it?

import matplotlib
matplotlib.use("Qt5Agg")
from PyQt5 import QtCore
from PyQt5.QtWidgets import *

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

import mywidgets
# mywidgets.MplCanvas is a wrapper of FigureCanvas in order to make the drawing convenient.

class ApplicationWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setWindowTitle("Hello")

        self.main_widget = QWidget(self)

        l = QVBoxLayout(self.main_widget)
        fig1 = Figure(figsize=(5, 4))
        self.sc = mywidgets.MplCanvas(self.main_widget, fig1)
        l.addWidget(self.sc)
        bdraw = QPushButton('Draw')
        bdraw.pressed.connect(self.draw)
        l.addWidget(bdraw)

        self.main_widget.setFocus()
        self.setCentralWidget(self.main_widget)

    def draw(self):
        # it does not report any error, but on lines are drawn.
        line = self.sc.axes.plot([1,2,3], 'r')
        print(line)


if __name__ == '__main__':
    app = QApplication([])

    aw = ApplicationWindow()
    aw.show()
    #sys.exit(qApp.exec_())
    app.exec_()
2
  • You're not providing the content of mywidget so we cannot run your code. Please provide a Minimal, Complete, and Verifiable example Commented Feb 22, 2018 at 8:19
  • 1
    In any case, I'm guessing you need to add a draw() or draw_idle() after your plotting instruction Commented Feb 22, 2018 at 8:20

1 Answer 1

1

You forgot to update the canvas after plotting to it.

def draw(self):
    line = self.sc.axes.plot([1,2,3], 'r')
    self.sc.draw_idle()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.