Plotly 更新破折号中的图形(打印)
我试图在仪表板上显示4个图形。 在这4个图中,我想在从1个图中选择后更新其中的3个。 有人能告诉我如何实现这一点吗? 或Plotly 更新破折号中的图形(打印),plotly,dashboard,plotly-dash,plotly.js,plotly-python,Plotly,Dashboard,Plotly Dash,Plotly.js,Plotly Python,我试图在仪表板上显示4个图形。 在这4个图中,我想在从1个图中选择后更新其中的3个。 有人能告诉我如何实现这一点吗? 或 为我提供一些示例应用程序 您只需要在单击时更新图形,而不是在悬停时更新 import dash import dash_core_components as dcc import dash_html_components as html import pandas as pd import plotly.graph_objs as go external_styleshee
为我提供一些示例应用程序 您只需要在单击时更新图形,而不是在悬停时更新
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objs as go
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
df = pd.read_csv(
'https://gist.githubusercontent.com/chriddyp/'
'cb5392c35661370d95f300086accea51/raw/'
'8e0768211f6b747c0db42a9ce9a0937dafcbd8b2/'
'indicators.csv')
available_indicators = df['Indicator Name'].unique()
app.layout = html.Div([
html.Div([
html.Div([
dcc.Dropdown(
id='crossfilter-xaxis-column',
options=[{'label': i, 'value': i} for i in available_indicators],
value='Fertility rate, total (births per woman)'
),
dcc.RadioItems(
id='crossfilter-xaxis-type',
options=[{'label': i, 'value': i} for i in ['Linear', 'Log']],
value='Linear',
labelStyle={'display': 'inline-block'}
)
],
style={'width': '49%', 'display': 'inline-block'}),
html.Div([
dcc.Dropdown(
id='crossfilter-yaxis-column',
options=[{'label': i, 'value': i} for i in available_indicators],
value='Life expectancy at birth, total (years)'
),
dcc.RadioItems(
id='crossfilter-yaxis-type',
options=[{'label': i, 'value': i} for i in ['Linear', 'Log']],
value='Linear',
labelStyle={'display': 'inline-block'}
)
], style={'width': '49%', 'float': 'right', 'display': 'inline-block'})
], style={
'borderBottom': 'thin lightgrey solid',
'backgroundColor': 'rgb(250, 250, 250)',
'padding': '10px 5px'
}),
html.Div([
dcc.Graph(
id='crossfilter-indicator-scatter',
clickData={'points': [{'customdata': 'Japan'}]}
)
], style={'width': '49%', 'display': 'inline-block', 'padding': '0 20'}),
html.Div([
dcc.Graph(id='x-time-series'),
dcc.Graph(id='y-time-series'),
], style={'display': 'inline-block', 'width': '49%'}),
html.Div(dcc.Slider(
id='crossfilter-year--slider',
min=df['Year'].min(),
max=df['Year'].max(),
value=df['Year'].max(),
marks={str(year): str(year) for year in df['Year'].unique()}
), style={'width': '49%', 'padding': '0px 20px 20px 20px'})
])
@app.callback(
dash.dependencies.Output('crossfilter-indicator-scatter', 'figure'),
[dash.dependencies.Input('crossfilter-xaxis-column', 'value'),
dash.dependencies.Input('crossfilter-yaxis-column', 'value'),
dash.dependencies.Input('crossfilter-xaxis-type', 'value'),
dash.dependencies.Input('crossfilter-yaxis-type', 'value'),
dash.dependencies.Input('crossfilter-year--slider', 'value')])
def update_graph(xaxis_column_name, yaxis_column_name,
xaxis_type, yaxis_type,
year_value):
dff = df[df['Year'] == year_value]
return {
'data': [go.Scatter(
x=dff[dff['Indicator Name'] == xaxis_column_name]['Value'],
y=dff[dff['Indicator Name'] == yaxis_column_name]['Value'],
text=dff[dff['Indicator Name'] == yaxis_column_name]['Country Name'],
customdata=dff[dff['Indicator Name'] == yaxis_column_name]['Country Name'],
mode='markers',
marker={
'size': 15,
'opacity': 0.5,
'line': {'width': 0.5, 'color': 'white'}
}
)],
'layout': go.Layout(
xaxis={
'title': xaxis_column_name,
'type': 'linear' if xaxis_type == 'Linear' else 'log'
},
yaxis={
'title': yaxis_column_name,
'type': 'linear' if yaxis_type == 'Linear' else 'log'
},
margin={'l': 40, 'b': 30, 't': 10, 'r': 0},
height=450,
hovermode='closest'
)
}
def create_time_series(dff, axis_type, title):
return {
'data': [go.Scatter(
x=dff['Year'],
y=dff['Value'],
mode='lines+markers'
)],
'layout': {
'height': 225,
'margin': {'l': 20, 'b': 30, 'r': 10, 't': 10},
'annotations': [{
'x': 0, 'y': 0.85, 'xanchor': 'left', 'yanchor': 'bottom',
'xref': 'paper', 'yref': 'paper', 'showarrow': False,
'align': 'left', 'bgcolor': 'rgba(255, 255, 255, 0.5)',
'text': title
}],
'yaxis': {'type': 'linear' if axis_type == 'Linear' else 'log'},
'xaxis': {'showgrid': False}
}
}
@app.callback(
dash.dependencies.Output('x-time-series', 'figure'),
[dash.dependencies.Input('crossfilter-indicator-scatter', 'clickData'),
dash.dependencies.Input('crossfilter-xaxis-column', 'value'),
dash.dependencies.Input('crossfilter-xaxis-type', 'value')])
def update_y_timeseries(clickData, xaxis_column_name, axis_type):
country_name = clickData['points'][0]['customdata']
dff = df[df['Country Name'] == country_name]
dff = dff[dff['Indicator Name'] == xaxis_column_name]
title = '<b>{}</b><br>{}'.format(country_name, xaxis_column_name)
return create_time_series(dff, axis_type, title)
@app.callback(
dash.dependencies.Output('y-time-series', 'figure'),
[dash.dependencies.Input('crossfilter-indicator-scatter', 'clickData'),
dash.dependencies.Input('crossfilter-yaxis-column', 'value'),
dash.dependencies.Input('crossfilter-yaxis-type', 'value')])
def update_x_timeseries(clickData, yaxis_column_name, axis_type):
dff = df[df['Country Name'] == clickData['points'][0]['customdata']]
dff = dff[dff['Indicator Name'] == yaxis_column_name]
return create_time_series(dff, axis_type, yaxis_column_name)
if __name__ == '__main__':
app.run_server(debug=True)
导入破折号
将仪表板核心组件作为dcc导入
将dash_html_组件导入为html
作为pd进口熊猫
导入plotly.graph_objs作为go
外部_样式表=['https://codepen.io/chriddyp/pen/bWLwgP.css']
app=dash.dash(名称,外部样式表=外部样式表)
df=pd.read\u csv(
'https://gist.githubusercontent.com/chriddyp/'
“cb5392c35661370d95f300086accea51/raw/”
“8E0768211F6B747C0DB42A9CE9A0937DAFCDB8B2/”
“indicators.csv”)
可用_indicators=df['Indicator Name'].unique()
app.layout=html.Div([
html.Div([
html.Div([
dcc.下拉列表(
id='crossfilter-xaxis-column',
选项=[{'label':i,'value':i}用于可用_指示器中的i],
value='生育率,总(每名妇女的出生率)'
),
放射性元素(
id='crossfilter-xaxis-type',
选项=[{'label':i,'value':i}表示['Linear','Log']]中的i,
value='Linear',
labelStyle={'display':'inline block'}
)
],
样式={'width':'49%,'display':'inline block'}),
html.Div([
dcc.下拉列表(
id='crossfilter-yaxis-column',
选项=[{'label':i,'value':i}用于可用_指示器中的i],
值='出生时预期寿命,总(年)'
),
放射性元素(
id='crossfilter-yaxis-type',
选项=[{'label':i,'value':i}表示['Linear','Log']]中的i,
value='Linear',
labelStyle={'display':'inline block'}
)
],style={'width':'49%,'float':'right','display':'inline block'})
],风格={
“borderBottom”:“细浅灰色实体”,
“背景色”:“rgb(250250250)”,
“填充”:“10px 5px”
}),
html.Div([
图(
id='crossfilter-indicator-scatter',
clickData={'points':[{'customdata':'Japan'}]}
)
],style={'width':'49%,'display':'inline block','padding':'020'}),
html.Div([
dcc.图(id='x-时间序列'),
dcc.图(id='y-时间序列'),
],style={'display':'inline block','width':'49%}),
Div(dcc.Slider(
id='crossfilter-year--slider',
min=df['Year'].min(),
max=df['Year'].max(),
值=df['Year'].max(),
marks={str(year):df['year']中年份的str(year)。unique()}
),style={'width':'49%,'padding':'0px 20px 20px'})
])
@app.callback(
破折号.dependencies.Output('crossfilter-indicator-scatter','figure'),
[dash.dependencies.Input('crossfilter-xaxis-column','value'),
dash.dependencies.Input('crossfilter-yaxis-column','value'),
dash.dependencies.Input('crossfilter-xaxis-type','value'),
dash.dependencies.Input('crossfilter-yaxis-type','value'),
dash.dependencies.Input('crossfilter-year--slider','value'))
def update_图形(xaxis_列名称、yaxis_列名称、,
xaxis_型,yaxis_型,
年份(单位价值):
dff=df[df['Year']==年份值]
返回{
“数据”:[go.Scatter(
x=dff[dff['Indicator Name']==xaxis\u column\u Name]['Value'],
y=dff[dff['Indicator Name']==yaxis\u column\u Name]['Value'],
text=dff[dff['Indicator Name']==yaxis\u column\u Name]['Country Name'],
customdata=dff[dff['Indicator Name']==yaxis\u column\u Name]['Country Name'],
mode='markers',
标记={
“尺寸”:15,
“不透明度”:0.5,
'line':{'width':0.5,'color':'white'}
}
)],
“布局”:go.layout(
xaxis={
“标题”:xaxis\u列\u名称,
'type':'linear'如果xaxis_type=='linear'或者'log'
},
亚克斯={
“标题”:yaxis\u列\u名称,
'type':'linear'如果yaxis_type=='linear'或者'log'
},
边距={'l':40,'b':30,'t':10,'r':0},
高度=450,
悬停模式
)
}
def创建时间序列(dff、轴类型、标题):
返回{
“数据”:[go.Scatter(
x=dff[‘年’],
y=dff[“值”],
模式='行+标记'
)],
“布局”:{
身高:225,
'边距':{'l':20,'b':30,'r':10,'t':10},
“注释”:[{
'x':0,'y':0.85,'xanchor':'left','yanchor':'bottom',
'xref':'paper','yref':'paper','showarrow':False,
'对齐':'左','bgcolor':'rgba(255,255,255,0.5)',
“文本”:标题
}],
'yaxis':{'type':'linear'如果axis_type=='linear'或者'log'},
'xaxis':{'showgrid':False}
}
}
@app.callback(
dash.dependencies.Output('x-time-series','figure'),
[破折号依赖项输入('crossfilter-indicator-scatter','clickData'),
dash.dependencies.Input('crossfilter-xaxis-column','value'),
dash.dependencies.Input('crossfilter-xaxis-type','value'))
def update_y_timeseries(单击数据、X轴列名称、轴类型):
国家/地区名称=点击数据['points'][0]['customdata']
dff=df[df['国家名称]==国家名称]
dff=dff[dff['Indicator Name']==xaxis\u column\u Name]
title='{}
{}'。格式(国家名称、xaxis列名称)
返回创建时间序列(dff、轴类型、标题)
@app.callback(
dash.dependencies.Output('y-time-series','figure'),
[破折号依赖项输入('crossfilter-indicator-scatter','clickData'),
破折号依赖项输入('crossfilter-yaxis-c