0
\documentclass{article}
\usepackage{pgfplots, tikz}
\usepackage{adjustbox}  % table scale


\begin{document}
\begin{figure}
\centering
\begin{adjustbox}{max width=.75\textwidth}
\begin{tikzpicture}
    \pgfplotsset{
        symbolic x coords={40,60,80,100,120},
        xtick=data,
        legend columns=-1,
        legend style={draw=none},
        legend to name=named,
    }

    \begin{axis}[
    axis y line*=left,
    xlabel=x-axis,
    ylabel=y-axis 1,
    ybar stacked, ymin=0,
    bar width=7mm, 
    legend entries={a,b},
    ]
    \addplot [fill=blue] coordinates {
        ({40},15)
        ({60},25)
        ({80},35)
        ({100},15)
        ({120},10)
    };
    \addplot [fill=red] coordinates {
        ({40},10)
        ({60},35)
        ({80},30)
        ({100},25)
        ({120},10)
    };
    \end{axis}

    \begin{axis}[
    axis y line*=right,
    ylabel=y-axis 2, legend entries={time},
    ]


    \addplot[smooth,mark=*,black]
    coordinates{
        ({40},15)
        ({60},25)
        ({80},35)
        ({100},15)
        ({120},10)
    };
    \end{axis}

    \end{tikzpicture}
\end{adjustbox}
\\
Sample: \ref{named}
\end{figure}
\end{document}

enter image description here

Problems:

  1. y-axis 2 label not appearing properly (appears left).

  2. Legends not showing for stacked plots.

  3. The text "Sample" and the legend seems to not appearing horizontally in the same line.

  • Possible duplicat: tex.stackexchange.com/a/199003/124842 – Bobyandbob Feb 24 '18 at 7:44
  • I have created MWE from that link, for using my purpose. But not able to solve. Legend would be outside. Why Y axis-2 appearing on left side? – Mr.EU Feb 24 '18 at 7:47
2

This solution is a combination of @cfr answer here and @soapygopher answer here. The double text could be removed, by adding only one axis label. The legend position is defined with legend style={at={(0.5,-0.2)},anchor=north} for both axis enviroments.

Here is the updated answer, because of question 3. You could add some text with inserting an extra column legend columns=4 instead of legend columns=3 with legend style you could do some format changes like text width. To add the extra text/titel use \addlegendimage{empty legend} and \addlegendentry{\textbf{Sample:}}. To add the second ylabel on the right side use ylabel=y-axis 2, ylabel near ticks, yticklabel pos=right, instead of axis y line*=right,:

\documentclass{article}
\usepackage{pgfplots, tikz}
\usepackage{adjustbox}  % table scale


\begin{document}

\begin{tikzpicture}
    \pgfplotsset{
        symbolic x coords={40,60,80,100,120},
        xtick=data,
        legend columns=4,
        legend style={
                    /tikz/every even column/.append style={text width=1.4cm}
                        },
    }

    \begin{axis}[
    axis y line*=left,
    xlabel=x-axis 1,
    ylabel=y-axis 1,
    ybar stacked, ymin=0,
    bar width=7mm, legend style={at={(0.5,-0.2)},anchor=north}
    ]

    \addplot [fill=blue,draw=none,area legend] coordinates {
        ({40},15)
        ({60},25)
        ({80},35)
        ({100},15)
        ({120},10)
    };\label{plot_one}
\addlegendentry{plot 1}
    \addplot [fill=red,draw=none,area legend] coordinates {
        ({40},10)
        ({60},35)
        ({80},30)
        ({100},25)
        ({120},10)
    };\label{plot_two}
\addlegendentry{plot 2}
    \end{axis}

 \begin{axis}[
    ylabel=y-axis 2, ylabel near ticks, yticklabel pos=right,legend style={at={(0.5,-0.2)},anchor=north},
    ]
\addlegendimage{empty legend}
\addlegendentry{\hspace*{0cm}\textbf{Sample:}}
\addlegendimage{/pgfplots/refstyle=plot_one}\addlegendentry{plot 1}
\addlegendimage{/pgfplots/refstyle=plot_two}\addlegendentry{plot 2}
    \addplot[smooth,mark=*,black]
    coordinates{
        ({40},15)
        ({60},25)
        ({80},35)
        ({100},15)
        ({120},10)
    };
    \addlegendentry{plot 3}
    \end{axis}

    \end{tikzpicture}

\end{document}

enter image description here

Or with 2x \begin{axis}[...,ymin=0,ymax=70, ...] you get:

enter image description here

  • Thanks for the ans. I updated some 1 extra requirement. Moreover, axis titles are required. [link] (overleaf.com/13996730spkbxmgbzjkh#/54278642) – Mr.EU Feb 24 '18 at 8:35
  • @Mr.EU : Do you want to add the word "Sample" to the legend? Related to tex.stackexchange.com/a/2332/124842, but horizontal? -Then you could add legend style={ /tikz/every even column/.append style={text width=1.4cm} }, to pgfplotsset , legend columns=4, instead of legend columns=4, and \addlegendimage{empty legend} \addlegendentry{\hspace*{0cm}\textbf{Sample:}} before your first legen entry. – Bobyandbob Feb 24 '18 at 10:26
  • thanks, can you please get the axis problem solve? – Mr.EU Feb 24 '18 at 10:32
  • great. Thanks. only "Y axis 2" Title is required in right side. Also what causes it overlapping? – Mr.EU Feb 24 '18 at 10:56
  • 1
    Do you want the label Y axis 2 on the right side? SO add: ` \begin{axis}[ ylabel=y-axis 2, ylabel near ticks, yticklabel pos=right,legend style={at={(0.5,-0.2)},anchor=north}, ]` i will update my answer. – Bobyandbob Feb 24 '18 at 11:02

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.