使用 tikz 创建堆叠条形图,不改变值

使用 tikz 创建堆叠条形图,不改变值

我想在每个条形图内添加另一个条形图来表示恒定时间。如下图所示(绿色)。它有点像堆叠条形图,但我只想显示读取图像的恒定时间。

\begin{figure}[!h]
\centering
\begin{tikzpicture}
\begin{axis}[
    ybar,
    ymin=0,
    %enlargelimits=0.15,
                legend image code/.code={%
                    \draw[#1, draw=none] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);
                },  
    legend style={at={(0.5,-0.10)},
      anchor=north,legend columns=-1},
    ylabel={Execution Time (ms)},
    symbolic x coords={RGB2GRAY,Gaussian,Box, Sobel},
    xtick=data,
    nodes near coords,
    nodes near coords align={vertical},
    ]
\addplot coordinates {(RGB2GRAY,54) (Gaussian,86)  (Box,154) (Sobel,268)  };%CPU
\addplot coordinates {(RGB2GRAY,170.71) (Gaussian, 172.065)  (Box,193.72) (Sobel,215.38)  };%GPU
\addplot coordinates {(RGB2GRAY,20.234959834) (Gaussian,26.492609995)  (Box,27.353843832) (Sobel,45.59262995) };%FPGA
\legend{CPU,GPU,FPGA}
\end{axis}
\end{tikzpicture}
\caption{Algorithms Including Image Read/Write }
\end{figure}

在此处输入图片描述

答案1

欢迎!一种相当粗暴的方法是叠加第二个“隐藏”hide axis轴()。要“同步轴”,您可能需要同时使用ymin=0,ymax=300,两者。

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{figure}[!h]
\centering
\begin{tikzpicture}
\begin{axis}[
    ybar,
    ymin=0,ymax=300,%<- added ymax
    %enlargelimits=0.15,
                legend image code/.code={%
                    \draw[#1, draw=none] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);
                },  
    legend style={at={(0.5,-0.10)},
      anchor=north,legend columns=-1},
    ylabel={Execution Time (ms)},
    symbolic x coords={RGB2GRAY,Gaussian,Box, Sobel},
    xtick=data,
    nodes near coords,
    nodes near coords align={vertical},
    ]
\addplot coordinates {(RGB2GRAY,54) (Gaussian,86)  (Box,154) (Sobel,268)  };%CPU
\addplot coordinates {(RGB2GRAY,170.71) (Gaussian, 172.065)  (Box,193.72) (Sobel,215.38)  };%GPU
\addplot coordinates {(RGB2GRAY,20.234959834) (Gaussian,26.492609995)  (Box,27.353843832) (Sobel,45.59262995) };%FPGA
\legend{CPU,GPU,FPGA}
\end{axis}
\begin{axis}[ybar,ymin=0,ymax=300,hide axis,
    symbolic x coords={RGB2GRAY,Gaussian,Box, Sobel}
    ]
\addplot[fill=green] coordinates {(RGB2GRAY,100) (Gaussian,100)  (Box,100) (Sobel,100)  };
\end{axis}
\end{tikzpicture}
\caption{Algorithms Including Image Read/Write }
\end{figure}
\end{document}

在此处输入图片描述

相关内容