Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to embed mplfnance in the application of pyqt5? #355

Open
lyl1836 opened this issue Mar 18, 2021 · 7 comments
Open

How to embed mplfnance in the application of pyqt5? #355

lyl1836 opened this issue Mar 18, 2021 · 7 comments
Labels
question Further information is requested

Comments

@lyl1836
Copy link

lyl1836 commented Mar 18, 2021

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 lyl1836 added the question Further information is requested label Mar 18, 2021
@DanielGoldfarb
Copy link
Collaborator

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

Then read this about mplfinance:

hth. all the best. --Daniel

@lyl1836
Copy link
Author

lyl1836 commented Mar 19, 2021

@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
Copy link
Author

lyl1836 commented Mar 19, 2021

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
Copy link
Author

lyl1836 commented Mar 19, 2021

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

@DanielGoldfarb
Copy link
Collaborator

@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?

@stevedidienne
Copy link

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)?

@DanielGoldfarb
Copy link
Collaborator

DanielGoldfarb commented Oct 13, 2021

@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!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants