2

I have problems with using a two y-axis graph.

\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}

\end{tikzpicture} \end{document}

enter image description here

The first problem is that I can't perform line break with ylabel even though I indicated that I want a centered alignment (as stated here).

The second problem is : how to center or right align the symbols (not the legend entries) in the legend ?

Thank you in advance for your help!

  • 2
    The first problem is because you add compat=newest inside the axis. If you set it outside the axis, the problem disappears. – user194703 Apr 15 at 19:53
  • 1
    @Schrödinger'scat, I need to work on my speed skill ^^. – BambOo Apr 15 at 19:58
2

The issue with the ylabel is because of the compat=newest inside the axis. If you move it out, it disappears. It suffices to move it before ylabel style but I think moving it out is cleaner. As for centering the legend pictures I have no better solution than redefining the corresponding legend image code. (Of course, you can do that locally if needed.)

\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}

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.