[Python] Plot a figure on a canvas

Discussão em 'Python' iniciado por Stack, Julho 1, 2025.

  1. Stack

    Stack Membro Participativo

    I am using Qt Designer and matplotlib for plotting.

    So far, I've promoted a Qwidget to a Canvas class and draw my plots on it by creating axes and plotting them with figure.add_subplot(), so code looks this way:

    ax = self.canvas.figure.add_subplot(111)

    ax.plot(data)

    self.canvas.draw()


    But now I have a figure object and I need to plot it. Is there a way to do that without instanciating axes and assigning them to a figure? Is there a way I can do : "self.canvas.add_figure" ? Because in my case I don't have access to the axes.

    Edit : here's the code of the canvas.

    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
    import matplotlib.pyplot as plt
    from PyQt4 import QtGui
    class Canvas(FigureCanvas):
    def __init__(self, parent=None):
    self.figure = plt.figure()
    FigureCanvas.__init__(self, self.figure)
    self.setParent(parent)
    FigureCanvas.setSizePolicy(self,
    QtGui.QSizePolicy.Expanding,
    QtGui.QSizePolicy.Expanding)
    FigureCanvas.updateGeometry(self)

    Continue reading...

Compartilhe esta Página