用途:pgfplots 是 LaTeX 的绘图宏包,用于在文档中生成折线图、柱状图、散点图、直方图、箱线图、误差棒、面积填充、3D 曲面、网格、等高线、参数曲线与极坐标图,并可从 CSV/TSV 文件读取数据。
主要能力:提供 axis、semilogxaxis、semilogyaxis、loglogaxis、polaraxis 等坐标轴环境;支持 xlabel、ylabel、title、xmin/xmax、domain、samples、grid、legend pos、width/height、axis lines、xtick、xticklabels、cycle list name、scaled ticks、restrict y to domain 等选项;支持 ybar、ybar stacked、ybar interval、boxplot、fill between、error bars、surf、mesh、contour filled 等绘图类型;内置 hot、cool、viridis、bluered、greenyellow、jet 等色图,可自定义 colormap 与 colorbar;支持多轴、groupplots 分组图、pin/箭头/竖线/色带/文本等标注,以及多种 mark 标记。
使用方式:在导言区写 \usepackage{pgfplots} 并设置 \pgfplotsset{compat=1.18};按需加载 fillbetween、groupplots、statistics、colorbrewer、polar 等库。基本结构为 tikzpicture 内嵌 axis 环境,用 \addplot 添加数据或表达式,用 \legend 或 \addlegendentry 添加图例。数据文件用 \addplot table[col sep=comma, x=x, y=y] {data.csv} 读取。
适用场景:学术论文、技术报告、幻灯片中的高质量矢量图表,尤其适合需要与 LaTeX 排版风格统一的场合。
限制与注意:必须设置 compat 版本以避免兼容性警告;三角函数默认使用角度,弧度输入需用 sin(deg(x));大数值或域可能触发 Dimension too large,可用 restrict y to domain 处理;3D 图采样过多会变慢,应减少 samples;CSV 需正确设置 col sep;fill between 需为每条曲线命名 name path;\addlegendentry 必须紧跟对应 \addplot。官方手册可用 texdoc pgfplots 查看,CTAN 页面为 https://ctan.org/pkg/pgfplots。