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 mplfinance to certain program in python? #125

Closed
bluesaturn1 opened this issue May 5, 2020 · 7 comments
Closed

how to embed mplfinance to certain program in python? #125

bluesaturn1 opened this issue May 5, 2020 · 7 comments
Labels
question Further information is requested

Comments

@bluesaturn1
Copy link

Hi.
It is very convenient and excellent.
It is well operated on Jupyter Notebook and Pycharm.
I want to embed into some program, not pop-up.
Is there any tip to embed on layout?

@bluesaturn1 bluesaturn1 added the question Further information is requested label May 5, 2020
@DanielGoldfarb
Copy link
Collaborator

DanielGoldfarb commented May 5, 2020

You should be able to embed using the kwarg block=False .
See these two comments here: #46 (comment) and #46 (comment)

You don't have to include the code discussed in those comments. That code is now built into mpf.plot(). Simply call mpf.plot(data,...,block=False)

Please let me know if that works for you.

crossed out above comments because they are incorrect.
see correct answer below

@bluesaturn1
Copy link
Author

bluesaturn1 commented May 5, 2020

Thanks. I am newbi.

I usually plot matplotlib item as below.

self.fig = plt.figure(figsize=(8, 5))
self.canvas = FigureCanvas(self.fig)
self.chartlayout.addWidget(self.canvas)
ax = self.fig.add_subplot(111)
ax = df.plot(data.......)
self.canvas.draw()

but mpf has inclued axes.
so I can't understand how to insert below code to canvas.

mpf.plot(daily,type='candle',mav=(5,20,60),volume=True)

@DanielGoldfarb
Copy link
Collaborator

DanielGoldfarb commented May 6, 2020

I am not very familiar with embedding plots in gui applications, however I think what you need to do is set mpf.plot(returnfig=True). (I made a mistake above mentioning setting block=False; forget that.)

Try the following: Do not create your own figure, but rather first call mpf.plot() and create your widget using the figure returned from mpf.plot()

fig, axlist = mpf.plot(ohlcv_dataframe, figratio=(8, 5), returnfig=True)
canvas = FigureCanvas(fig)
chartlayout.addWidget(canvas)
canvas.draw()

The above code is not exact. As I said I am not familiar with this stuff, but you should be able to get the general idea from the above and modify the code accordingly for your needs. Please let me know how it goes.

@bluesaturn1
Copy link
Author

Thank you very much. Finally I can embed mpf chart to my program.

20200506222820

20200506222839

But one more thing.
whenever I click button to see chart, image is getting more.
I want delete previous image and plot new chart.

@DanielGoldfarb
Copy link
Collaborator

I'm not sure about that. As previously mentioned, I have no experience embedding matplotlib plots in gui applications. I imagine it depends on how your code responds to the button's click event. And it seems to me your code is responding to the click by re-plotting the figure without closing the old one. You may try modifying the button click handler to check if the plot has already been plotted and, if so, do nothing.

Alternatively, upon click, you may try closing the figure before plotting a new one. If the data possibly has changed, then re-plotting may be what you want:

plt.close(fig)

See also here: https://stackoverflow.com/a/11141305/1639359

@lignpeng
Copy link

lignpeng commented Jun 7, 2020

very good!
mobai dalao!

@hmhjapan
Copy link

hmhjapan commented Jan 2, 2021

@bluesaturn1
Could you publish all the Source Code?
I have the same problem. Refer to these Source Code,I still can't finish.
thank you!

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

4 participants