mplfinance icon indicating copy to clipboard operation
mplfinance copied to clipboard

How to embed mplfnance in the application of pyqt5?

Open lyl1836 opened this issue 3 years ago • 7 comments

Hi,Daniel: Mplfinance is very convenient! Thank you !

I want to build a financial analysis tool with pyqt5. I want to embed mplfinance generated charts in pyqt5. What should I do?

Thank you very much.

lyl1836 avatar Mar 18 '21 08:03 lyl1836

@lyl1836 I would suggest that you first read about embedding Matplotlib plots in PyQt5:

  • There appear to be several good articles/tutorials here: https://www.google.com/search?q=imbed+matplotlib+in+pyqt5

Then read this about mplfinance:

  • https://github.com/matplotlib/mplfinance/wiki/Acessing-mplfinance-Figure-and-Axes-objects

hth. all the best. --Daniel

DanielGoldfarb avatar Mar 18 '21 17:03 DanielGoldfarb

@DanielGoldfarb Thank you for your answer, I use: fig, axlist= mpf.plot ( self.df , returnfig = true), is the implementation of embedding mplfinance in pyqt5. However, the two queries are overlapped, which cannot be cleared by many ways. 1、The code is as follows:

       if self.canvas:
            self.hLayoutMap.removeWidget(self.convas)
            sip.delete(self.canvas)
            plt.cla
            plt.clf()
            plt.close(self.canvas)
        fig, axlist = mpf.plot(self.df, returnfig=True)
        canvas = FigureCanvasQTAgg(fig)
        self.hLayoutMap.addWidget(canvas)

2、图形效果: Uploading p1.png…

lyl1836 avatar Mar 19 '21 00:03 lyl1836

To add image effect, I want a picture, but every time I run it, I superimpose a graph. I can't find a suitable way to delete the original graph.

image

lyl1836 avatar Mar 19 '21 00:03 lyl1836

@DanielGoldfarb : This problem has been dealt with, thank you very much!

lyl1836 avatar Mar 19 '21 09:03 lyl1836

@lyl1836 I was planning to respond:

It's very difficult for me to tell because I can only see a small amount of your code. Maybe, every time you run it, if you are calling mpf.plot() again, try del fig before you call mpf.plot()

Can you tell me how you solved the problem, in case someone reports a similar problem?

DanielGoldfarb avatar Mar 19 '21 13:03 DanielGoldfarb

Hello, I have the same problem. I can't put the completed plot in a widget. the technique explained above does not allow to put "add-plot", the volume plot or even "ax = ax" of our choice. Do you have a solution. (python 3.8 pyqt5)?

stevedidienne avatar Oct 13 '21 15:10 stevedidienne

@stevedidienne Steve, The technique mentioned above should work fine with addplot and with volume, for example:

       if self.canvas:
            self.hLayoutMap.removeWidget(self.convas)
            sip.delete(self.canvas)
            plt.cla
            plt.clf()
            plt.close(self.canvas)
            
        aps = [ mpf.make_addplot(somedata,kwargs,...),
                mpf.make_addplot(otherdata,kwargs,...), ]
        fig, axlist = mpf.plot(self.df,returnfig=True,addplot=aps,volume=True)
        canvas = FigureCanvasQTAgg(fig)
        self.hLayoutMap.addWidget(canvas)

Alternatively, if you want to set you own Axes, then use ax=myownaxes as a kwarg in both mpf.make_addplot() and mpf.plot() (of course, potentially using a different Axes object in each case). And please note that if you do specify your own Axes objects, then you must also specify an Axes object for the volume (i.e. if you set ax=someAxes, then you can no longer say volume=True but must rather say volume=myVolumeAxes).

If for any reason, the above answer is not clear to you, then I recommend that you first read completely the following documentation:

... then try again what you want to do (and then, if still having issues, post a follow up question here).


Please note: External Axes mode is discouraged. Although it gives you greater control over the Figure and Axes objects, there are some mplfinance features that are not available (because mplfinance no longer has control over the creation of the Figure and Axes objects). This technique should only be used if you are quite familiar with Matplotlib, and if what you are trying to accomplish cannot be done in any other way (ask, maybe it can!)

DanielGoldfarb avatar Oct 13 '21 15:10 DanielGoldfarb