1
votes

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.

1
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
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
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
Yes, it works!! Thank you so much. Now i understand a bit more about Qt Designer. Thank you again. - Pablo Flores
Great. I've added it as a formal solution below - Suever

1 Answers

1
votes

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)