1. 概览

2. 安装

  1. import plotly.graph_objects as go
  2. fig = go.Figure(data=go.Bar(y=[2, 3, 1]))
  3. # you can also use `fig.show()`
  4. fig.write_html("first_figure.html", auto_open=True)

3. 在Dash中使用Plotly

  1. import dash
  2. import dash_core_components as dcc
  3. import dash_html_components as html
  4. import plotly.graph_objects as go
  5. from dash.dependencies import Input, Output
  6. app = dash.Dash(__name__)
  7. app.layout = html.Div([
  8. html.P("Color:"),
  9. dcc.Dropdown(
  10. id="dropdown",
  11. options=[
  12. {"label": x, "value": x}
  13. for x in ["Gold", "MediumTurquoise", "LightGreen"]
  14. ],
  15. value="Gold",
  16. clearable=False
  17. ),
  18. dcc.Graph(id="graph")
  19. ])
  20. @app.callback(
  21. Output("graph", "figure"),
  22. [Input("dropdown", "value")])
  23. def display_color(color):
  24. fig = go.Figure(data=go.Bar(y=[2, 3, 1], marker_color=color))
  25. return fig
  26. app.run_server(debug=True)

image.pngimage.png

clearable 如果设置为 True,则在下拉框中会出现一个 x 清除选项

4. Jupyter Notebook支持

  1. import plotly.graph_objects as go
  2. fig = go.Figure(data=go.Bar(y=[2, 3, 1]))
  3. fig.show()

💡 你也可以直接使用 FigureWidget 对象,改写上面语句为 fig = go.FigureWidget(data=go.Bar(y=[2, 3, 1])) ,显示的页面效果是一样的。

5. JupyterLab支持

6. 静态图片导出

6.1 Kaleido

6.2 Orca

7. Geo地图扩展支持

8. Chart Studio支持

9. Dash是什么?

  1. import dash
  2. import plotly.graph_objects as go
  3. import dash_core_components as dcc
  4. import dash_html_components as html
  5. fig = go.Figure()
  6. app = dash.Dash()
  7. app.layout = html.Div([
  8. dcc.Graph(figure=fig)
  9. ])
  10. app.run_server(debug=True, use_reloader=False)