在 plotly Dash 中显示一个简单的 matplotlib 图
Showing a simple matplotlib plot in plotly Dash
是否可以在 plotly 的 Dash 框架中显示一个简单的 matplotlib 图(通常由 plt.show()
生成的那种)?或者只是带有 plotly 的 Scatters 和 Data traces 的类似 plotly 的图形?
具体来说,我想我需要一个与 Graph
不同的组件(见下文),以及一种在 update_figure
函数中 return 简单绘图的方法。
示例:
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)
参考https://plot.ly/matplotlib/modifying-a-matplotlib-figure/。 plotly.tools
库中有一个 mpl_to_plotly
函数,它将 return 来自 matplotlib 图的绘图图(然后可以 returned 到 Graph 的图属性)。
编辑:刚刚注意到您刚才问过这个问题。也许以上是一个新功能,但它是最干净的方式。
如果你不想要交互式情节,你可以return静态情节(从help找到)
import io
import base64
...
app.layout = html.Div(children=[
...,
html.Img(id='example') # img element
])
@app.callback(
dash.dependencies.Output('example', 'src'), # src attribute
[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)
buf = io.BytesIO() # in-memory files
plt.scatter(x, y)
plt.savefig(buf, format = "png") # save to the above file object
plt.close()
data = base64.b64encode(buf.getbuffer()).decode("utf8") # encode to html elements
return "data:image/png;base64,{}".format(data)
UserWarning: Starting a Matplotlib GUI outside of the main thread will likely fail
在我的情况下,尽管有警告消息,它仍然有效
是否可以在 plotly 的 Dash 框架中显示一个简单的 matplotlib 图(通常由 plt.show()
生成的那种)?或者只是带有 plotly 的 Scatters 和 Data traces 的类似 plotly 的图形?
具体来说,我想我需要一个与 Graph
不同的组件(见下文),以及一种在 update_figure
函数中 return 简单绘图的方法。
示例:
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)
参考https://plot.ly/matplotlib/modifying-a-matplotlib-figure/。 plotly.tools
库中有一个 mpl_to_plotly
函数,它将 return 来自 matplotlib 图的绘图图(然后可以 returned 到 Graph 的图属性)。
编辑:刚刚注意到您刚才问过这个问题。也许以上是一个新功能,但它是最干净的方式。
如果你不想要交互式情节,你可以return静态情节(从help找到)
import io
import base64
...
app.layout = html.Div(children=[
...,
html.Img(id='example') # img element
])
@app.callback(
dash.dependencies.Output('example', 'src'), # src attribute
[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)
buf = io.BytesIO() # in-memory files
plt.scatter(x, y)
plt.savefig(buf, format = "png") # save to the above file object
plt.close()
data = base64.b64encode(buf.getbuffer()).decode("utf8") # encode to html elements
return "data:image/png;base64,{}".format(data)
UserWarning: Starting a Matplotlib GUI outside of the main thread will likely fail
在我的情况下,尽管有警告消息,它仍然有效