我如何才能让图例变得色彩缤纷?我如何才能在条形图中显示价值?

我如何才能让图例变得色彩缤纷?我如何才能在条形图中显示价值?
\documentclass[border=5mm] {standalone}
\usepackage{pgfplots, pgfplotstable}
\usepackage{xcolor, color}


\begin{document}

\begin{tikzpicture}
    \definecolor{SpecialYellow}{HTML}{F9DC21}
    \definecolor{SpecialGreen}{HTML}{46A62E}
    \definecolor{SpecialBlue}{HTML}{4192F5}

    \pgfplotstableread{
    Label   Engine  Check   OS
    Cached  4.49    0.83    4
    Normal  12.45   3.9     12
    }\datatable

    \begin{axis}[xbar stacked, xmin=0, xmax=30, ytick=data, yticklabels from table={\datatable}{Label}, draw=black, height=25mm, width=5cm, legend style={at={(0,1.5)}, anchor=west,legend columns=3, area legend} ]
        \addlegendentry{Engine}
        \addplot [fill=SpecialBlue, draw=none] table[x=Engine] {\datatable};
        \addlegendentry{Check}
        \addplot [fill=SpecialGreen, draw=white] table[x=Check] {\datatable};
        \addlegendentry{OS}
        \addplot [fill=SpecialYellow, draw=white] table[x=OS] {\datatable};
    \end{axis}
\end{tikzpicture}
\end{document}

答案1

[区域图例] 需要位于 [图例样式] 之外。

我无法弄清楚此图的 y 坐标是什么。0 对应于底部以下。下部条形图的中心位于约 5 处,上部条形图的中心位于约 12 处。

\documentclass[border=5mm] {standalone}
\usepackage{pgfplots, pgfplotstable}
\usepackage{xcolor, color}

\begin{document}

\pgfplotstableread{
  Label   Engine  Check   OS
  Cached  4.49    0.83    4
  Normal  12.45   3.9     12
}\datatable

\begin{tikzpicture}
    \definecolor{SpecialYellow}{HTML}{F9DC21}
    \definecolor{SpecialGreen}{HTML}{46A62E}
    \definecolor{SpecialBlue}{HTML}{4192F5}

    \begin{axis}[xbar stacked, xmin=0, xmax=30, ytick=data, yticklabels from table={\datatable}{Label}, 
      draw=black, height=25mm, width=5cm, tick align=outside,
      legend style={at={(0,1.5)}, anchor=west,legend columns=3}, area legend]
        \addlegendentry{Engine}
        \addplot [fill=SpecialBlue, draw=none] table[x=Engine] {\datatable};
        \addlegendentry{Check}
        \addplot [fill=SpecialGreen, draw=white] table[x=Check] {\datatable};
        \addlegendentry{OS}
        \addplot [fill=SpecialYellow, draw=white] table[x=OS] {\datatable};
        \coordinate (A) at (axis cs: 4.49,5);
    \end{axis}
    \node[inner sep=0pt,left] at (A) {\scriptsize 4.49};
\end{tikzpicture}
\end{document}

条形图

相关内容