Python 仪表板交互式图形不会从数据帧更新
我试图创建一个条形图,它可以从Dash上的多选下拉菜单中更新,但当我选择一个区号来绘制时,没有任何变化 我的数据帧由大约11行4列组成:Python 仪表板交互式图形不会从数据帧更新,python,pandas,plotly-dash,Python,Pandas,Plotly Dash,我试图创建一个条形图,它可以从Dash上的多选下拉菜单中更新,但当我选择一个区号来绘制时,没有任何变化 我的数据帧由大约11行4列组成: Zip Code Area Name percent no internet Mean Income Past 12 Months 0 38126 South Forum 0.8308 26346 1 38106 South Memphis 0.8223 3140
Zip Code Area Name percent no internet Mean Income Past 12 Months
0 38126 South Forum 0.8308 26346
1 38106 South Memphis 0.8223 31403
2 38108 Hollywood 0.7356 31278
以下是回调的代码:
@app.callback(
dash.dependencies.Output('graph1', 'figure'),
[dash.dependencies.Input('demo-dropdown', 'value')])
def update_image_src(selector):
data = []
filtered_data = df[df['Zip Code'] == zip_code]
for zip_code in selector:
data.append(dict(
x=filtered_data['Area Name'][0],
y=filtered_data['percent no internet'][0],
type='bar',
name=zip_code))
figure = {
'data': data,
'layout': {
'title': 'Percent of homes lacking broadband internet',
'xaxis' : dict(
title=data[0],
titlefont=dict(
family='Courier New, monospace',
size=20,
color='#7f7f7f'
)),
'yaxis' : dict(
title='% No Internet',
titlefont=dict(
family='Helvetica, monospace',
size=20,
color='#7f7f7f'
))
}
}
return figure
IIUC,这是一个你想要的代码。我可以用您的原始代码说明的一点是,首先您不定义图的类型,其次,您可以使用isin一次获取全部,而不是遍历选择器。希望对你有帮助
import dash
from dash.dependencies import Output,Input
import dash_core_components as dcc
import dash_html_components as html
# the few rows of your question
import pandas as pd
df = pd.DataFrame( {'Zip Code': {0: 38126, 1: 38106, 2: 38108},
'Area Name': {0: 'South Forum', 1: 'South Memphis', 2: 'Hollywood'},
'percent no internet': {0: 0.8308, 1: 0.8223, 2: 0.7356},
'Mean Income Past 12 Months': {0: 26346, 1: 31403, 2: 31278}})
# main app
app = dash.Dash(__name__)
app.layout = html.Div(children=[
dcc.Dropdown(
id='demo-dropdown',
options=[
{'label': 38126, 'value': 38126},
{'label': 38106, 'value': 38106},
{'label': 38108, 'value': 38108}
],
value=[38126, 38106],
multi=True
),
dcc.Graph(id='graph1'),
])
@app.callback(
dash.dependencies.Output('graph1', 'figure'),
[dash.dependencies.Input('demo-dropdown', 'value')])
def update_image_src(selector):
# instead of iterating, use isin to select all the zipcode from the dropdown
filtered_data = df.loc[ df['Zip Code'].isin(selector),
['Area Name', 'percent no internet']]
figure = {
# in figure, see how the data is defined
'data': [{'x': filtered_data['Area Name'],
'y': filtered_data['percent no internet'],
'type': 'bar'}
],
'layout': {
'title': 'Percent of homes lacking broadband internet',
'xaxis' : dict(
title='Area Name', #change this, I'm not sure what you wanted
titlefont=dict(
family='Courier New, monospace',
size=20,
color='#7f7f7f'
)
),
'yaxis' : dict(
title='% No Internet',
titlefont=dict(
family='Helvetica, monospace',
size=20,
color='#7f7f7f'
)
)
}
}
return figure
if __name__ == '__main__':
app.run_server(debug=True)
编辑:要获得不同的颜色和图例,请在字典图中替换为:
figure = {
# in figure, see how is define the data
'data': [{'x': [area_name],
'y': [percent],
'type': 'bar',
'name': area_name}
for area_name, percent in filtered_data.to_numpy()
],
...
过滤后的邮政编码是否应该在selector中的邮政编码循环中,而不是之前?我尝试了这个方法,图形会更新并通过添加轴标题来识别更改,但数据本身仍然没有实际打印。它只是一个带有标题的空图表@Ben.TI看,你能在问题中以文本形式发布数据框吗?你可以点击编辑,在问题的底部,我看不到图像。问题可能来自从过滤的_数据获取数据的方式,实际上,执行此过滤的_数据['Area Name'][0]意味着您获取列'Area Name',以及标签为0而不是位置为0的索引,因此在无法查看数据的情况下,我不确定,但是我猜你的索引是不同的标签?@Ben.T我在问题的前三行加了一个@Ben.T我还尝试将所需的数据存储为一个列表,并以类似于so-l=[],x=df[df['Zip Code']='38126']['Zip Code'][0],l.appendx,l的方式输入它,因为我目前通过手动创建一个包含数据子集的字典来处理这些图。它是这样工作的,但我显然想从我的数据帧中提取数据,这就是为什么我尝试这种方法,因为我认为需要将数据作为列表输入,但上面的方法也不起作用:/this worked!只有一个问题,当它绘制多个项目时,每个新条都是相同的颜色,而不是新的颜色。同时,我也失去了传说。它正确地绘制了数据,但我没有这两个方面@Ben。T@ZachCornelison顺便说一句,现在我更好地理解了为什么你们需要一个循环,一开始我并没有得到它,因为你们只绘制了一系列的值!很抱歉,没问题,你提供的代码对我来说非常适合@本·T