使用 PGFPlots 将 Y 轴标签与条形图对齐

使用 PGFPlots 将 Y 轴标签与条形图对齐

我已经为下面的代码苦苦挣扎了一段时间了,我打算放弃了!希望有人能帮忙,这样我就可以在一张图中显示多个条形图。

\begin{figure}[htbp]
    \centering
    \begin{adjustbox}{width=\textwidth}
        \begin{tikzpicture}
            \begin{axis}[
                width=0.8\textwidth,
                reverse legend,
                height=18cm,
                xbar,
                xmin=0, xmax=35,
                ytick={1,2,3},
                yticklabels={EE1, EE2, EE3},
                nodes near coords,
                nodes near coords align={horizontal},
                legend style={
                    at={(1.1,0.5)},
                    anchor=west,
                    draw,
                    fill=none,
                    legend columns=1,
                    /tikz/column 2/.style={column sep=10pt},  % Adjust the spacing between legend entries
                },
            ]
            \addplot [fill=blue] coordinates {(6, 1)};
            \addplot [fill=green] coordinates {(16, 1)};
            \addplot [fill=yellow] coordinates {(14, 1)};
            \addplot [fill=orange] coordinates {(8, 1)};
            \addplot [fill=red] coordinates {(8, 1)};
            
            \addplot [fill=blue] coordinates {(6, 2)};
            \addplot [fill=green] coordinates {(16, 2)};
            \addplot [fill=yellow] coordinates {(14, 2)};
            \addplot [fill=orange] coordinates {(8, 2)};
            \addplot [fill=red] coordinates {(8, 2)};
            
            \addplot [fill=blue] coordinates {(6, 3)};
            \addplot [fill=green] coordinates {(16, 3)};
            \addplot [fill=yellow] coordinates {(14, 3)};
            \addplot [fill=orange] coordinates {(8, 3)};
            \addplot [fill=red] coordinates {(8, 3)};
            
            % Specify the legend entries and their colors
            \addlegendimage{fill=blue}
            \addlegendentry{Strongly Agree}
            
            \addlegendimage{fill=green}
            \addlegendentry{Somewhat Agree}
            
            \addlegendimage{fill=yellow}
            \addlegendentry{Neutral}
            
            \addlegendimage{fill=orange}
            \addlegendentry{Somewhat Disagree}
            
            \addlegendimage{fill=red}
            \addlegendentry{Strongly Disagree}
            \end{axis}
        \end{tikzpicture}
    \end{adjustbox}
    \caption{Results of ...}
    \label{bc:EE1}
\end{figure}

此代码给出了以下图像:

条形图

但是,如您所见:EE2 完美地对齐在这些条形的中间,但对于 EE3 和 EE1 则并非如此。我该如何解决这个问题?我有一堆条形图需要讨论,因此将一些条形图强行合并到一个图中会使我的 PDF 更加整洁,至少我是这么认为的。

相关内容