막대 차트 수직 정렬 및 크기 조정

막대 차트 수직 정렬 및 크기 조정

막대형 차트의 수직 정렬에 문제가 있습니다. 내 데이터에 단일 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}

위 코드의 결과를 보여주는 이미지

관련 정보