分组条形图

分组条形图

我正在尝试构建分组条形图,但有三个问题似乎找不到答案:

  1. 如何修改组之间的间距?或者至少确保没有条形超出图表范围?

  2. 如何消除组中条形之间的间距?

  3. 如何在图例中在每个符号和描述之间添加一个空格?

\documentclass[12pt,a4paper,onecolumn, openright]{report}
\usepackage{xcolor}
\usepackage{pgfplots}
\usepackage{tikz}

% Define bar chart colors
%
\definecolor{bblue}{HTML}{4F81BD}
\definecolor{rred}{HTML}{C0504D}
\definecolor{ggreen}{HTML}{9BBB59}
\definecolor{ppurple}{HTML}{9F4C7C}


\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width  = 0.85*\textwidth,
        height = 8cm,
        major x tick style = transparent,
        ybar,
        bar width=14pt,
        ymajorgrids = true,
        ylabel = {Run time speed},
        symbolic x coords={EgyptHD,Hover,Navi},
        xtick = data,
        scaled y ticks = false,
    ]
        \addplot[style={bblue,fill=bblue,mark=none}]
            coordinates {(EgyptHD, 1.0) (Hover,1.0) (Navi,1.0)};

        \addplot[style={rred,fill=rred,mark=none}]
            coordinates {(EgyptHD,1.123) (Hover,0.85) (Navi,1.09)};

        \addplot[style={ggreen,fill=ggreen,mark=none}]
            coordinates {(EgyptHD,0.92) (Hover,0.56) (Navi,0.95)};

        \addplot[style={ppurple,fill=ppurple,mark=none}]
            coordinates {(EgyptHD,0.74) (Hover,1.07) (Navi,1.23)};

        \legend{No vectorization,TreeScore $>2$,TreeScore $>3$,TreeScore $>4$}
    \end{axis}
\end{tikzpicture}
\end{document}

输出

答案1

  1. 为了减小组之间的空间,可以使用来增加 x 轴范围enlarge x limits=0.25(与数据范围相比,这将轴范围增加 25%)。
  2. 要删除条形之间的间距,请使用ybar=0pt而不是ybar。请注意,这会导致条形之间因轮廓而略有重叠。为避免这种情况,您可以设置,或通过设置绘图选项ybar=2*\pgflinewidth来禁用绘制条形轮廓。draw opacity=0
  3. 为了在图例图像和标签之间获得固定的空间,您可以将标签对齐到左侧并使用设置列之间的空间legend cell align=left, legend style={ column sep=1ex }(感谢 Qrrbrbirlbel!)。

\documentclass[12pt,a4paper,onecolumn, openright]{report}
\usepackage{xcolor}
\usepackage{pgfplots}
\usepackage{tikz}

% Define bar chart colors
%
\definecolor{bblue}{HTML}{4F81BD}
\definecolor{rred}{HTML}{C0504D}
\definecolor{ggreen}{HTML}{9BBB59}
\definecolor{ppurple}{HTML}{9F4C7C}


\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width  = 0.85*\textwidth,
        height = 8cm,
        major x tick style = transparent,
        ybar=2*\pgflinewidth,
        bar width=14pt,
        ymajorgrids = true,
        ylabel = {Run time speed},
        symbolic x coords={EgyptHD,Hover,Navi},
        xtick = data,
        scaled y ticks = false,
        enlarge x limits=0.25,
        ymin=0,
        legend cell align=left,
        legend style={
                at={(1,1.05)},
                anchor=south east,
                column sep=1ex
        }
    ]
        \addplot[style={bblue,fill=bblue,mark=none}]
            coordinates {(EgyptHD, 1.0) (Hover,1.0) (Navi,1.0)};

        \addplot[style={rred,fill=rred,mark=none}]
             coordinates {(EgyptHD,1.123) (Hover,0.85) (Navi,1.09)};

        \addplot[style={ggreen,fill=ggreen,mark=none}]
             coordinates {(EgyptHD,0.92) (Hover,0.56) (Navi,0.95)};

        \addplot[style={ppurple,fill=ppurple,mark=none}]
             coordinates {(EgyptHD,0.74) (Hover,1.07) (Navi,1.23)};

        \legend{No vectorization,TreeScore $>2$,TreeScore $>3$,TreeScore $>4$}
    \end{axis}
\end{tikzpicture}
\end{document}

相关内容