我想实现一个windrose (使用Dash)来显示风向数据。我使用了一个回调函数,其思想是在不同的时间显示数据-为此,我使用了一个滑块,可以在其中选择时间。这里的问题是,在选择时间后,我必须双击windrose,以便更新它并显示数据。
我使用了相同的代码和普通的线条图,在那里它工作得很好(这意味着它不需要双击就可以立即更新)。
提前感谢!
# visit http://127.0.0.1:8050/ in your web browser.
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import pandas as pd
from dash.dependencies import Input, Output
import logging
# Load data
df = pd.read_csv('..\\..\\data\\raw\\fake_wind.csv', #
index_col=0,
parse_dates=True) # finds dates "automatically"
df.index = pd.to_datetime(df['Date']) # convert argument to datetime
# Initialize the app
app = dash.Dash(__name__)
# app.config.suppress_callback_exceptions = True # ??
app.layout = html.Div(
children=[
html.H1("Wind Direction"),
dcc.Slider(id='windrose',
min=1,#min(df.index),
max=5,#max(df.index),
value=5,
marks={
0: '0 °F',
3: '3 °F',
5: '5 °F',
7.65: '7.65 °F',
10: '10 °F'
}
),
html.Div(id='output_container', children=[]),
#html.Br(),
#dcc.Graph(id='sun_map', figure={})
dcc.Graph(id='wind_map2', config={'displayModeBar': False}, animate=True)
])
import numpy as np
@app.callback(
dash.dependencies.Output(component_id='wind_map2', component_property='figure'),
[dash.dependencies.Input('windrose', 'value')])
def update_output(value):
#fig=make_subplots(rows=2, cols=2, specs=[[{'type': 'polar'}]*2]*2)
row = df.iloc[value,:]
barplot = go.Barpolar(
r = [np.random.randint(1,10)],
width=[10],
theta = [np.random.randint(1,360)],
marker_color=["#E4FF87", '#709BFF', '#709BFF', '#FFAA70', '#FFAA70', '#FFDF70', '#B6FFB4'],
marker_line_color="black",
marker_line_width=2,
opacity=0.8
)
fig = go.Figure(data=barplot)
return fig
#return 'You have selected "{}"'.format(value)
fig.update_layout(
xaxis=dict(title="time"),
template='plotly_dark',
paper_bgcolor='rgba(0, 0, 0, 0)',
plot_bgcolor='rgba(0, 0, 0, 0)',
#yaxis="W/s^2",
#yaxis2=dict(title="Celsius",
#overlaying='y',
#side='right'),
font=dict(
family="Courier New, monospace",
size=18,
color="RebeccaPurple"
)
)
return fig
# Run the app
if __name__ == '__main__':
app.run_server(debug=True) # "hot-reloading" (Dash automatically refreshes browser when changes)发布于 2021-09-21 12:15:17
现在回答可能有点晚了。然而,我认为你应该将滑块中的"updatemode“从”mouseup“改为”drag“。根据这里的文档,https://dash.plotly.com/dash-core-components/slider
mouseup更新模式(一个值等于:'mouseup',‘
’;默认的'mouseup'):确定组件应该何时更新其value属性。如果是mouseup (默认值),则仅当用户完成拖动滑块时,滑块才会触发其值。如果拖动,则滑块将在被拖动时不断更新其值。如果您希望在拖动过程中和拖动后执行不同的操作,请将updatemode保留为mouseup,并使用drag_value作为持续更新值。
https://stackoverflow.com/questions/66504377
复制相似问题