Plotnine Geoms
任务目标
- 本 Skill 用于:掌握 Plotnine 各种几何对象的用途和使用方法
- 能力包含:散点图、线图、柱状图、箱线图、密度图、面积图等 geom 选择
- 触发条件:当用户需要创建特定类型的数据可视化图表时
前置准备
- 环境要求:已完成 plotnine-basics 学习
操作步骤
标准流程
1. 散点图 (geom_point)
from plotnine import ggplot, aes, geom_point
(
ggplot(df, aes("x", "y", color="category"))
+ geom_point(size=3, alpha=0.7)
)
2. 线图 (geom_line)
from plotnine import ggplot, aes, geom_line
(
ggplot(df, aes("time", "value", linetype="group"))
+ geom_line()
)
3. 柱状图 (geom_bar / geom_col)
# geom_bar: 计数柱状图
ggplot(df, aes("category")) + geom_bar()
# geom_col: 值柱状图
ggplot(df, aes("category", "value")) + geom_col()
4. 箱线图 (geom_boxplot)
from plotnine import geom_boxplot
(
ggplot(df, aes("category", "value", fill="category"))
+ geom_boxplot(alpha=0.7)
)
5. 密度图 (geom_density)
from plotnine import geom_density
(
ggplot(df, aes("value", fill="category"))
+ geom_density(alpha=0.5)
)
常用 Geom速查
| Geom |
用途 |
关键参数 |
| geom_point |
散点图 |
size, color, shape, alpha |
| geom_line |
线图 |
linetype, color, size |
| geom_bar |
计数柱状图 |
fill, color |
| geom_col |
值柱状图 |
fill, color |
| geom_boxplot |
箱线图 |
fill, color, outlier |
| geom_density |
密度图 |
fill, color, alpha |
| geom_histogram |
直方图 |
bins, fill |
| geom_smooth |
平滑线 |
method, se, color |
| geom_jitter |
抖动散点 |
width, height |
| geom_violin |
小提琴图 |
fill, color, trim |
| geom_area |
面积图 |
fill, alpha |
| geom_ribbon |
带状图 |
fill, alpha |
| geom_errorbar |
误差棒 |
width, size |
| geom_text |
文本标注 |
label, size |
可选分支
当需要组合多个 geom 时
(
ggplot(df, aes("x", "y"))
+ geom_point() # 散点
+ geom_line() # 线
+ geom_smooth(method="lm") # 平滑线
)
资源索引
领域参考
references/geom-point-line.md
- 何时读取:需要创建散点图或线图时
- 内容:geom_point, geom_line, geom_path, geom_jitter
references/geom-bar-boxplot.md
- 何时读取:需要创建柱状图或箱线图时
- 内容:geom_bar, geom_col, geom_boxplot, geom_violin
references/geom-density-histogram.md
- 何时读取:需要创建分布图时
- 内容:geom_density, geom_histogram, geom_freqpoly
注意事项
Geom 与 Stat 配对
- geom_bar 默认使用 stat_count
- geom_smooth 默认使用 stat_smooth
- 可以显式指定 stat 参数
位置调整 (position)
- position_dodge: 躲避
- position_fill: 堆叠填充
- position_jitter: 抖动减少重叠
- position_stack: 堆叠
1---2name: plotnine-geoms3description: 掌握Plotnine几何对象层,包括散点图、线图、柱状图、箱线图、密度图等40+种geom的使用方法和场景选择4---56# Plotnine Geoms78## 任务目标9- 本 Skill 用于:掌握 Plotnine 各种几何对象的用途和使用方法10- 能力包含:散点图、线图、柱状图、箱线图、密度图、面积图等 geom 选择11- 触发条件:当用户需要创建特定类型的数据可视化图表时1213## 前置准备14- 环境要求:已完成 plotnine-basics 学习1516## 操作步骤1718### 标准流程1920#### 1. 散点图 (geom_point)21```python22from plotnine import ggplot, aes, geom_point2324(25 ggplot(df, aes("x", "y", color="category"))26 + geom_point(size=3, alpha=0.7)27)28```2930#### 2. 线图 (geom_line)31```python32from plotnine import ggplot, aes, geom_line3334(35 ggplot(df, aes("time", "value", linetype="group"))36 + geom_line()37)38```3940#### 3. 柱状图 (geom_bar / geom_col)41```python42# geom_bar: 计数柱状图43ggplot(df, aes("category")) + geom_bar()4445# geom_col: 值柱状图46ggplot(df, aes("category", "value")) + geom_col()47```4849#### 4. 箱线图 (geom_boxplot)50```python51from plotnine import geom_boxplot5253(54 ggplot(df, aes("category", "value", fill="category"))55 + geom_boxplot(alpha=0.7)56)57```5859#### 5. 密度图 (geom_density)60```python61from plotnine import geom_density6263(64 ggplot(df, aes("value", fill="category"))65 + geom_density(alpha=0.5)66)67```6869### 常用 Geom速查7071| Geom | 用途 | 关键参数 |72|------|------|---------|73| geom_point | 散点图 | size, color, shape, alpha |74| geom_line | 线图 | linetype, color, size |75| geom_bar | 计数柱状图 | fill, color |76| geom_col | 值柱状图 | fill, color |77| geom_boxplot | 箱线图 | fill, color, outlier |78| geom_density | 密度图 | fill, color, alpha |79| geom_histogram | 直方图 | bins, fill |80| geom_smooth | 平滑线 | method, se, color |81| geom_jitter | 抖动散点 | width, height |82| geom_violin | 小提琴图 | fill, color, trim |83| geom_area | 面积图 | fill, alpha |84| geom_ribbon | 带状图 | fill, alpha |85| geom_errorbar | 误差棒 | width, size |86| geom_text | 文本标注 | label, size |8788### 可选分支8990#### 当需要组合多个 geom 时91```python92(93 ggplot(df, aes("x", "y"))94 + geom_point() # 散点95 + geom_line() # 线96 + geom_smooth(method="lm") # 平滑线97)98```99100## 资源索引101102### 领域参考103- [references/geom-point-line.md](references/geom-point-line.md)104 - 何时读取:需要创建散点图或线图时105 - 内容:geom_point, geom_line, geom_path, geom_jitter106107- [references/geom-bar-boxplot.md](references/geom-bar-boxplot.md)108 - 何时读取:需要创建柱状图或箱线图时109 - 内容:geom_bar, geom_col, geom_boxplot, geom_violin110111- [references/geom-density-histogram.md](references/geom-density-histogram.md)112 - 何时读取:需要创建分布图时113 - 内容:geom_density, geom_histogram, geom_freqpoly114115## 注意事项116117### Geom 与 Stat 配对118- geom_bar 默认使用 stat_count119- geom_smooth 默认使用 stat_smooth120- 可以显式指定 stat 参数121122### 位置调整 (position)123- position_dodge: 躲避124- position_fill: 堆叠填充125- position_jitter: 抖动减少重叠126- position_stack: 堆叠