Plotly-express-12-实现多子图subplots
在很多的实际业务需求中,需要将多个图形集中放置一个figure
中,而不是单独显示,在这种情况下我们需要使用子图的概念。本文中讲解如何在plotly中使用plotly.graph_objects
绘制各种形式的子图
Figures with subplots are created using the
make_subplots
function from theplotly.subplots
module.
参数详解
1 | make_subplots(rows=1, cols=1, shared_xaxes=False, shared_yaxes=False, start_cell='top-left', print_grid=False, horizontal_spacing=None, vertical_spacing=None, subplot_titles=None, column_widths=None, row_heights=None, specs=None, insets=None, column_titles=None, row_titles=None, x_title=None, y_title=None, **kwargs) |
1 | Parameters |
使用模块
1 | from plotly.subplots import make_subplots |
一行多列
1 | fig = make_subplots(rows=1, cols=2, |
一列多行
1 | fig = make_subplots(rows=3, cols=1) # 其实就是3*1的方式 |
多行多列
1 | fig = make_subplots(rows=2, cols=2, # 2*2 |
子图属性设置
- 第一个子图的起始位置
- 每个子图的标题
- 子图之间的间隔设置
- 如何共享x轴
- 每个子图中的文本信息设置及位置显示
- 子图右边的图例名称
- 子图的位置通过row/col实现
- 单独设置xy轴的名称
1 | fig = make_subplots(rows=2, cols=2, # 2*2 |
共享颜色轴
选择颜色
1 | ['aggrnyl', 'agsunset', 'algae', 'amp', 'armyrose', 'balance', |
1 | fig = make_subplots(rows=1, cols=2, shared_yaxes=True) |
1 | fig = make_subplots(rows=1, cols=2, shared_yaxes=True) |
自定义子图位置(占几行几列)
写法说明:
- {}:表示该位置出现一个子图
- {“rowspan”:2} 表示该位置的子图占据2行
- None:表示该位置上没有子图
1 | fig = make_subplots( |
子图类型
当不同的子图放在一起的时候,需要指定子图的类型,常见的类型有:
By default, the make_subplots function assumes that the traces that will be added to all subplots are 2-dimensional cartesian traces (e.g. scatter, bar, histogram, violin, etc.). Traces with other subplot types (e.g. scatterpolar, scattergeo, parcoords, etc.) are supporteed by specifying the type subplot option in the specs argument to make_subplots.
Here are the possible values for the type option:
- “xy”: 2D Cartesian subplot type for scatter, bar, etc. This is the default if no type is specified.
- “scene”: 3D Cartesian subplot for scatter3d, cone, etc.
- “polar”: Polar subplot for scatterpolar, barpolar, etc.
- “ternary”: Ternary subplot for scatterternary.
- “mapbox”: Mapbox subplot for scattermapbox.
- “domain”: Subplot type for traces that are individually positioned. pie, parcoords, parcats, etc.
1 | fig = make_subplots( |
又是收获满满的一篇💪