gpt4 book ai didi

python - 在plotly Dash中显示一个简单的matplotlib图

转载 作者:行者123 更新时间:2023-12-03 16:55:00 25 4
gpt4 key购买 nike

是否可以在plotly的Dash框架中显示一个简单的matplotlib图(通常由plt.show()生成的图)?还是只是带有散点图和数据迹线的类似于散点图的图?

具体来说,我想我需要的组件与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)

最佳答案

请引用https://plot.ly/matplotlib/modifying-a-matplotlib-figure/mpl_to_plotly库中有一个plotly.tools函数,它将从matplotlib图形返回一个绘图图形(然后可以将其返回到Graph的图形属性)。
编辑:刚注意到您刚才问了这个。也许以上是一个新功能,但它是最干净的方法。

关于python - 在plotly Dash中显示一个简单的matplotlib图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49851280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com