[ SKILL_DOCUMENTATION ]
# 布局、样式与自定义
## 子图 (Subplots)
### 创建子图
python
from plotly.subplots import make_subplots
import plotly.graph_objects as go
# 基础网格
fig = make_subplots(rows=2, cols=2)
# 在指定位置添加轨迹
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1)
fig.add_trace(go.Bar(x=['A', 'B', 'C'], y=[1, 3, 2]), row=1, col=2)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[2, 3, 4]), row=2, col=1)
### 子图选项
python
fig = make_subplots(
rows=2, cols=2,
# 标题
subplot_titles=('图表 1', '图表 2', '图表 3', '图表 4'),
# 自定义尺寸
column_widths=[0.7, 0.3],
row_heights=[0.4, 0.6],
# 间距
horizontal_spacing=0.1,
vertical_spacing=0.15,
# 共享坐标轴
shared_xaxes=True, # 或 'columns', 'rows', 'all'
shared_yaxes=False,
# 轨迹类型 (可选,用于混合类型)
specs=[[{'type': 'scatter'}, {'type': 'bar'}],
[{'type': 'surface'}, {'type': 'table'}]]
)
### 混合子图类型
python
from plotly.subplots import make_subplots
import plotly.graph_objects as go
# 2D 和 3D 子图
fig = make_subplots(
rows=1, cols=2,
specs=[[{'type': 'scatter'}, {'type': 'scatter3d'}]]
)
fig.add_trace(go.Scatter(x=[1, 2], y=[3, 4]), row=1, col=1)
fig.add_trace(go.Scatter3d(x=[1, 2], y=[3, 4], z=[5, 6]), row=1, col=2)
### 自定义子图坐标轴
python
# 更新特定子图坐标轴
fig.update_xaxes(title_text='X 轴标签', row=1, col=1)
fig.update_yaxes(title_text='Y 轴标签', range=[0, 100], row=2, col=1)
# 更新所有 X 轴
fig.update_xaxes(showgrid=True, gridcolor='lightgray')
### 共享颜色标尺
python
fig = make_subplots(rows=1, cols=2)
fig.add_trace(go.Bar(x=['A', 'B'], y=[1, 2],
marker=dict(color=[1, 2], coloraxis='coloraxis')),
row=1, col=1)
fig.add_trace(go.Bar(x=['C', 'D'], y=[3, 4],
marker=dict(color=[3, 4], coloraxis='coloraxis')),
row=1, col=2)
fig.update_layout(coloraxis=dict(colorscale='Viridis'))
## 模板与主题
### 内置模板
python
import plotly.express as px
import plotly.io as pio
# 可用模板
templates = [
'plotly', # 默认
'plotly_white', # 白色背景
'plotly_dark', # 深色主题
'ggplot2', # ggplot2 风格
'seaborn', # Seaborn 风格
'simple_white', # 极简白
'presentation', # 用于演示
'xgr