0


i want add my plot to pyqt ui
i am using pandas for plot.

data= pandas.DataFrame(pandas.read_sql(sql, my_connection))
data[data['GENERAL_TYPE']=='phone'].pivot_table('CEID','CONTACTED_PERSON_NEW','RESULT',  aggfunc='count').plot(kind='bar', stacked=True, figsize=(10,10))
data[data['GENERAL_TYPE']=='phone'].pivot_table(['CEID'],['CONTACTED_PERSON_NEW'],['DATA4_CAMP'],  aggfunc='count').plot(kind='bar',figsize=(10,10), stacked=True)
data[data['GENERAL_TYPE']=='phone'].pivot_table(['CEID'],['CONTACTED_PERSON_NEW'],  aggfunc='count').plot(kind='pie',subplots=True, legend=False, stacked=False, figsize=(10,10)) 

and after this i have few plots :

How i can use this example(How to embed matplotib in pyqt - for Dummies) add my plots to different layouts in one window ?

Community
  • 1
  • 1

1 Answers1

0

The example you provided does not run for me plus it contains an error, as stated in one of the comments. When embedding in PyQt you cannot use plt.figure() because this will start its own event loop. You must use matplotlib.Figure() instead. I strongly recommend that you base your code on this example from the Matplotlib documentation, instead of on the 'for Dummies' answer.

If you got a regular Matplotlib plot working in PyQt, you can move on and try a Pandas plot. I don't know much about Pandas but it seems to me that Pandas plots to the current axes by default. This will not work when embedding in PyQt; plt.figure() sets the current axes, but we must use matplotlib.Figure() so there is no current axis. However, I think that the Pandas plot command accepts an ax keyword parameter. This allows you to pass your own axes object instead (which is defined with self.axes = fig.add_subplot(111) in the example)

In the Matplotlib example you can try to replace

self.axes.plot(...)

with your Pandas plot command and pass ax=self.axes. Something like this

data[...].pivot_table(...).plot(ax=self.axes, ...)

If that doesn't work I would ask the Pandas community. I will add a tag.

titusjan
  • 5,376
  • 2
  • 24
  • 43