user14708750
user14708750

Reputation:

How can plot a matlab graph as html in Python Dash

I am trying to display a plot on the HTML page. How can I insert this plot inside an Html.DiV(). I tried but it gave me an error.

import pandas as pd
import yfinance as yf
import matplotlib.pyplot as plt
import pyfolio as pf 
ticker='AAPL'

tickers_list = [ticker]
data = pd.DataFrame(columns=tickers_list)
for ticker in tickers_list:
    data[ticker] = yf.download(ticker, period='10y',)['Adj Close']
data = data.pct_change().dropna().mean(axis=1)
data.head()

fig, ax = plt.subplots()
pf.plot_monthly_returns_dist(data)

Upvotes: 0

Views: 1841

Answers (1)

WolVes
WolVes

Reputation: 1336

Matplotlib graphics are not HTML based like Plotly graphs. To show a Matplotlib graph, you would need to save the graphic as an image first, then utilize Dash's html.Img function to load the image to your website/dashboard.

You can save your matplotlib plot by adding the following to the bottom of your above script. Note that the image will naturally not be interactive, you would need to remake the graphic with plotly first for the graphic to be interactive.

plt.savefig('monthly_returns_dist.jpg)

You are then free to point to that save location with html.Img.

You can learn more about html.Img and how to use it here: https://dash.plotly.com/dash-html-components/img

Upvotes: 2

Related Questions