另一个 dash/plotly 困境
Another dash/plotly dilemma
我不想一直发关于 dash\plotly 的帖子,但我不明白这里出了什么问题。我已经从 plotly 站点复制并粘贴了代码,以便显示带有下拉菜单的饼图。当我创建仪表板时,有一个下拉菜单显示所有类别,但看不到饼图。请帮助我了解出了什么问题。代码如下:
import pandas as pd
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
# This dataframe has 244 lines, but 4 distinct values for `day`
url="https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DS0321EN-SkillsNetwork/datasets/spacex_launch_dash.csv"
spacex_df=pd.read_csv(url)
spacex_df.rename(columns={'Launch Site':'Site'}, inplace=True)
app = dash.Dash(__name__)
app.layout = html.Div([
html.P("site-dropdown:"),
dcc.Dropdown(
id='Site',
value='Site',
options=[{'value': x, 'label': x}
for x in ['CCAFS LC-40', 'CCAFS SLC-40', 'KSC LC-39A', 'VAFB SLC-4E']],
clearable=False
),
dcc.Graph(id="pie-chart"),
])
@app.callback(
Output("pie-chart", "figure"),
[Input("Launch Site", "value")])
def generate_chart(Site, values):
fig = px.pie(spacex_df, values='Site', names='Site')
return fig
app.run_server()
我认为你的代码的主要问题是你的 generate_chart
函数有两个参数,但是 @app.callback
装饰器只有一个输入参数,而 values
参数在px.pie中,您需要传递一个列名,其值可以相加。在这种情况下使用 px.pie 的一个问题是,如果您对 'class' 列中的值求和,则表示成功的 0 总和为 0。
由于您在评论中解释说您真正想要在饼图中显示的是成功和失败的计数('class'
列中的 0 和 1),我认为它会是最容易总结特定 'Site'
的 0 和 1 的数量,并将参数 labels=['success','failure']
和 values=[success_count, failure_count]
传递给 go.Pie
.
import pandas as pd
import dash
from dash import html, dcc
from dash.dependencies import Input, Output
import plotly.graph_objects as go
import plotly.express as px
# This dataframe has 244 lines, but 4 distinct values for `day`
url="https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DS0321EN-SkillsNetwork/datasets/spacex_launch_dash.csv"
spacex_df=pd.read_csv(url)
spacex_df.rename(columns={'Launch Site':'Site'}, inplace=True)
app = dash.Dash(__name__)
app.layout = html.Div([
html.P("site-dropdown:"),
dcc.Dropdown(
id='Site',
value='Site',
options=[{'value': x, 'label': x}
for x in ['CCAFS LC-40', 'CCAFS SLC-40', 'KSC LC-39A', 'VAFB SLC-4E']],
clearable=False
),
dcc.Graph(id="pie-chart"),
])
@app.callback(
Output("pie-chart", "figure"),
[Input("Site", "value")])
def generate_chart(value):
pie_data = spacex_df[spacex_df['Site'] == value]
success_count = sum(pie_data['class'] == 0)
failure_count = sum(pie_data['class'] == 1)
fig = go.Figure(data=[go.Pie(labels=['success','failure'], values=[success_count, failure_count])])
fig.update_layout(title=f"Site: {value}")
return fig
if __name__ == '__main__':
app.run_server(debug=True)
我不想一直发关于 dash\plotly 的帖子,但我不明白这里出了什么问题。我已经从 plotly 站点复制并粘贴了代码,以便显示带有下拉菜单的饼图。当我创建仪表板时,有一个下拉菜单显示所有类别,但看不到饼图。请帮助我了解出了什么问题。代码如下:
import pandas as pd
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
# This dataframe has 244 lines, but 4 distinct values for `day`
url="https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DS0321EN-SkillsNetwork/datasets/spacex_launch_dash.csv"
spacex_df=pd.read_csv(url)
spacex_df.rename(columns={'Launch Site':'Site'}, inplace=True)
app = dash.Dash(__name__)
app.layout = html.Div([
html.P("site-dropdown:"),
dcc.Dropdown(
id='Site',
value='Site',
options=[{'value': x, 'label': x}
for x in ['CCAFS LC-40', 'CCAFS SLC-40', 'KSC LC-39A', 'VAFB SLC-4E']],
clearable=False
),
dcc.Graph(id="pie-chart"),
])
@app.callback(
Output("pie-chart", "figure"),
[Input("Launch Site", "value")])
def generate_chart(Site, values):
fig = px.pie(spacex_df, values='Site', names='Site')
return fig
app.run_server()
我认为你的代码的主要问题是你的 generate_chart
函数有两个参数,但是 @app.callback
装饰器只有一个输入参数,而 values
参数在px.pie中,您需要传递一个列名,其值可以相加。在这种情况下使用 px.pie 的一个问题是,如果您对 'class' 列中的值求和,则表示成功的 0 总和为 0。
由于您在评论中解释说您真正想要在饼图中显示的是成功和失败的计数('class'
列中的 0 和 1),我认为它会是最容易总结特定 'Site'
的 0 和 1 的数量,并将参数 labels=['success','failure']
和 values=[success_count, failure_count]
传递给 go.Pie
.
import pandas as pd
import dash
from dash import html, dcc
from dash.dependencies import Input, Output
import plotly.graph_objects as go
import plotly.express as px
# This dataframe has 244 lines, but 4 distinct values for `day`
url="https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DS0321EN-SkillsNetwork/datasets/spacex_launch_dash.csv"
spacex_df=pd.read_csv(url)
spacex_df.rename(columns={'Launch Site':'Site'}, inplace=True)
app = dash.Dash(__name__)
app.layout = html.Div([
html.P("site-dropdown:"),
dcc.Dropdown(
id='Site',
value='Site',
options=[{'value': x, 'label': x}
for x in ['CCAFS LC-40', 'CCAFS SLC-40', 'KSC LC-39A', 'VAFB SLC-4E']],
clearable=False
),
dcc.Graph(id="pie-chart"),
])
@app.callback(
Output("pie-chart", "figure"),
[Input("Site", "value")])
def generate_chart(value):
pie_data = spacex_df[spacex_df['Site'] == value]
success_count = sum(pie_data['class'] == 0)
failure_count = sum(pie_data['class'] == 1)
fig = go.Figure(data=[go.Pie(labels=['success','failure'], values=[success_count, failure_count])])
fig.update_layout(title=f"Site: {value}")
return fig
if __name__ == '__main__':
app.run_server(debug=True)