隐藏条形图中节点的 0 值

隐藏条形图中节点的 0 值

当我激活节点附近的坐标时,是否可以隐藏 0 值?如果我可以隐藏那些特定 0 值的条形图就更好了。

我尝试使用 nodes near coords=y filter/.expression={y==0 ? nan : y},,但是它隐藏了 x 轴标签。

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{siunitx}
\pgfplotsset{width=7cm,compat=1.3}
\hyphenation{}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    legend style={font=\tiny},
    ybar=1pt,
    bar width =2pt,
    ymin=0,ymax=200,
    enlarge y limits={upper=0.15},
    legend image code/.code={\draw[#1, draw=none] (0cm,-0.1cm) rectangle (0.3cm,0.1cm);
                },
    ymajorgrids = true,
    legend style={at={(-0.00000009,0.845)},
                   anchor=west,legend columns=1},
    ylabel={Energy Consumption (Watts)},
    symbolic x coords={R2G,Gaussian,Box,Sobel,Gsn Pyd,Extrema,Orientation,Descriptor,Edge Total,SIFT Total},
    xtick=data,
    nodes near coords,
    nodes near coords style={font=\tiny, anchor=west,rotate=90,inner
    xsep=0.5pt},
    x tick label style = {font=\small, rotate=45, anchor=east},
    ]

\addplot coordinates {(R2G,54) (Gaussian,68)  (Box,80) (Sobel,95) (Gsn Pyd,0)(Extrema,0) (Orientation,0) (Descriptor,0) (Edge Total, 105)  (SIFT Total, 125)};%CPU

\addplot [fill=teal!]  coordinates {(R2G,80) (Gaussian,105)  (Box,111) (Sobel,120) (Gsn Pyd,130)(Extrema,50) (Orientation,47) (Descriptor,35) (Edge Total, 132) (SIFT Total, 150)  };%GPU

\addplot coordinates {(R2G,11) (Gaussian,32)  (Box,32) (Sobel,20) (Gsn Pyd,26)(Extrema,12) (Orientation,10) (Descriptor,5) (Edge Total, 43) (SIFT Total, 52) };%FPGA

\addplot coordinates {(R2G,15) (Gaussian,38)  (Box,39) (Sobel,32) (Gsn Pyd,28) (Extrema,18) (Orientation,15) (Descriptor,9) (Edge Total, 52) (SIFT Total, 72) };%HLS

\legend{CPU,GPU,FPGA,HLS}
\end{axis}
\end{tikzpicture}
\end{document}

答案1

我建议使用y filter/.expression={y==0 ? nan : y}如下方法:只需将 GPU 图移到 CPU 图前面,标签即可保留。

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{siunitx}
\pgfplotsset{width=7cm,compat=1.3}
\hyphenation{}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    legend style={font=\tiny},
    ybar=1pt,
    bar width =2pt,
    ymin=0,ymax=200,
    y filter/.expression={y==0 ? nan : y},
    enlarge y limits={upper=0.15},
    legend image code/.code={\draw[#1, draw=none] (0cm,-0.1cm) rectangle (0.3cm,0.1cm);
                },
    ymajorgrids = true,
    legend style={at={(-0.00000009,0.845)},
                   anchor=west,legend columns=1},
    ylabel={Energy Consumption (Watts)},
    symbolic x coords={R2G,Gaussian,Box,Sobel,Gsn Pyd,Extrema,Orientation,Descriptor,Edge Total,SIFT Total},
    xtick=data,
    nodes near coords,
    nodes near coords style={font=\tiny, anchor=west,rotate=90,inner
    xsep=0.5pt},
    x tick label style = {font=\small, rotate=45, anchor=east},
    ]
\addplot [fill=teal!] coordinates {(R2G,80) (Gaussian,105)  (Box,111) (Sobel,120) (Gsn Pyd,130)(Extrema,50) (Orientation,47) (Descriptor,35) (Edge Total, 132) (SIFT Total, 150)  };%GPU

\addplot coordinates {(R2G,54) (Gaussian,68)  (Box,80) (Sobel,95) (Gsn Pyd,0)(Extrema,0) (Orientation,0) (Descriptor,0) (Edge Total, 105)  (SIFT Total, 125)};%CPU

\addplot coordinates {(R2G,11) (Gaussian,32)  (Box,32) (Sobel,20) (Gsn Pyd,26)(Extrema,12) (Orientation,10) (Descriptor,5) (Edge Total, 43) (SIFT Total, 52) };%FPGA

\addplot coordinates {(R2G,15) (Gaussian,38)  (Box,39) (Sobel,32) (Gsn Pyd,28) (Extrema,18) (Orientation,15) (Descriptor,9) (Edge Total, 52) (SIFT Total, 72) };%HLS

\legend{GPU,CPU,FPGA,HLS}
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容