1. 概览

  1. import plotly.express as px
  2. fig = px.line(x=["a", "b", "c"], y=[1, 3, 2], title="sample figure")
  3. print(fig)
  4. fig.show()

输出结果:

  1. Figure({
  2. 'data': [{'hovertemplate': 'x=%{x}<br>y=%{y}<extra></extra>',
  3. 'legendgroup': '',
  4. 'line': {'color': '#636efa', 'dash': 'solid'},
  5. 'mode': 'lines',
  6. 'name': '',
  7. 'orientation': 'v',
  8. 'showlegend': False,
  9. 'type': 'scatter',
  10. 'x': array(['a', 'b', 'c'], dtype=object),
  11. 'xaxis': 'x',
  12. 'y': array([1, 3, 2]),
  13. 'yaxis': 'y'}],
  14. 'layout': {'legend': {'tracegroupgap': 0},
  15. 'template': '...',
  16. 'title': {'text': 'sample figure'},
  17. 'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title': {'text': 'x'}},
  18. 'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'y'}}}
  19. })

2. 访问数据结构

  1. import json
  2. import dash
  3. import dash_core_components as dcc
  4. import dash_html_components as html
  5. import plotly.express as px
  6. from dash.dependencies import Input, Output
  7. fig = px.line(x=["a", "b", "c"], y=[1, 3, 2], title="sample figure", height=325)
  8. app = dash.Dash(__name__)
  9. app.layout = html.Div([
  10. dcc.Graph(id="graph", figure=fig),
  11. html.Pre(
  12. id="structure",
  13. style={
  14. "border": "thin lightgrey solid",
  15. "overflow": "scoll",
  16. "height": "275px"
  17. }
  18. )
  19. ])
  20. @app.callback(Output("structure", "children"), [Input("graph", "figure")])
  21. def display_structure(fig_json):
  22. return json.dumps(fig_json, indent=2)
  23. app.run_server(debug=True)