\pgfmathprintnumber 超出轴值的条形值标签

\pgfmathprintnumber 超出轴值的条形值标签

我尝试仅标记超出轴的 y 值,而不标记其余部分(如下图所示)。有什么建议可以实现吗?在此处输入图片描述

我的 MWE(感谢@杰克):

    \documentclass{scrbook}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{decorations.pathmorphing}


\definecolor{bblue}{HTML}{4F81BD}
\definecolor{rred}{HTML}{C0504D}
\definecolor{ggreen}{HTML}{9BBB59}
\definecolor{ppurple}{HTML}{9F4C7C}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
    \begin{axis}[
        width  = 0.9*\textwidth,
        height = 8cm,
        major x tick style = transparent,
        ybar=2*\pgflinewidth,
        bar width=4pt,
        ymin=0,
        axis on top,
        ymax=12,
        ymajorgrids = true,
        xtick = data,
        xlabel = {Environmental indicators},
        symbolic x coords={GWP, ODP, POCP, AP, EP(T), EP(FW), EP(M), ADP, CED},
        restrict y to domain*=0:14, % Cut values off at 14
    visualization depends on=rawy\as\rawy, % Save the unclipped values
    after end axis/.code={ % Draw line indicating break
     \draw [ultra thick, white, decoration={snake, amplitude=1pt}, decorate] (rel axis cs:0,1.05) -- (rel axis cs:1,1.05);
        },
    nodes near coords={%
     \pgfmathprintnumber{\rawy}% Print unclipped values
        },
    axis lines*=left,
    clip=false
    ]
\addplot[style={bblue,fill=bblue,mark=none}]
            coordinates {   (GWP, 1.0) 
                            (ODP,1.0)
                            (POCP,1.0)
                            (AP,1.0)
                            (EP(T),1.0)
                            (EP(FW),1.0)
                            (EP(M),1.0)
                            (ADP,1.0)
                            (CED,1.0)};
        \addplot[style={rred,fill=rred,mark=none}]
             coordinates {(GWP,0.11) 
                            (ODP,4.28) 
                            (POCP,0.28 )
                            (AP,0.67)
                            (EP(T),0.38)
                            (EP(FW),1.0)
                            (EP(M),0.55)
                            (ADP,4.20)
                            (CED,1.0)};
        \addplot[style={ggreen,fill=ggreen,mark=none}]
             coordinates {(GWP,1.42)
                            (ODP,1.37) 
                            (POCP,1.26)
                            (AP,6.50)
                            (EP(T),6.65)
                            (EP(FW),48.56)
                            (EP(M),2.97)
                            (ADP,2.25)
                            (CED,1.0)};
\addplot[style={ppurple,fill=ppurple,mark=none}]
             coordinates {  (GWP,0.74) 
                            (ODP,2.07) 
                            (POCP,2.69)
                            (AP,7.10)
                            (EP(T),3.03)
                            (EP(FW),6.88)
                            (EP(M),3.04)
                            (ADP,2.53)
                            (CED,1.0)};
\legend{Singapore,France,Germany,Spain}
    \end{axis}
\end{tikzpicture}
\caption{Comparison of electricity profiles per 1 kWh of electricity}
\end{figure}
\end{document}

答案1

要实现这一点,需要两个步骤。首先,您需要nodes near coords从参数列表中删除 及其值\begin[axis]。(这将删除全部标签)。接下来,您需要向带有绿色条nodes near coords的个体中添加\addplot标签。这样做,您将无法为所有绿色条添加标题,这显然不是您真正需要的。

但是,有一个技巧可以只nodes near coords在您想要的特定坐标上添加值。(您从索引 0 开始计算坐标)。因此,在这种情况下,(EP(FW),48.56)是第六个项目,但索引为 5。从上面获取值{\pgfmathprintnumber{\rawy}}并将其包装起来,\ifnum...\fi如下例所示:

\documentclass{scrbook}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{decorations.pathmorphing}


\definecolor{bblue}{HTML}{4F81BD}
\definecolor{rred}{HTML}{C0504D}
\definecolor{ggreen}{HTML}{9BBB59}
\definecolor{ppurple}{HTML}{9F4C7C}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
    \begin{axis}[
        width  = 0.9*\textwidth,
        height = 8cm,
        major x tick style = transparent,
        ybar=2*\pgflinewidth,
        bar width=4pt,
        ymin=0,
        axis on top,
        ymax=12,
        ymajorgrids = true,
        xtick = data,
        xlabel = {Environmental indicators},
        symbolic x coords={GWP, ODP, POCP, AP, EP(T), EP(FW), EP(M), ADP, CED},
        restrict y to domain*=0:14, % Cut values off at 14
    visualization depends on=rawy\as\rawy, % Save the unclipped values
    after end axis/.code={ % Draw line indicating break
     \draw [ultra thick, white, decoration={snake, amplitude=1pt}, decorate] (rel axis cs:0,1.05) -- (rel axis cs:1,1.05);
        },
    %nodes near coords={%
     %\pgfmathprintnumber{\rawy}% Print unclipped values
     %},%REMOVED THIS PART
    axis lines*=left,
    clip=false
    ]
\addplot[style={bblue,fill=bblue,mark=none}]
            coordinates {(GWP, 1.0) 
                            (ODP,1.0)
                            (POCP,1.0)
                            (AP,1.0)
                            (EP(T),1.0)
                            (EP(FW),1.0)
                            (EP(M),1.0)
                            (ADP,1.0)
                            (CED,1.0)};
        \addplot[style={rred,fill=rred,mark=none}]
             coordinates {(GWP,0.11) 
                            (ODP,4.28) 
                            (POCP,0.28 )
                            (AP,0.67)
                            (EP(T),0.38)
                            (EP(FW),1.0)
                            (EP(M),0.55)
                            (ADP,4.20)
                            (CED,1.0)};
        \addplot[style={ggreen,fill=ggreen,mark=none},nodes near coords={\ifnum\coordindex=5{\pgfmathprintnumber{\rawy}}\else\fi}]%UPDATED THIS WITH A CONDITIONAL
             coordinates {(GWP,1.42)
                            (ODP,1.37) 
                            (POCP,1.26)
                            (AP,6.50)
                            (EP(T),6.65)
                            (EP(FW),48.56)
                            (EP(M),2.97)
                            (ADP,2.25)
                            (CED,1.0)};
        \addplot[style={ppurple,fill=ppurple,mark=none}]
             coordinates {  (GWP,0.74) 
                            (ODP,2.07) 
                            (POCP,2.69)
                            (AP,7.10)
                            (EP(T),3.03)
                            (EP(FW),6.88)
                            (EP(M),3.04)
                            (ADP,2.53)
                            (CED,1.0)};
\legend{Singapore,France,Germany,Spain}
    \end{axis}
\end{tikzpicture}
\caption{Comparison of electricity profiles per 1 kWh of electricity}
\end{figure}
\end{document}

只有一个条形带有标签的图表

编辑:

如果你感兴趣的话,我可以改编一下有点类似的问题

相关内容