You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在LaTeX图表Y轴按指定格式显示小数

PGFPlots Y轴刻度显示格式修正方案

我使用LaTeX的pgfplots包绘制图表时,希望Y轴精确显示0、0.01、0.02、0.03、0.04、0.05、0.06、0.07、0.08、0.09、0.1这些数值,但生成的图表Y轴数字会自动省略末尾零或切换显示格式,虽数值正确但排版观感差,需实现按指定格式精确显示。

以下是原代码:

第一段原代码

\documentclass{article}
\usepackage{graphicx} % Required for inserting images

\usepackage{pgfplots}
\begin{document}

\begin{figure} 
\centering
\begin{tikzpicture} [style={outer sep=5}]
\begin{axis}[ymin=0,ymax=0.1, xmin=15, xmax=50,
xtick={15,20,...,50},
ytick={0,0.01,...,0.1},
grid={major},clip=false,grid style={dashed},
title={},
legend style={at={(1.2 ,1)},anchor=north},
font=\footnotesize]

% 25
\addplot[color=green, mark=*, samples=10] [error bars/.cd,y dir=both, y explicit] coordinates {
(15, 0.0032)
(20, 0.0039) 
(25, 0.0052) 
(30, 0.007)
(35, 0.009)
(40, 0.0095) 
(45, 0.01)
(50, 0.012)
};


\end{axis}
\end{tikzpicture}
\caption[]{}
\end{figure}

\end{document}

第二段原代码

\begin{figure} [H]
\centering
\begin{tikzpicture} [style={outer sep=5}]
\begin{axis}[ymin=0.004,ymax=0.018, xmin=15, xmax=50,
xlabel= $Number of Nodes$, ylabel = $end-to-end (sec)$,
xtick={15,20,...,50},
ytick={0.004,0.005,...,0.018},
yticklabel style={/pgf/number format/fixed},
grid={major},clip=false,grid style={dashed},
title={},
legend style={at={(1.2 ,1)},anchor=north},
font=\footnotesize]

% TAODV
\addplot[color=blue, mark=*, samples=10] [error bars/.cd,y dir=both, y explicit] coordinates {
(15, 0.0072) +-(0.001, -0.001)
(20, 0.0079) +-(0.001, -0.001)
(25, 0.0092) +-(0.001, -0.001)
(30, 0.01) +-(0.001, -0.001)
(35, 0.013) +-(0.0018, -0.0018)
(40, 0.0137) +-(0.001, -0.001)
(45, 0.015) +-(0.001, -0.001)
(50, 0.0168)+-(0.001, -0.001)
};

% AODV
\addplot[color=red, mark=*, samples=10] [error bars/.cd,y dir=both, y explicit] coordinates {
(15, 0.0052) +-(0.001, -0.001)
(20, 0.0057) +-(0.001, -0.001)
(25, 0.0067) +-(0.001, -0.001)
(30, 0.0074) +-(0.001, -0.001)
(35, 0.0082) +-(0.0018, -0.0018)
(40, 0.0095) +-(0.001, -0.001)
(45, 0.01) +-(0.001, -0.001)
(50, 0.0109) +-(0.001, -0.001)
};

\legend{
    $TAODV$,
    $AODV$
}
\end{axis}
\end{tikzpicture}
\caption[End-to-end delay of TAODV vs. AODV]{End-to-end delay of TAODV vs. AODV with \%95 confidence interval}
\end{figure}

解决方法

方案1:手动指定刻度标签(最精准)

直接在axis环境中添加yticklabels,完全自定义显示内容,适用于第一段代码的需求:

ytick={0,0.01,...,0.1},
yticklabels={0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1},

方案2:设置固定小数精度

通过yticklabel style强制固定小数格式并指定精度,适用于第二段代码的需求:

yticklabel style={/pgf/number format/fixed, /pgf/number format/precision=3},
  • fixed:禁用科学计数法,强制使用固定小数格式
  • precision=3:保留3位小数,确保0.004、0.005等刻度完整显示

修改后的代码示例

第一段修改后代码

\documentclass{article}
\usepackage{graphicx}
\usepackage{pgfplots}
\begin{document}

\begin{figure} 
\centering
\begin{tikzpicture} [style={outer sep=5}]
\begin{axis}[ymin=0,ymax=0.1, xmin=15, xmax=50,
xtick={15,20,...,50},
ytick={0,0.01,...,0.1},
% 手动指定刻度显示内容
yticklabels={0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1},
grid={major},clip=false,grid style={dashed},
title={},
legend style={at={(1.2 ,1)},anchor=north},
font=\footnotesize]

\addplot[color=green, mark=*, samples=10] [error bars/.cd,y dir=both, y explicit] coordinates {
(15, 0.0032)
(20, 0.0039) 
(25, 0.0052) 
(30, 0.007)
(35, 0.009)
(40, 0.0095) 
(45, 0.01)
(50, 0.012)
};

\end{axis}
\end{tikzpicture}
\caption[]{}
\end{figure}

\end{document}

第二段修改后代码

\begin{figure} [H]
\centering
\begin{tikzpicture} [style={outer sep=5}]
\begin{axis}[ymin=0.004,ymax=0.018, xmin=15, xmax=50,
xlabel= $Number of Nodes$, ylabel = $end-to-end (sec)$,
xtick={15,20,...,50},
ytick={0.004,0.005,...,0.018},
% 设置固定3位小数精度
yticklabel style={/pgf/number format/fixed, /pgf/number format/precision=3},
grid={major},clip=false,grid style={dashed},
title={},
legend style={at={(1.2 ,1)},anchor=north},
font=\footnotesize]

% TAODV
\addplot[color=blue, mark=*, samples=10] [error bars/.cd,y dir=both, y explicit] coordinates {
(15, 0.0072) +-(0.001, -0.001)
(20, 0.0079) +-(0.001, -0.001)
(25, 0.0092) +-(0.001, -0.001)
(30, 0.01) +-(0.001, -0.001)
(35, 0.013) +-(0.0018, -0.0018)
(40, 0.0137) +-(0.001, -0.001)
(45, 0.015) +-(0.001, -0.001)
(50, 0.0168)+-(0.001, -0.001)
};

% AODV
\addplot[color=red, mark=*, samples=10] [error bars/.cd,y dir=both, y explicit] coordinates {
(15, 0.0052) +-(0.001, -0.001)
(20, 0.0057) +-(0.001, -0.001)
(25, 0.0067) +-(0.001, -0.001)
(30, 0.0074) +-(0.001, -0.001)
(35, 0.0082) +-(0.0018, -0.0018)
(40, 0.0095) +-(0.001, -0.001)
(45, 0.01) +-(0.001, -0.001)
(50, 0.0109) +-(0.001, -0.001)
};

\legend{
    $TAODV$,
    $AODV$
}
\end{axis}
\end{tikzpicture}
\caption[End-to-end delay of TAODV vs. AODV]{End-to-end delay of TAODV vs. AODV with \%95 confidence interval}
\end{figure}

内容的提问来源于stack exchange,提问作者Ali Alzahrani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 22:10:44