1

I created in QtDesigner this QDialog:

enter image description here

I would like to know how can I draw something in this Matplotlib widget that I put there. I mean, if I write some code to create a matplotlib figure without Qt Designer, I can write something like:

self.figure_canvas = FigureCanvas(Figure())

self.axes = self.figure_canvas.figure.add_subplot(111)

x = np.arange(0,5,0.5)
y = np.sin(x)

and then draw doing:

ax.plot(x,y) or self.axes.plot(x,y)

How can I access to this widget to draw something? Hope you can help me.

Pablo Flores
  • 667
  • 1
  • 13
  • 33
  • You will want to add your `FigureCanvas` to the widget which based on your screenshot should be accessible within your `QMainWindow` class as `self.matplotlibwidget`. Then you can call `self.matplotlibwidget.addWidget(self.figure_canvas)` – Suever Mar 25 '16 at 15:55
  • Thank you for your answer. I tried doing exactly this before, and I get an error saying `matplotlibwidget object has no attribute addwidget`. – Pablo Flores Mar 25 '16 at 17:13
  • 1
    Oh ok so you've already got the widget. Sorry mis-read that. You should be just able to use the widget to an an axes directly. `self.matplotlibwidget.axes.plot(x,y)` – Suever Mar 25 '16 at 17:15
  • Yes, it works!! Thank you so much. Now i understand a bit more about Qt Designer. Thank you again. – Pablo Flores Mar 25 '16 at 18:12
  • 1
    Great. I've added it as a formal solution below – Suever Mar 25 '16 at 18:14

1 Answers1

1

Based on the screenshot that you have provided, it seems that the MatplotlibWidget should be accessible as self.matplotlibwidget from within your QMainWindow class. This is because of the value listed in the "Object" column of the Object Inspector.

You can use this object directly to add plots to your GUI.

self.matplotlibwidget.axes.plot(x, y)
Suever
  • 64,497
  • 14
  • 82
  • 101