[ SKILL_DOCUMENTATION ]
# Plotly 图表类型
按类别组织的图表类型综合指南。
## 基础图表
### 散点图
python
import plotly.express as px
fig = px.scatter(df, x='x', y='y', color='category', size='size')
# 带趋势线
fig = px.scatter(df, x='x', y='y', trendline='ols')
### 折线图
python
fig = px.line(df, x='date', y='value', color='group')
# 从宽格式数据创建多条线
fig = px.line(df, x='date', y=['metric1', 'metric2', 'metric3'])
### 柱状图
python
# 垂直柱状图
fig = px.bar(df, x='category', y='value', color='group')
# 水平柱状图
fig = px.bar(df, x='value', y='category', orientation='h')
# 堆叠柱状图
fig = px.bar(df, x='category', y='value', color='group', barmode='stack')
# 分组柱状图
fig = px.bar(df, x='category', y='value', color='group', barmode='group')
### 饼图
python
fig = px.pie(df, names='category', values='count')
# 圆环图
fig = px.pie(df, names='category', values='count', hole=0.4)
### 面积图
python
fig = px.area(df, x='date', y='value', color='category')
## 统计图表
### 直方图
python
# 基础直方图
fig = px.histogram(df, x='values', nbins=30)
# 带边缘图
fig = px.histogram(df, x='values', marginal='box') # 或 'violin', 'rug'
# 2D 直方图
fig = px.density_heatmap(df, x='x', y='y', nbinsx=20, nbinsy=20)
### 箱线图
python
fig = px.box(df, x='category', y='value', color='group')
# 带缺口箱线图
fig = px.box(df, x='category', y='value', notched=True)
# 显示所有点
fig = px.box(df, x='category', y='value', points='all')
### 小提琴图
python
fig = px.violin(df, x='category', y='value', color='group', box=True, points='all')
### 条带/点图
python
fig = px.strip(df, x='category', y='value', color='group')
### 分布图
python
# 经验累积分布
fig = px.ecdf(df, x='value', color='group')
# 边缘分布
fig = px.scatter(df, x='x', y='y', marginal_x='histogram', marginal_y='box')
### 误差棒
python
fig = px.scatter(df, x='x', y='y', error_y='error', error_x='x_error')
# 使用 graph_objects 自定义误差棒
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(
x=[1, 2, 3],
y=[5, 10, 15],
error_y=dict(
type='data',
array=[1, 2, 3],
visible=True
)
))
## 科学图表
### 热力图
python
# 从矩阵数据创建
fig = px.imshow(z_mat