如何在 Python Dash 中将 matlab 图绘制为 html

How can plot a matlab graph as html in Python Dash

我正在尝试在 HTML 页面上显示一个图。如何将此图插入 Html.DiV() 中。 我试过了,但它给了我一个错误。

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)

Matplotlib 图形不像 Plotly 图形那样基于 HTML。要显示 Matplotlib 图形,您需要先将图形保存为图像,然后利用 Dash 的 html.Img 函数将图像加载到您的 website/dashboard.

您可以通过将以下内容添加到上述脚本的底部来保存您的 matplotlib 图。注意图片自然不会交互,需要先用plotly重制图才能交互

plt.savefig('monthly_returns_dist.jpg)

然后您可以使用 html.Img.

自由指向该保存位置

您可以在此处了解有关 html.Img 及其使用方法的更多信息:https://dash.plotly.com/dash-html-components/img