Graph
Create from Direct Data
  • data, list of dicts
  • layout, dict
  • frames, list of dicts, points to a list of figures, each of which will be cycled through when animation is triggered
    1. import dash
    2. import dash_html_components as html
    3. import dash_core_components as dcc
    4. app = dash.Dash(__name__)
    5. fig = {
    6. 'data': [
    7. {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF', 'showlegend': False}
    8. ],
    9. 'layout': {
    10. 'title': 'Dash Data Visualization',
    11. 'xaxis': {
    12. 'title': 'time'
    13. },
    14. 'height': 400,
    15. 'width': 800,
    16. 'paper_bgcolor': 'navy',
    17. 'plot_bgcolor': 'blue',
    18. 'margin': {
    19. 'l': 0,
    20. 'r': 0,
    21. }
    22. }
    23. }
    24. app.layout = html.Div(dcc.Graph(figure=fig))
    25. if __name__ == "__main__":
    26. app.run_server(debug=True)
    Create with Graph Objects
    1. import dash
    2. import dash_html_components as html
    3. import dash_core_components as dcc
    4. import plotly.graph_objects as go
    5. import pandas as pd
    6. app = dash.Dash(__name__)
    7.  
    8. df = pd.read_csv('https://plotly.github.io/datasets/country_indicators.csv')
    9. temp = df[df['Country Name'] == 'Canada']
    10. temp = temp[temp['Indicator Name'] == 'CO2 emissions (metric tons per capita)']
    11.  
    12. fig = go.Figure(go.Bar(x=temp['Year'], y=temp['Value']))
    13.  
    14. child = dcc.Graph(figure=fig)
    15. app.layout = html.Div(child)
    16. if __name__ == "__main__":
    17. app.run_server(debug=True)
    1. import dash
    2. import dash_html_components as html
    3. import dash_core_components as dcc
    4. import plotly.graph_objects as go
    5. import pandas as pd
    6. app = dash.Dash(__name__)
    7.  
    8. df = pd.read_csv('https://plotly.github.io/datasets/country_indicators.csv')
    9. temp = df[df['Country Name'] == 'Canada']
    10. temp = temp[temp['Indicator Name'] == 'CO2 emissions (metric tons per capita)']
    11.  
    12. fig = go.Figure()
    13.  
    14. trace = go.Bar(x=temp['Year'], y=temp['Value'])
    15.  
    16. fig.add_trace(trace)
    17.  
    18. child = dcc.Graph(figure=fig)
    19. app.layout = html.Div(child)
    20. if __name__ == "__main__":
    21. app.run_server(debug=True)
    Create with Plotly Express
    1. import dash
    2. import dash_html_components as html
    3. import dash_core_components as dcc
    4. import plotly.graph_objects as go
    5. import plotly.express as px
    6. import pandas as pd
    7. app = dash.Dash(__name__)
    8.  
    9. df = pd.read_csv('https://plotly.github.io/datasets/country_indicators.csv')
    10. temp = df[df['Country Name'] == 'Canada']
    11. temp = temp[temp['Indicator Name'] == 'CO2 emissions (metric tons per capita)']
    12.  
    13. fig = px.bar(temp, x='Year', y='Value')
    14.  
    15. child = dcc.Graph(figure=fig)
    16. app.layout = html.Div(child)
    17. if __name__ == "__main__":
    18. app.run_server(debug=True)
    Create with Pandas
    1. import dash
    2. import dash_html_components as html
    3. import dash_core_components as dcc
    4. import plotly.graph_objects as go
    5. import plotly.express as px
    6. import pandas as pd
    7. pd.options.plotting.backend = "plotly"
    8.  
    9. app = dash.Dash(__name__)
    10.  
    11. df = pd.read_csv('https://plotly.github.io/datasets/country_indicators.csv')
    12. temp = df[df['Country Name'] == 'Canada']
    13. temp = temp[temp['Indicator Name'] == 'CO2 emissions (metric tons per capita)']
    14.  
    15. fig = temp.plot.bar(x='Year', y='Value')
    16.  
    17. child = dcc.Graph(figure=fig)
    18. app.layout = html.Div(child)
    19. if __name__ == "__main__":
    20. app.run_server(debug=True)
    Update Layout
    1. import dash
    2. import dash_html_components as html
    3. import dash_core_components as dcc
    4. import plotly.graph_objects as go
    5. import plotly.express as px
    6. import pandas as pd
    7. app = dash.Dash(__name__)
    8.  
    9. df = pd.read_csv('https://plotly.github.io/datasets/country_indicators.csv')
    10. temp = df[df['Country Name'] == 'Canada']
    11. temp = temp[temp['Indicator Name'] == 'CO2 emissions (metric tons per capita)']
    12.  
    13. fig = px.bar(temp, x='Year', y='Value')
    14.  
    15. fig.update_layout(
    16. width=600,
    17. height=300,
    18. title = {'text':'Layout', 'x':0.5, 'y':0.95},
    19. plot_bgcolor='navy',
    20. paper_bgcolor='LightBlue',
    21. margin={'l': 40, 'b': 40, 't': 40, 'r': 0},
    22. hovermode='closest')
    23.  
    24. child = dcc.Graph(figure=fig)
    25. app.layout = html.Div(child)
    26. if __name__ == "__main__":
    27. app.run_server(debug=True)
    Subplots
    1. import dash
    2. import dash_html_components as html
    3. import dash_core_components as dcc
    4. import plotly.graph_objects as go
    5. from plotly.subplots import make_subplots
    6. import pandas as pd
    7. app = dash.Dash(__name__)
    8.  
    9. df = pd.read_csv('https://plotly.github.io/datasets/country_indicators.csv')
    10. temp = df[df['Country Name'] == 'Canada']
    11. temp = temp[temp['Indicator Name'] == 'CO2 emissions (metric tons per capita)']
    12. temp2 = df[df['Country Name'] == 'United State']
    13. temp2 = temp[temp['Indicator Name'] == 'CO2 emissions (metric tons per capita)']
    14.  
    15. fig = make_subplots(rows=1, cols=2)
    16.  
    17. trace_1 = go.Bar(x=temp['Year'], y=temp['Value'])
    18. trace_2 = go.Bar(x=temp2['Year'], y=temp2['Value'])
    19.  
    20. fig.add_trace(trace_1, row=1, col=1)
    21. fig.add_trace(trace_2, row=1, col=2)
    22.  
    23. fig.update_layout(
    24. width=600,
    25. height=300,
    26. title = {'text':'Layout', 'x':0.5, 'y':0.95},
    27. plot_bgcolor='navy',
    28. paper_bgcolor='LightBlue',
    29. margin={'l': 40, 'b': 40, 't': 40, 'r': 0},
    30. hovermode='closest')
    31.  
    32. child = dcc.Graph(figure=fig)
    33. app.layout = html.Div(child)
    34. if __name__ == "__main__":
    35. app.run_server(debug=True)
    Multiple Plots
    1. import dash
    2. import dash_html_components as html
    3. import dash_core_components as dcc
    4. import plotly.graph_objects as go
    5. from plotly.subplots import make_subplots
    6. import pandas as pd
    7. app = dash.Dash(__name__)
    8.  
    9. df = pd.read_csv('https://plotly.github.io/datasets/country_indicators.csv')
    10. temp = df[df['Country Name'] == 'Canada']
    11. temp = temp[temp['Indicator Name'] == 'CO2 emissions (metric tons per capita)']
    12. temp2 = df[df['Country Name'] == 'United State']
    13. temp2 = temp[temp['Indicator Name'] == 'CO2 emissions (metric tons per capita)']
    14.  
    15. fig = go.Figure()
    16.  
    17. trace_1 = go.Bar(x=temp['Year'], y=temp['Value'])
    18. trace_2 = go.Bar(x=temp2['Year'], y=temp2['Value'])
    19.  
    20. fig.add_trace(trace_1)
    21. fig.add_trace(trace_2)
    22.  
    23. child = dcc.Graph(figure=fig)
    24. app.layout = html.Div(child)
    25. if __name__ == "__main__":
    26. app.run_server(debug=True)
    Interactive Graph
  • hoverData, data when mouse is hovered
  • clickData, data selected by clicking
  • selectedData, data selected with box select or lasso select
  • relyoutData, click and drag on the graph to zoom or click on the zoom buttons in the graph's menu bar, or click on legend items
    1. import json
    2. import dash
    3. import dash_html_components as html
    4. import dash_core_components as dcc
    5. from dash.dependencies import Input, Output
    6. import plotly.graph_objects as go
    7. from plotly.subplots import make_subplots
    8. import pandas as pd
    9. app = dash.Dash(__name__)
    10.  
    11. df = pd.read_csv('https://plotly.github.io/datasets/country_indicators.csv')
    12. temp = df[df['Country Name'] == 'Canada']
    13. temp = temp[temp['Indicator Name'] == 'CO2 emissions (metric tons per capita)']
    14.  
    15. fig = go.Figure()
    16.  
    17. trace_1 = go.Bar(x=temp['Year'], y=temp['Value'])
    18.  
    19. fig.add_trace(trace_1)
    20.  
    21. child = dcc.Graph(figure=fig, id='graph')
    22. app.layout = html.Div([child, html.Div(id='output')])
    23.  
    24. @app.callback(
    25. Output('output', 'children'),
    26. Input('graph', 'hoverData'))
    27. def display_hover_data(hoverData):
    28. print(hoverData)
    29. return json.dumps(hoverData, indent=2)
    30. if __name__ == "__main__":
    31. app.run_server(debug=True)
    Reference
  • Graph Objects
  • Express
  • Python Figure Reference
  • Fundamentals
  • Intro to Animations in Python
  • JavaScript Figure Reference