我有一个包含10个土壤样本的csv文件。每个样本都有一些参数,例如OM和Clay,此外还有频谱(从400到750)。 我正在尝试创建一个包含3个部分的仪表板:
,当我将鼠标悬停在第一幅图中的某个点上时,我希望统计数据和频谱图能够自动更新。 我能够使用统计数据执行此操作,但是我无法使用频谱进行操作。 我不知道我是在正确读取数据还是回调出现问题。
我是python和dash的新手,所以需要您的帮助 谢谢
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.offline as pyo
import pandas as pd
import cufflinks as cf
from dash.dependencies import Input, Output
app = dash.Dash()
df = pd.read_csv('test_GitHub.csv', index_col=None, na_values=['NA'],sep=',',low_memory=False)
app.layout = html.Div([ #global title
html.Div([ #header
html.H1('Title'),
html.H3('Additional title')
]),
html.Div([
dcc.Graph(
id = 'test_scatter',
figure = {
'data': [go.Scatter(
x = df['Clay'],
y = df['Sand'],
text = df['Soil'],
hoverinfo = ['text','x','y'],
mode = 'markers'
)],
'layout': go.Layout(
title = 'Plot',
xaxis = {'title': 'Clay'},
yaxis = {'title': 'Sand'},
hovermode = 'closest'
)}),
dcc.Markdown(
id = 'stats',
),
dcc.Graph(
id = 'spec',
figure = {
'data': [go.Scatter(
x = df['400':'750'],
y = df.iloc[soil_index]['400':'750'],
text = df['Soil'],
hoverinfo = ['text','x','y'],
mode = 'markers'
)],
'layout': go.Layout(
title = 'Spectrum',
xaxis = {'title': 'Wavelength (nm)'},
yaxis = {'title': 'Value'},
hovermode = 'closest'
)})
],style = {'width': '50%', 'height': '50%', 'display': 'inline-block'})])
@app.callback(
Output('stats', 'children'),
[Input('test_scatter','hoverData')])
def callback_stats(hoverData):
soil_index = hoverData['points'][0]['pointIndex']
stats = """
The soil is located in Latitude {} and Longitude {} and contains:
{}(%) Organic Matter
{}(% wt.) Clay
{}(% wt.) Silt
{}(% wt.) Sand
""".format(df.iloc[soil_index]['OM'],
df.iloc[soil_index]['Clay'],
df.iloc[soil_index]['Silt'],
df.iloc[soil_index]['Sand'])
return stats
@app.callback(
Output('spec', 'children'),
[Input('test_scatter','hoverData')])
def callback_spec(hoverData):
soil_index = hoverData['points'][0]['pointIndex']
return stats
if name == 'main':
app.run_server()