带有 ylabel 和 legend 的两个 y 轴图形问题 - PGFplots

带有 ylabel 和 legend 的两个 y 轴图形问题 - PGFplots

我在使用双 y 轴图时遇到了问题。

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis y line*=right,
        ymin=0, ymax=300,
        ylabel style={align=center},
        ylabel={Very very very very very very long ylabel\\with a line break},
        width=0.9\linewidth,
        height=8cm,
        ymajorgrids,
        xtick ={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
        compat=newest,
        ybar
    ]

    \addplot[bar width=5 pt, draw=blue, fill=blue!10] coordinates{
        (1,96)(2,114.9)(3,169.7)(4,182.1)(5,217.4)(6,238.7)(7,248.5)(8,242.3)(9,202.7)(10,147.2)(11,94.4)(12,81.8)
    };
    \label{C}

    \addplot[bar width=5 pt, draw=red, fill=red!10] coordinates{
        (1,88)(2,103)(3,171)(4,180)(5,199)(6,220)(7,250)(8,202)(9,166)(10,163)(11,140)(12,94)
    };
    \label{D}
\end{axis}

\begin{axis}[
    axis y line*=left,
    ymin=0, ymax=30,
    xtick ={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
    xticklabels={},
    ylabel=ylabel1,
    width=0.9\linewidth,
    height=8cm,
    compat=newest,
    legend style={at={(0,1)},anchor=north west},
]

    \addplot+[mark=none, smooth, draw=blue, thick] coordinates{
        (1,3.1)(2,3.3)(3,5.4)(4,7.4)(5,11.0)(6,14.1)(7,15.8)(8,15.7)(9,12.9)(10,10.4)(11,6.1)(12,3.8)
    };
    \addlegendentry{A}

    \addplot+[mark=none, smooth, draw=red, thick] coordinates{
      (1,10.1)(2,11.7)(3,15.1)(4,17.3)(5,21.2)(6,24.5)(7,26.9)(8,27.1)(9,24.0)(10,19.4)(11,13.7)(12,10.5)
    };
    \addlegendentry{B}
    \addlegendimage{/pgfplots/refstyle=C,ybar, ybar legend}\addlegendentry{C}
    \addlegendimage{/pgfplots/refstyle=D,ybar, ybar legend}\addlegendentry{D}
\end{axis}

\结束{tikzpicture} \结束{文档}

在此处输入图片描述

ylabel第一个问题是,尽管我表示我想要居中对齐,但我无法执行换行(如上所述这里)。

第二个问题是:如何使图例中的符号(不是图例条目)居中或右对齐?

预先感谢您的帮助!

答案1

的问题ylabel是由于compat=newest轴内的。如果你把它移出,它就会消失。之前移动它就足够了ylabel style,但我认为把它移出更干净。至于将图例图片居中,我没有比重新定义相应的更好的解决方案legend image code。(当然,如果需要,你可以在本地执行此操作。)

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest,
/pgfplots/ybar legend/.style={
/pgfplots/legend image code/.code={
\draw [##1,/tikz/.cd,bar width=3pt,yshift=-0.2em,bar shift=0pt]
plot coordinates {(0.5em,0.8em) (0.5em+2*\pgfplotbarwidth,0.6em)}; },
}}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis y line*=right,
        ymin=0, ymax=300,
        ylabel style={align=center},
        ylabel={Very very very very very very long ylabel\\with a line break},
        width=0.9\linewidth,
        height=8cm,
        ymajorgrids,
        xtick ={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
        ybar
    ]
    \addplot[bar width=5 pt, draw=blue, fill=blue!10] coordinates{
        (1,96)(2,114.9)(3,169.7)(4,182.1)(5,217.4)(6,238.7)(7,248.5)(8,242.3)(9,202.7)(10,147.2)(11,94.4)(12,81.8)
    };
    \label{C}

    \addplot[bar width=5 pt, draw=red, fill=red!10] coordinates{
        (1,88)(2,103)(3,171)(4,180)(5,199)(6,220)(7,250)(8,202)(9,166)(10,163)(11,140)(12,94)
    };
    \label{D}
\end{axis}

\begin{axis}[
    axis y line*=left,
    ymin=0, ymax=30,
    xtick ={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
    xticklabels={},
    ylabel=ylabel1,
    width=0.9\linewidth,
    height=8cm,
    legend style={at={(0,1)},anchor=north west},
]

    \addplot+[mark=none, smooth, draw=blue, thick] coordinates{
        (1,3.1)(2,3.3)(3,5.4)(4,7.4)(5,11.0)(6,14.1)(7,15.8)(8,15.7)(9,12.9)(10,10.4)(11,6.1)(12,3.8)
    };
    \addlegendentry{A}

    \addplot+[mark=none, smooth, draw=red, thick] coordinates{
      (1,10.1)(2,11.7)(3,15.1)(4,17.3)(5,21.2)(6,24.5)(7,26.9)(8,27.1)(9,24.0)(10,19.4)(11,13.7)(12,10.5)
    };
    \addlegendentry{B}
    \addlegendimage{/pgfplots/refstyle=C,ybar, ybar legend}\addlegendentry{C}
    \addlegendimage{/pgfplots/refstyle=D,ybar, ybar legend}\addlegendentry{D}
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容