带图例的条形图中 x 位置错误

带图例的条形图中 x 位置错误

我最近开始使用LaTeX它来完成我的学士论文,但无法解决符号 x 坐标错位的问题。此外,我希望不同条之间的间距保持一致。

我怀疑我以某种方式错误地使用了“\addplot”坐标,但也许有一个简单的方法可以解决我的问题?

在此处输入图片描述

这是该图的代码:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}
\begin{figure}
    \centering
    \begin{tikzpicture}
        \begin{axis} [
                title={Top ten ranking distributions for page hits in 2021 on distrowatch.com},
                ybar,
                ylabel={Hits per day},
                ymin=0,
                ymax=4000,
                width=\textwidth,
                xtick=data,%{MX Linux,EndevaourOS,Manjaro,Mint,Pop! OS,Ubuntu,Debian,elementary,Garuda,Fedora},
                x tick label style={rotate=90, align=center},
                symbolic x coords={MX Linux,EndevaourOS,Manjaro,Mint,Pop! OS,Ubuntu,Debian,elementary,Garuda,Fedora},
                % point meta=explicit symbolic,
                nodes near coords,
                % legend columns=3,
                % enlarge x limits=true,
                %unbounded coords=jump,
            ]
            \addplot coordinates {
                    (MX Linux, 3384)
                    (Mint, 2067)
                    (Pop! OS, 1668)
                    (Ubuntu, 1339)
                    (Debian, 1274)
                    (elementary, 1101)
                };

            \addplot coordinates {
                    (EndevaourOS, 2601)
                    (Manjaro, 2302)
                    (Garuda, 1081)
                };

            \addplot coordinates {
                    (Fedora, 997)
                };
            \legend{APT, Pacman, DNF}
        \end{axis}
    \end{tikzpicture}
\end{figure}
\end{document}

答案1

我刚刚找到了一种方法来移动条形组,使其看起来不错。虽然我并不觉得这个解决方案有多优雅,但它确实有效。

\documentclass{article}

\usepackage{pgfplots}

\begin{document}
\begin{figure}
    \centering
    \begin{tikzpicture}
        \begin{axis} [
                title={Top ten ranking distributions for page hits in 2021 on distrowatch.com},
                ybar,
                ylabel={Hits per day},
                ymin=0,
                ymax=4000,
                width=\textwidth,
                % xtick={1,2,3,4,5,6,7,8,9,10},
                % xticklabels={MX Linux,EndevaourOS,Manjaro,Mint,Pop! OS,Ubuntu,Debian,elementary,Garuda,Fedora}
                %xtick=data,%{MX Linux,EndevaourOS,Manjaro,Mint,Pop! OS,Ubuntu,Debian,elementary,Garuda,Fedora},
                x tick label style={rotate=90, align=center},
                symbolic x coords={MX Linux,EndevaourOS,Manjaro,Mint,Pop! OS,Ubuntu,Debian,elementary,Garuda,Fedora},
                % point meta=explicit symbolic,
                nodes near coords,
                % legend columns=3,
                % enlarge x limits=true,
                %unbounded coords=jump,
                %table/x expr=\coordindex,
            ]

            \addplot [red, fill=red!30!white, bar shift=0.5pt] coordinates {
                    (MX Linux, 3384)
                    (Mint, 2067)
                    (Pop! OS, 1668)
                    (Ubuntu, 1339)
                    (Debian, 1274)
                    (elementary, 1101)
                };

            \addplot [green, fill=green!20!white] coordinates {
                    (EndevaourOS, 2601)
                    (Manjaro, 2302)
                    (Garuda, 1081)
                };

            \addplot [blue, fill=blue!30!white, bar shift=-0.5pt] coordinates  {
                    (Fedora, 997)
                };

            \legend{APT, Pacman, DNF}
        \end{axis}
    \end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

相关内容