pgfplots가 서브플롯을 정렬하면 Y 눈금 레이블이 잘못 정렬됨

pgfplots가 서브플롯을 정렬하면 Y 눈금 레이블이 잘못 정렬됨

Peter Grill이 수용한 솔루션에 따라이 질문, 하위 플롯을 수직으로 정렬하기 위해 다음 줄을 추가했습니다.

\pgfplotsset{yticklabel style={text width=3em,align=right}}

이 솔루션은 두 플롯 사이의 약간의 정렬 불량을 수정하는 데 효과적이지만 y 눈금 레이블을 엉망으로 만들어 플롯과 겹치게 만듭니다.

MWE:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{subfig}
\pgfplotsset{compat=1.17}

\begin{document}

\definecolor{bblue}{HTML}{4F81BD}
\definecolor{rred}{HTML}{C0504D}
\definecolor{ppurple}{HTML}{9F4C7C}

\pgfplotsset{yticklabel style={text width=3em,align=right}}
\begin{figure}[]
    \centering
    \subfloat[]{%
    \begin{tikzpicture}
        \begin{axis}[
            width = 0.9\textwidth,
            height = 8cm,
            xtick = data,
            enlarge x limits = 0.10,
            major x tick style = transparent,
            symbolic x coords = {2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020},
            ymajorgrids = true,
            ylabel = {Gastos Totais (R\$)},
            y coord trafo/.code = {\pgfmathparse{\pgfmathresult/1000000}},
            yticklabel = {\pgfmathprintnumber{\tick}\,mi},
            scaled y ticks = false,
            ybar = 2*\pgflinewidth,
            ymin = 0,
            bar width = 10pt,
            legend cell align = left,
            legend style = {
                at = {(1, 1.05)},
                anchor = south east,
                column sep = 1ex
            },
            /pgf/number format/.cd,
                1000 sep = {.}
            ]
            \addplot[style = {bblue, fill = bblue, mark = none}]
                coordinates {(2010, 134148978.40)
                             (2011, 163850342.64)
                             (2012, 189780916.97)
                             (2013, 226166578.45)
                             (2014, 246515645.81)
                             (2015, 313435568.42)
                             (2016, 321922725.99)
                             (2017, 351241496.32)
                             (2018, 348859916.86)
                             (2019, 464608106.68)
                             (2020, 316765254.56)};
            \addplot[style = {rred, fill = rred, mark = none}]
                coordinates {(2010, 584857230.67)
                             (2011, 633624150.04)
                             (2012, 675257494.54)
                             (2013, 730684305.39)
                             (2014, 837961674.33)
                             (2015, 890103343.15)
                             (2016, 937943259.40)
                             (2017, 988067801.53)
                             (2018, 1043622588.84)
                             (2019, 1264544768.36)
                             (2020, 844383399.89)};
            \legend{Defensoria Pública, Ministério Público}
        \end{axis}
    \end{tikzpicture}%
    }
    
    \subfloat[]{%
    \begin{tikzpicture}
        \begin{axis}[
            width = 0.9\textwidth,
            height = 6cm,
            grid = both,
            xtick = data,
            enlarge x limits = 0.10,
            symbolic x coords = {2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020},
            ylabel = {Diferença (R\$)},
            y coord trafo/.code={\pgfmathparse{\pgfmathresult/1000000}},
            yticklabel = \pgfmathprintnumber{\tick}\,mi,
            scaled y ticks = false
            ]
            \addplot[style = {ppurple, mark = *}]
                coordinates {(2010, 450708252.27)
                             (2011, 469773807.40)
                             (2012, 485476577.57)
                             (2013, 504517726.94)
                             (2014, 591446028.52)
                             (2015, 576667774.73)
                             (2016, 616020533.41)
                             (2017, 636826305.21)
                             (2018, 694762671.98)
                             (2019, 799936661.68)
                             (2020, 527618145.33)};
        \end{axis}
    \end{tikzpicture}%
    }
    \caption*{My Caption}
    \label{fig:gastos-mpdp}
\end{figure}
\end{document}

다음 이미지를 생성합니다.

여기에 이미지 설명을 입력하세요

조명이 레이블을 정렬하고 플롯을 잘못 정렬한다는 점을 주석 처리합니다. Figure 및 Subfloat 대신 테이블 환경을 사용해 보았지만 정확히 동일한 동작을 얻습니다.

답변1

tikzpicture다이어그램을 정렬하는 데 문제가 있는 경우 옵션을 사용하면 trim axis left해결될 수 있습니다.

\documentclass{article}
\usepackage{subfig}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\definecolor{bblue}{HTML}{4F81BD}
\definecolor{rred}{HTML}{C0504D}
\definecolor{ppurple}{HTML}{9F4C7C}

\begin{document}

\begin{figure}[]
\pgfplotsset{% common diagrams' options/parameters
    %yticklabel style={text width=3em,align=right},
    width = 0.9\textwidth,
    xtick = data,
    enlarge x limits = 0.10,
    symbolic x coords = {2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020},
    yticklabel = \pgfmathprintnumber{\tick}\,mi,
    scaled y ticks = false,
    y coord trafo/.code = {\pgfmathparse{\pgfmathresult/1000000}},
    tick label style = {font=\footnotesize}
    /pgf/number format/.cd,1000 sep = {.}
            }
\centering
    \subfloat[]{%
    \begin{tikzpicture}[trim axis left]
        \begin{axis}[
            height = 8cm,
            major x tick style = transparent,
            ymajorgrids = true,
            ylabel = {Gastos Totais (R\$)},
            ybar = 2*\pgflinewidth,
            bar width = 8pt,
            legend style = {at = {(1, 1.05)},
                            anchor = south east,
                            column sep = 1ex
                            },
            ]
            \addplot[style = {bblue, fill = bblue, mark = none}]
                coordinates {(2010, 134148978.40)
                             (2011, 163850342.64)
                             (2012, 189780916.97)
                             (2013, 226166578.45)
                             (2014, 246515645.81)
                             (2015, 313435568.42)
                             (2016, 321922725.99)
                             (2017, 351241496.32)
                             (2018, 348859916.86)
                             (2019, 464608106.68)
                             (2020, 316765254.56)};
            \addplot[style = {rred, fill = rred, mark = none}]
                coordinates {(2010, 584857230.67)
                             (2011, 633624150.04)
                             (2012, 675257494.54)
                             (2013, 730684305.39)
                             (2014, 837961674.33)
                             (2015, 890103343.15)
                             (2016, 937943259.40)
                             (2017, 988067801.53)
                             (2018, 1043622588.84)
                             (2019, 1264544768.36)
                             (2020, 844383399.89)};
            \legend{Defensoria Pública, Ministério Público}
        \end{axis}
    \end{tikzpicture}%
    }

    \subfloat[]{%
    \begin{tikzpicture}[trim axis left]
        \begin{axis}[
            height = 6cm,
            grid = both,
            ylabel = {Diferença (R\$)},
            ]
            \addplot[style = {ppurple, mark = *}]
                coordinates {(2010, 450708252.27)
                             (2011, 469773807.40)
                             (2012, 485476577.57)
                             (2013, 504517726.94)
                             (2014, 591446028.52)
                             (2015, 576667774.73)
                             (2016, 616020533.41)
                             (2017, 636826305.21)
                             (2018, 694762671.98)
                             (2019, 799936661.68)
                             (2020, 527618145.33)};
        \end{axis}
    \end{tikzpicture}%
    }
    \caption*{My Caption}
    \label{fig:gastos-mpdp}
\end{figure}
\end{document}

여기에 이미지 설명을 입력하세요

MWE와 비교하여 다음과 같은 변경이 이루어졌습니다.

  • 그림 위치 옵션이 추가되었습니다(대신 []경고 표시 No positions in optional float specifier가 사용됨 [htp]).
  • 두 사진의 공통 옵션은 \tikzset이후 에 수집됩니다 \begin{figure}[htp]. 이로써 오류가 제거됩니다 dimension is to large.
  • 너비가 ybar8pt로 줄어듭니다(막대 그룹이 더 잘 보이도록).
  • 눈금 라벨의 글꼴 크기가 \footnotesize.

다이어그램의 y축 레이블 사이의 거리가 두 이미지 모두에서 동일하도록 하려면 yticklabel style=...공통 tikzset및 제거 tikzpicture옵션 에서 주석 처리를 해제해야 합니다.[trim axis left]

관련 정보