条形图垂直对齐和缩放

条形图垂直对齐和缩放

我的条形图垂直对齐有问题。我开始使用单个 xbar 图表来显示我的数据,但它看起来很糟糕,因为一个项目非常大,另一个项目非常小。因此,我想为每个项目制作多个图表,但它们并没有真正对齐。这是第一个版本,其中缩放是灾难性的。我希望能够比较不同的图表。

如果我尝试制作多个 xbar 图表,它们不会对齐。

在此处输入图片描述

\documentclass{report}
\usepackage{pgfplots}
\usepackage{tikz}
\begin{document}
\begin{figure}
\begin{tikzpicture} 
\begin{axis}[tickwidth = 0pt,xbar, xmin=0, width=12cm, height=3.5cm, enlarge y limits=0.5, symbolic y coords={no,yes}, ytick=data, nodes near coords,y axis line style = { opacity = 0 },
   axis x line       = none ] 
\addplot coordinates {(1000000,yes) (2000000,no)}; 
\addplot coordinates {(4000000,yes) (1000000,no)}; 
\end{axis} 
\end{tikzpicture}
\begin{tikzpicture} 
\begin{axis}[  tickwidth= 0pt,xbar, xmin=0, width=12cm, height=3.5cm, enlarge y limits=0.5, symbolic y coords={noooooo,yeeeeees}, ytick=data, nodes near coords,y axis line style = { opacity = 0 },
   axis x line       = none ] 
\addplot coordinates {(3,noooooo) (7,yeeeeees)};
\addplot coordinates {(30,noooooo) (1,yeeeeees)}; 
\end{axis} 
\end{tikzpicture}
\end{figure}

\begin{figure}
\begin{tikzpicture} 
\begin{axis}[  tickwidth= 0pt,xbar, xmin=0, enlarge y limits=0.5, symbolic y coords={noooooo,yeeeeees, yes, no}, ytick=data, nodes near coords,y axis line style = { opacity = 0 },
   axis x line       = none ] 
\addplot coordinates {(3,noooooo) (7,yeeeeees) (1000000,yes) (2000000,no)};
\addplot coordinates {(30,noooooo) (1,yeeeeees)(4000000,yes) (1000000,no)}; 
\end{axis} 
\end{tikzpicture}
\end{figure}
\end{document}

答案1

我猜想垂直对齐图的最简单方法就是利用库groupplots

symbolic coords(请注意,目前在 中使用时似乎存在错误groupplot。这就是为什么我需要使用不同的方法来提供数据。而且因为我无论如何都“被迫”重新陈述数据,所以我还将它们从 切换到 ,coordinatestable更加灵活(同样)。)

有关详细信息,请查看代码中的注释。

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % load the `groupplots` library
    \usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
    \begin{groupplot}[
        group style={
            % define `groupplot` size (2 vertical plots) ...
            group size=1 by 2,
            % ... and the distance between the plots
            vertical sep=5mm,
        },
        % (here are your options which are in common for all plots)
        width=12cm,
        height=3.5cm,
        tickwidth=0pt,
        xbar,
        xmin=0,
        enlarge y limits={abs=0.5},     % <-- (adapted)
        ytick=data,
        nodes near coords,
        y axis line style={opacity=0},
        axis x line=none,
        legend style={
            % position the legend below the plot
            at={(0.0,-0.1)},
            anchor=north west,
            % (add the rest of style you want to use here)
        },
        % use the coordinate index for the y value
        % (which is needed after restating the data `coordinates` to a `table`)
        table/y expr={\coordindex},
    ]
    % start the first plot
    \nextgroupplot[
        % names to be stated as `yticklabels`
        % (which were before the `symbolic y coords`)
        yticklabels={no,yes},
    ]
        % restated data from `coordinates` to `table` which is much simpler
        % (one could also combine all data in a single data file and call
        %  different columns from that here)
        \addplot table {
            1000000
            2000000
        };
        \addplot table {
            4000000
            1000000
        };
    \nextgroupplot[yticklabels={noooooo,yeeeeees}]
        \addplot table {
            3
            7
        };
        \addplot table {
            30
            1
        };
        % add the legend entries to the last `\nextgroupplot`
        \legend{a,b}
    \end{groupplot}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容