1. 概览
import plotly.express as px
fig = px.line(x=["a", "b", "c"], y=[1, 3, 2], title="sample figure")
print(fig)
fig.show()
输出结果:
Figure({
'data': [{'hovertemplate': 'x=%{x}<br>y=%{y}<extra></extra>',
'legendgroup': '',
'line': {'color': '#636efa', 'dash': 'solid'},
'mode': 'lines',
'name': '',
'orientation': 'v',
'showlegend': False,
'type': 'scatter',
'x': array(['a', 'b', 'c'], dtype=object),
'xaxis': 'x',
'y': array([1, 3, 2]),
'yaxis': 'y'}],
'layout': {'legend': {'tracegroupgap': 0},
'template': '...',
'title': {'text': 'sample figure'},
'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title': {'text': 'x'}},
'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'y'}}}
})
2. 访问数据结构
import json
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
from dash.dependencies import Input, Output
fig = px.line(x=["a", "b", "c"], y=[1, 3, 2], title="sample figure", height=325)
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Graph(id="graph", figure=fig),
html.Pre(
id="structure",
style={
"border": "thin lightgrey solid",
"overflow": "scoll",
"height": "275px"
}
)
])
@app.callback(Output("structure", "children"), [Input("graph", "figure")])
def display_structure(fig_json):
return json.dumps(fig_json, indent=2)
app.run_server(debug=True)