破折号无法工作(下拉列表和条形图)
我似乎无法制作仪表板,主要问题是在下拉列表和绘图中(条形图)。我尝试了大多数东西,但我仍然不知道问题所在。有人知道我的代码怎么了吗? “人口”基本上是“祖国”。
这是我的代码:
import dash
from jupyter_dash import JupyterDash # pip install dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objs as go
from dash.dependencies import Input, Output
df = pd.read_csv(r'C:\Users\mrsta\OneDrive\Desktop\cpsdata.csv')
app = JupyterDash()
#AppLayout
app.layout = html.Div([
html.H1("Population in a city", style={'text-align': 'center'}),
dcc.Dropdown(
id='population-dropdown',
options=[{'label': i, 'value': i} for i in list(df.Native_Country_of_Father.unique())],
placeholder="Please select..",
searchable=True
),
dcc.Slider(
id='year-slider',
min=df['Year'].min(),
max=df['Year'].max(),
value=df['Year'].min(),
step=None,
marks={str(Year): str(Year) for Year in df['Year'].unique()}
),
dcc.Graph(id='graph-with-slider'),
],
style={'width': '48%', 'display': 'inline-block'})
#App CallBack
@app.callback(
Output('graph-with-slider', 'figure'),
[Input('year-slider', 'value'),
Input('population-dropdown','value')])
#Figure Update
def update_figure(selected_year, selected_population):
filtered_df = df[df['Native_Country_of_Father']] == selected_population
filtered_df = filtered_df[filtered_df.Year == selected_year]
traces = []
for i in df.Native_Country_of_Father.unique():
df_by_population = filtered_df[filtered_df['selected_population'] == i]
traces.append(go.Bar(
x=df_by_population['City'],
y=df_by_population['Native_Country_of_Father'], #Numberofpopulation
text=df_by_continent['City'],
barmode='stack'),
name=i
)
return {
'data': traces,
'layout': go.Layout(
margin={'l': 40, 'b': 40, 't': 10, 'r': 10},
legend={'x': 0, 'y': 1},
hovermode='closest'
)
}
if __name__ =='__main__':
app.run_server(mode="external")
这是我的数据框架和我收到的错误消息:
< a href =“ https://i.sstatic.net/hun2t.png” rel =“ nofollow noreferrer”>
我从多个来源裁剪并编辑了此图像。
I can't seem to make my dashboard and the main problem is in the dropdown and the plot (bar chart). I have tried most of the stuffs but I still can't figure out what's the problem. Does anyone know what's wrong with my code? The 'population' is basically the 'native country'.
I uploaded my data here:
https://github.com/standatagithub/cpsdata/blob/main/cpsdata.csv
This is my code:
import dash
from jupyter_dash import JupyterDash # pip install dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objs as go
from dash.dependencies import Input, Output
df = pd.read_csv(r'C:\Users\mrsta\OneDrive\Desktop\cpsdata.csv')
app = JupyterDash()
#AppLayout
app.layout = html.Div([
html.H1("Population in a city", style={'text-align': 'center'}),
dcc.Dropdown(
id='population-dropdown',
options=[{'label': i, 'value': i} for i in list(df.Native_Country_of_Father.unique())],
placeholder="Please select..",
searchable=True
),
dcc.Slider(
id='year-slider',
min=df['Year'].min(),
max=df['Year'].max(),
value=df['Year'].min(),
step=None,
marks={str(Year): str(Year) for Year in df['Year'].unique()}
),
dcc.Graph(id='graph-with-slider'),
],
style={'width': '48%', 'display': 'inline-block'})
#App CallBack
@app.callback(
Output('graph-with-slider', 'figure'),
[Input('year-slider', 'value'),
Input('population-dropdown','value')])
#Figure Update
def update_figure(selected_year, selected_population):
filtered_df = df[df['Native_Country_of_Father']] == selected_population
filtered_df = filtered_df[filtered_df.Year == selected_year]
traces = []
for i in df.Native_Country_of_Father.unique():
df_by_population = filtered_df[filtered_df['selected_population'] == i]
traces.append(go.Bar(
x=df_by_population['City'],
y=df_by_population['Native_Country_of_Father'], #Numberofpopulation
text=df_by_continent['City'],
barmode='stack'),
name=i
)
return {
'data': traces,
'layout': go.Layout(
margin={'l': 40, 'b': 40, 't': 10, 'r': 10},
legend={'x': 0, 'y': 1},
hovermode='closest'
)
}
if __name__ =='__main__':
app.run_server(mode="external")
This is my dataframe and the error message I received:
This is how I think my output should look like. I crop and edited this image from multiple sources.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有多种因素阻止它起作用。首先是下拉菜没有出现。原因是数据包含NAN,需要删除。删除的结果列表是DD_LIST。接下来,将滑块更改为范围滑块。默认值是年度最低和最低年度的最小值。这是2005年。接下来,由于我们正在循环浏览图,而无需一次绘制数据,因此我们正在绘制过滤后的数据框架。条形图类型已分组,但是预期的输出不在组模式下,因此已禁用。图形更新功能的返回仅重写为无花果。通过更新滑块右侧的值来更新图。 selected_yaer 1 是右侧的值。进行此规范是为了了解滑块的返回值。如果按原样使用滑块,则需要修改代码,因为条件是滑块必须大于或等于开始年份,并且小于或等于或等于最终值。如果一年的选择是一年,则需要将其更改为下拉截止按钮。
There were multiple factors that prevented it from working. The first is that the dropdowns do not appear. The reason for this is that the data contains nan and needs to be removed. The resulting list that was removed is dd_list. Next, the slider is changed to a range slider. The default value is the minimum and minimum of the year plus 1. Here it is 2005. Next, since we are looping through the graph without drawing the data all at once, we are graphing the filtered data frames. The bar chart type is grouped, but the expected output is not in group mode, so it is disabled. The return of the graph update function is rewritten to fig only. The graph is updated by changing the value on the right side of the slider. selected_yaer1 is the value on the right side. This specification was made to understand the return value of the slider. If the slider is used as it is, the code will need to be modified because the condition is that the slider must be greater than or equal to the start year and less than or equal to the end value. If the year selection is to be a single year, it will need to be changed to a drop-down or radio button.