破折号-日期未出现在滑块行上

时间:2018-10-09 09:44:19

标签: python plotly plotly-dash

下面的代码可以正常工作,但滑块行上没有日期(请检查屏幕截图):

Dash Graph with Slider (screenshot)

excel文件中的“日期”列具有以下格式:“ yyyy-mm-dd”,但日期始终为01:2017-12-01; 2017-05-01; 2017-02-01; 2017-08-01等 因此,基本上,“日期”列仅包含2017年的月份和月份。

当我在Jupyter Notebook中测试输出时,我可以看到以下内容为“滑块标记”输出:

滑块(id ='date-slider',标记= {'2017-05-01':'2017-05-01','2017-02-01':'2017-02-01','2017 -01-01':'2017-01-01','2017-09-01':'2017-09-01','2017-04-01':'2017-04-01','2017-11 -01':'2017-11-01','2017-03-01':'2017-03-01','2017-10-01':'2017-10-01','2017-07-01 ':'2017-07-01','2017-08-01':'2017-08-01','2017-06-01':'2017-06-01','2017-12-01': '2017-12-01'},值='2017-01-01',最小值='2017-01-01',最大值='2017-12-01')

我确定日期格式有些问题,但不知道如何解决该问题。

这是代码本身:

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 datetime
import numpy as np

df = pd.read_excel("stats3 - Copy.xlsx",sheet_name=0)

#sort dataframe by date for the line plot
df2 = df.sort_values('date')

games = df.game.unique()

app = dash.Dash()

app.css.append_css({'external_url': 'https://cdn.rawgit.com/plotly/dash-app-stylesheets/2d266c578d2a6e8850ebce48fdb52759b2aef506/stylesheet-oil-and-gas.css'})

app.layout = html.Div([

html.Div(children='''You can select the game title you are interested in for the detailed observation on the drop-down below:''',
    style={'font-family':'Helvetica, monospace', 'color':'#59abe3'}, className="row"),

html.Div([
html.Div([dcc.Dropdown(
    id='game',
    options=[{'label':i, 'value':i} for i in games],
    value='',
    placeholder='Please select the game',
    multi=True)],
    className='nine columns')], className="row"),

    html.Div([
    html.Div([
    dcc.Graph(id='graph-with-slider')],
    className='nine columns')], className="row"),

html.Div(children='''You can select the date you are interested in on the slider below:''', style={'font-family':'Helvetica, monospace', 'color':'#59abe3'},
className="row"),

html.Div([
html.Div([
dcc.Slider(
        id='date-slider',
        min=df2['date'].apply(lambda x: x.strftime('%Y-%m-%d')).min(),
        max=df2['date'].apply(lambda x: x.strftime('%Y-%m-%d')).max(),
        value=df2['date'].apply(lambda x: x.strftime('%Y-%m-%d')).min(),
        step=None,
        marks={str(date): str(date) for date in df2['date'].apply(lambda x: x.strftime('%Y-%m-%d')).unique()}
    )], className='nine columns'),
    ], className="row")
])

@app.callback(
    dash.dependencies.Output('graph-with-slider', 'figure'),
    [dash.dependencies.Input('date-slider', 'value'),
    dash.dependencies.Input('game', 'value')])
def update_figure(selected_date, game):
    filtered_df = df.loc[df2['date'].apply(lambda x: x.strftime('%Y-%m-%d')) == selected_date]

    if (game != '' and game is not None):
        filtered_df = filtered_df[df.game.str.contains('|'.join(game))]

    traces = []
    for i in filtered_df.game.unique():
        df_by_game = filtered_df[filtered_df['game'] == i]
        traces.append(go.Scatter(
            x=df_by_game['conversions'],
            y=round((df_by_game['revenue']),2),
            text="Advertiser: " + df_by_game['advertiser'] + "<br>" + "Publisher: " + df_by_game['affiliate'],
            mode='markers',
            opacity=0.7,
            marker={'size': 15,},
            name=i))

    return {
        'data': traces,
        'layout': go.Layout(
            xaxis={'title': 'Conversions'},
            yaxis={'title': 'Revenue'},
            margin={'l': 40, 'b': 40, 't': 50, 'r': 10},
            legend=dict(x=-0.2, y=0.5),
            hovermode='closest',
            title="Something")
            }

if __name__ == '__main__':
    app.run_server()

0 个答案:

没有答案