0

I was designing my GUI with QtDesigner and PyQt4. First in QtDesigner I added a matplotlib widget to my GUI. Then in the code I want to add multiple embedded figures to be displayed in parallel.

I checked the given code of the MatplotlibWidget class that comes with PyQt4. It uses the following code to create Figure.

self.figure = Figure(figsize=(width, height), dpi=dpi)
Canvas.__init__(self, self.figure)

But when I check how to add more figures, most of the solution online is using the

pyplot.figure()

But this is not suitable for my case, since it creates standalone dialog. I want the new figure to be embedded in my current GUI.

Any one knows how to add new figures without using the pyplot?

noname
  • 369
  • 3
  • 14

1 Answers1

2

Without a minimal working example or idea which widget you're using it's a little tricky to help. I'm not sure about QtDesigner but with a matplotlib figure, you would add a number of subplots to the figure, plot initial data on these (e.g. a line) and then update the data for each artist (the line) as part of the widget's update process, e.g.

Add subplots to figure

ax1 = self.figure.add_subplot(2,1,1)
ax2 = self.figure.add_subplot(2,1,2)

Draw an initial line on each

l1, = ax1.plot(np.linspace(0,10,100),np.linspace(0,2,100))
l2, = ax2.plot(np.linspace(0,10,100),np.linspace(0,2,100))

With the lines updated by changing val from the widget,

def update(val):
    l1.set_ydata(val)
    l2.set_ydata(val)

where update is bound to the widget,

widget.on_changed(update)
Community
  • 1
  • 1
Ed Smith
  • 12,716
  • 2
  • 43
  • 55
  • may I ask what is the difference between add_subplot() and add_axes()? – noname Aug 26 '15 at 12:40
  • @tom From the docs, what I understand is that these two methods provide different ways of locating the subplot or axes. But conceptually the subplot and axes are the same thing, am I right? – noname Aug 26 '15 at 13:00
  • @noname: Its not that a subplot and an axes are the same thing, but the both `add_subplot` and `add_axes` return an `Axes` instance. They are just different ways of acheiveing the same thing – tmdavison Aug 26 '15 at 13:03
  • There is a problem with this add_subplot() in my use case. I want to be able to add new subplots by clicking a button by the user, which means the matplotlib figure needs to know how to automatically arrange these subplots. Any idea how this can be achieved? – noname Aug 26 '15 at 13:05
  • Hmm, may not be trivial. This question may help: http://stackoverflow.com/questions/12319796/dynamically-add-create-subplots-in-matplotlib. Also, check out this answer for details of how to arrange http://stackoverflow.com/questions/3584805/in-matplotlib-what-does-the-argument-mean-in-fig-add-subplot111. It may be easier to clear the figure `fig.clf`, add all the axis again and replot data. Note you can use `fig, axs = plt.subplots(3,3)` to quickly setup figures. – Ed Smith Aug 26 '15 at 13:33