Я новичок в питоне и тире.Я пришел из C ++ фона.Я следую уроку 3 « Basic Dash Callbacks ».И я хотел сделать scatter или (Scatter3d) для « gapminderDataFiveYear.csv » с несколькими «выпадающими» и «радиоэлементами».Но я пробовал много раз, однако данные не отображаются на графике рассеяния.Пожалуйста, помогите с ошибками в коде ниже:
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objs as go
import numpy as np
import webbrowser as wb
from os import listdir
from os.path import isfile, join
import numpy as np
#
df = pd.read_csv(
'https://raw.githubusercontent.com/plotly/'
'datasets/master/gapminderDataFiveYear.csv')
df.to_csv('test.csv')
df = pd.read_csv('./test.csv')
df['test'] = np.random.randint (10, 100, size = len(df))
col_to_choose = df.loc[:, df.dtypes == np.float64].columns.values.tolist()
app = dash.Dash()
app.layout = html.Div([
html.Div([
html.Div([
dcc.Dropdown(
id = 'xaxis-column',
options = [{'label' : i, 'value': i} for i in col_to_choose],
value = 'pop'
),
dcc.RadioItems(
id = 'xaxis-type',
options = [{'label' : i, 'value' : i} for i in ['Linear', 'Log']],
value = 'Linear',
labelStyle = {'display':'inline-block'}
)
],
style = {'width' : '48%', 'display' : 'inline-block'}),
html.Div([
dcc.Dropdown(
id = 'yaxis-column',
options = [{'label' : i, 'value': i} for i in col_to_choose],
value = 'gdpPercap'
),
dcc.RadioItems(
id = 'yaxis-type',
options = [{'label' : i, 'value' : i} for i in ['Linear', 'Log']],
value = 'log',
labelStyle = {'display':'inline-block'}
)
],
style = {'width' : '48%', 'display' : 'inline-block'}),
]),
dcc.Graph(id = 'graph-practice'),
dcc.Slider(
id = 'year-slider',
min = df['year'].min(),
max = df['year'].max(),
value = df['year'].max(),
step = None,
marks = {str(year):str(year) for year in df ['year'].unique()}
)
])
@app.callback(
dash.dependencies.Output('graph-practice', 'figure'),
[dash.dependencies.Input('xaxis-column', 'value'),
dash.dependencies.Input('yaxis-column', 'value'),
dash.dependencies.Input('xaxis-type', 'value'),
dash.dependencies.Input('yaxis-type', 'value'),
dash.dependencies.Input('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['xaxis_column_name'].values,
y = dff['yaxis_column_name'].values,
text = dff['country'],
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': 40, 't':10, 'r':0},
hovermode = 'closest'
)
}
if __name__ == '__main__':
wb.open_new('http://127.0.0.1:8050/')
app.run_server(debug = False)
введите описание изображения здесь