破折号图不显示

时间:2019-08-30 16:18:05

标签: python graph plotly-dash hyphen

我正在尝试使用Dash绘制图形。当我在Plotly中复制示例时,它可以正常工作,但是当我用自己的值更改一些变量并运行它时,除图形外,所有元素都将出现。

这是代码:

app = dash.Dash(__name__)

app.layout = html.Div([
    dcc.Graph(id='graph-with-slider'),
    dcc.Slider(
        id='year-slider',
        min=subset_date['Year'].unique().min(),
        max=subset_date['Year'].unique().max(),
        value=subset_date['Year'].unique().min(),
        step=None

    )

])

@app.callback(
    Output('graph-with-slider', 'figure'),
    [Input('year-slider', 'value')])
def update_figure(selected_year):
    return {
        'data': go.Scatter(x=subset_date.index,
                          y=subset_date['Value'],
                          mode='lines'),
        'layout': go.Layout(
            title='My Title'
        )

    }

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

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

这是一个简单的应用布局:

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

import pandas as pd
import plotly.graph_objs as go

# Step 1. Launch the application
app = dash.Dash()

# Step 2. Import the dataset

# Scimago Journal & Country Rank
# from www.scimagojr.com 
st = {'Date': ['2008-01-01', '2009-01-01', '2010-01-01', '2011-01-01', '2012-01-01', '2013-01-01', '2014-01-01', '2015-01-01', '2016-01-01','2017-01-01', '2018-01-01'],
       # 'Turkey': [26526, 30839, 33325, 34926, 36766, 40302, 41400, 44529, 47138, 44584,45582],
        'Iran': [20006, 24509, 30013, 39682, 41513, 42252, 44923, 45013, 52538, 56029, 60268]
        }
st = pd.DataFrame(st)
# range slider options
st['Date'] = pd.to_datetime(st.Date)
dates = ['2008-01-01', '2009-01-01', '2010-01-01', '2011-01-01', '2012-01-01', '2013-01-01', '2014-01-01', '2015-01-01', '2016-01-01','2017-01-01', '2018-01-01']
        



# Step 3. Create a plotly figure
trace_1 = go.Scatter(x = st.Date, y = st['Iran'],
                    name = 'Iran',
                    line = dict(width = 2,
                                color = 'rgb(229, 151, 50)'))
layout = go.Layout(title = 'Scimago Journal & Country Rank (Iran)',
                   hovermode = 'closest')
fig = go.Figure(data = [trace_1], layout = layout)


# Step 4. Create a Dash layout
app.layout = html.Div([
               
                # adding a plot
                dcc.Graph(id = 'plot', figure = fig),

                
                
                # range slider
               
                   
                    dcc.RangeSlider(id = 'slider',
                                    marks = {i : dates[i] for i in range(0, 11)},
                                    min = 0,
                                    max = 10,
                                    value = [1, 7])
])
                       
                   


# Step 5. Add callback functions
@app.callback(Output('plot', 'figure'),
             [
             Input('slider', 'value')])
def update_figure( input2):
    # filtering the data
    st2 = st[(st.Date > dates[input2[0]]) & (st.Date < dates[input2[1]])]
    # updating the plot
    trace_1 = go.Scatter(x = st2.Date, y = st2['Iran'],
                        name = 'Iran',
                      #  mode = 'markers'
                       line = dict(width = 2,
                                color = 'rgb(229, 151, 50)'))
                        
    
    fig = go.Figure(data = [trace_1], layout = layout)
    return fig
  
# Step 6. Add the server clause
if __name__ == '__main__':
    app.run_server(debug = False)