plotly の Dash フレームワークで、単純な matplotlib プロット (通常は によって生成される種類plt.show()
) を表示することは可能ですか? または、plotly の Scatters および Data トレースを備えた plotly のようなグラフのみですか?
具体的には、(下記参照)とは異なるコンポーネントGraph
と、関数内で単純なプロットを返す方法が必要だと思いますupdate_figure
。
例:
import dash
import dash_core_components as dcc
import dash_html_components as html
import numpy as np
import matplotlib.pyplot as plt
app = dash.Dash()
app.layout = html.Div(children=[
html.H1(children='Hello Dash'),
dcc.Slider(
id='n_points',
min=10,
max=100,
step=1,
value=50,
),
dcc.Graph(id='example') # or something other than Graph?...
])
@app.callback(
dash.dependencies.Output('example', 'figure'),
[dash.dependencies.Input('n_points', 'value')]
)
def update_figure(n_points):
#create some matplotlib graph
x = np.random.rand(n_points)
y = np.random.rand(n_points)
plt.scatter(x, y)
# plt.show()
return None # return what, I don't know exactly, `plt`?
if __name__ == '__main__':
app.run_server(debug=True)
ベストアンサー1
参照するhttps://plot.ly/matplotlib/matplotlib-figure の変更/mpl_to_plotly
ライブラリにはplotly.tools
、matplotlib の図から plotly 図 (Graph の図属性に返すことができます) を返す関数があります。
編集: 少し前にこの質問をしたのに気付きました。上記は新しい機能かもしれませんが、最もクリーンな方法です。