2

I am in need of your help. I have a problem with which I am struggling for a few days, but can not find a solution to it.

I would like to place three charts side by side.

All charts should have the same x-axis-lable, y-axis-lable and legend. I.e. not each chart individually, but a global axis-lable so to speak.

Edit:

-All three y-axes are scaled the same and only need a global legend(and values).

-The x-axis values should remain, but the label (R) should be global.

-There can be multiple plots in each diagram, so it is nicer if the legend is flush over the three diagrams.

Edit2:

There is one small blemish. Is it possible to bring the legend (lots of text) exactly flush with the charts on the left and right? (See my example code!)

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{width=6cm,height=6cm}
\usetikzlibrary{calc}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}[baseline=(current axis.south)]
    \begin{axis}[name=leftplot, width=0.3\textwidth,height=0.2\textheight,
                    grid=major,                 
                    cycle list name=color,
                    ylabel={R},
%                   xlabel={x},
                    ylabel near ticks,
                    ]               
        \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
    \end{axis}
    \begin{axis}[name=middleplot, at ={($(leftplot.east)+(1cm,0)$)}, anchor=west,
                    width=0.3\textwidth,height=0.2\textheight,
                    grid=major,                 
                    cycle list name=color,
%                   ylabel={R},
                    xlabel={x},
                    ylabel near ticks,
                    legend style={at={(0.5,1.05)}, anchor=south, 
                        legend columns=3, column sep=6pt}
                    ]               
        \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
        \addlegendentry{11 1111 111 }
        \addlegendentry{22 2222 222};
        \addlegendentry{33 3333 333};
        \addlegendentry{44 4444 444};
        \addlegendentry{55 5555 555};
        \addlegendentry{66 6666 666};
    \end{axis}
    \begin{axis}[name=rightplot, at = {($(middleplot.east)+(1cm,0)$)}, anchor=west,
                    width=0.3\textwidth,height=0.2\textheight,
                    grid=major,                 
                    cycle list name=color,
%                   ylabel={R},
%                   xlabel={x},
                    ylabel near ticks,
                    legend style={at={(0.5,1.05)}, anchor=south, 
                        legend columns=3, column sep=6pt}
                    ]               
        \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
    \end{axis}
\end{tikzpicture}   
\caption{Example}
\end{figure}
        
\end{document}

Many greetings Parsec

1: Picture: Final (It should look like this) enter image description here

2: Code-Example

    \usepackage{pgfplots}
        \pgfplotsset{width=6cm,height=6cm}
        \begin{document}
        \begin{figure}
        \begin{center}
        \begin{minipage}{\linewidth}
        \centering
        \begin{tikzpicture}[baseline=(current axis.south)]
        \begin{axis}[width=0.3\textwidth,height=0.2\textheight,
                                    grid=major,                 
                                    cycle list name=color,
                                    ylabel={R},
                                    xlabel={x},
                                    ylabel near ticks,
                                    ]               
                                    \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
        \end{axis}
        \end{tikzpicture}   
        \begin{tikzpicture}[baseline=(current axis.south)]
        \begin{axis}[width=0.3\textwidth,height=0.2\textheight,
                                    grid=major,                 
                                    cycle list name=color,
                                    ylabel={R},
                                    xlabel={x},
                                    ylabel near ticks,
                                    legend style={at={(0.5,1.05)},anchor=south, legend columns=3,column sep=6pt}
]               
                                    \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                                \addlegendentry{1}
                                \addlegendentry{2};
                                \addlegendentry{3};
                                \addlegendentry{4};
                                \addlegendentry{5};
                                \addlegendentry{6};
        \end{axis}
        \end{tikzpicture}                           
        \begin{tikzpicture}[baseline=(current axis.south)]
        \begin{axis}[width=0.3\textwidth,height=0.2\textheight,
                                    grid=major,                 
                                    cycle list name=color,
                                    ylabel={R},
                                    xlabel={x},
                                    ylabel near ticks,
                                    ]                                       
                                    \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};                            
        \end{axis}
        \end{tikzpicture}
        \end{minipage}
        \end{center}
        \caption{Example}
        \end{figure}
        
        \end{document}
2
  • Just an idea. Don't label x axis on first and third plot, don't label y axis on second and third plot. Add external legend on third plot.
    – Ignasi
    Commented Mar 30, 2021 at 10:44
  • Thanks for your idea. I just tried this but run into a problem with the legend. Since I have multiple plots per chart I have to put them over the three images (after all). That would not succeed with your idea, since then the first and last diagram is shifted.
    – Parsec
    Commented Mar 30, 2021 at 11:46

1 Answer 1

1

Just a first test with OP's code. Every plot is an independent tikzpicture and they are aligned according their baseline.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{width=6cm,height=6cm}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}[baseline=(current axis.south)]
    \begin{axis}[width=0.3\textwidth,height=0.2\textheight,
                    grid=major,                 
                    cycle list name=color,
                    ylabel={R},
%                   xlabel={x},
                    ylabel near ticks,
                    ]               
        \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
    \end{axis}
\end{tikzpicture}   
\begin{tikzpicture}[baseline=(current axis.south)]
    \begin{axis}[width=0.3\textwidth,height=0.2\textheight,
                    grid=major,                 
                    cycle list name=color,
%                   ylabel={R},
                    xlabel={x},
                    ylabel near ticks,
                    legend style={at={(0.5,1.05)}, anchor=south, 
                        legend columns=3, column sep=6pt}
                    ]               
        \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
        \addlegendentry{1}
        \addlegendentry{2};
        \addlegendentry{3};
        \addlegendentry{4};
        \addlegendentry{5};
        \addlegendentry{6};
    \end{axis}
\end{tikzpicture}   
\begin{tikzpicture}[baseline=(current axis.south)]
    \begin{axis}[width=0.3\textwidth,height=0.2\textheight,
                    grid=major,                 
                    cycle list name=color,
%                   ylabel={R},
%                   xlabel={x},
                    ylabel near ticks,
                    legend style={at={(0.5,1.05)}, anchor=south, 
                        legend columns=3, column sep=6pt}
                    ]               
        \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
    \end{axis}
\end{tikzpicture}   
\caption{Example}
\end{figure}
        
\end{document}

enter image description here

Second option: All plots are inte the same tikzpicture and they are aligned according their "node" anchors fixed with a name option for every plot. This solution is shown in groupplots library section.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{width=6cm,height=6cm}
\usetikzlibrary{calc}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}[baseline=(current axis.south)]
    \begin{axis}[name=leftplot, width=0.3\textwidth,height=0.2\textheight,
                    grid=major,                 
                    cycle list name=color,
                    ylabel={R},
%                   xlabel={x},
                    ylabel near ticks,
                    ]               
        \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
    \end{axis}
    \begin{axis}[name=middleplot, at ={($(leftplot.east)+(1cm,0)$)}, anchor=west,
                    width=0.3\textwidth,height=0.2\textheight,
                    grid=major,                 
                    cycle list name=color,
%                   ylabel={R},
                    xlabel={x},
                    ylabel near ticks,
                    legend style={at={(0.5,1.05)}, anchor=south, 
                        legend columns=3, column sep=6pt}
                    ]               
        \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
        \addlegendentry{1}
        \addlegendentry{2};
        \addlegendentry{3};
        \addlegendentry{4};
        \addlegendentry{5};
        \addlegendentry{6};
    \end{axis}
    \begin{axis}[name=rightplot, at = {($(middleplot.east)+(1cm,0)$)}, anchor=west,
                    width=0.3\textwidth,height=0.2\textheight,
                    grid=major,                 
                    cycle list name=color,
%                   ylabel={R},
%                   xlabel={x},
                    ylabel near ticks,
                    legend style={at={(0.5,1.05)}, anchor=south, 
                        legend columns=3, column sep=6pt}
                    ]               
        \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
    \end{axis}
\end{tikzpicture}   
\caption{Example}
\end{figure}
        
\end{document}

enter image description here

Third option: Using groupplots library

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\pgfplotsset{width=6cm,height=6cm}
\usetikzlibrary{calc}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}[baseline=(current axis.south)]
\begin{groupplot}[%
    group style={%
        group size=3 by 1,
    },
    width=0.3\textwidth,height=0.2\textheight,
    grid=major, cycle list name=color, ylabel near ticks,
    legend style={at={(0.5,1.05)}, anchor=south, legend columns=3, column sep=6pt}
    ] 
    \nextgroupplot[ylabel={R}]           
        \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
    \nextgroupplot[xlabel={x}]            
        \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
        \addlegendentry{1};
        \addlegendentry{2};
        \addlegendentry{3};
        \addlegendentry{4};
        \addlegendentry{5};
        \addlegendentry{6};
    \nextgroupplot            
        \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
\end{groupplot} 
\end{tikzpicture}
\end{figure}
\end{document}

enter image description here

Update: Aligning with legend margins

To align first and third plot with the long legend borders, the easiest way is to draw the central plot first and use outer anchors (the legend defines them) to fix lateral plots.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{width=6cm,height=6cm}
\usetikzlibrary{calc}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}[baseline=(current axis.south)]
    \begin{axis}[name=middleplot, 
                    width=0.3\textwidth,height=0.2\textheight,
                    grid=major,                 
                    cycle list name=color,
%                   ylabel={R},
                    xlabel={x},
                    ylabel near ticks,
                    legend style={at={(0.5,1.05)}, anchor=south, 
                        legend columns=3, column sep=6pt}
                    ]               
        \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
        \addlegendentry{11 1111 111 }
        \addlegendentry{22 2222 222};
        \addlegendentry{33 3333 333};
        \addlegendentry{44 4444 444};
        \addlegendentry{55 5555 555};
        \addlegendentry{66 6666 666};
    \end{axis}
    \begin{axis}[name=leftplot, at ={(middleplot.outer west|-middleplot.center)}, anchor=west,
                         width=0.3\textwidth,height=0.2\textheight,
                    grid=major,                 
                    cycle list name=color,
                    ylabel={R},
%                   xlabel={x},
                    ylabel near ticks,
                    ]               
        \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
    \end{axis}
    \begin{axis}[name=rightplot, at = {(middleplot.center-|middleplot.outer east)}, anchor=east,
                    width=0.3\textwidth,height=0.2\textheight,
                    grid=major,                 
                    cycle list name=color,
%                   ylabel={R},
%                   xlabel={x},
                    ylabel near ticks,
                    legend style={at={(0.5,1.05)}, anchor=south, 
                        legend columns=3, column sep=6pt}
                    ]               
        \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
                \addplot coordinates {(1,6.45)(2,3.4)(3,4.8)(4,4.1)(5,4.4)};
    \end{axis}
\end{tikzpicture}   
\caption{Example}
\end{figure}
        
\end{document}

enter image description here

3
  • Hello, thank you very much for your detailed answer. The second code seems to be very close to the solution for my problem. Thank you very much for that. There is one small blemish. Is it possible to bring the legend (lots of text) exactly flush with the charts on the left and right? (See my example code!) Many greetings Parsec
    – Parsec
    Commented Mar 31, 2021 at 7:29
  • Hello, thank you very much for your detailed answer. The second code seems to be very close to the solution for my problem. Thank you very much for that. There is one small blemish. Is it possible to bring the legend (lots of text) exactly flush with the charts on the left and right? (See my example code!: EDIT2). Many greetings Parsec
    – Parsec
    Commented Mar 31, 2021 at 7:38
  • @Parsec Take a look at updated answer. I think I did what you wanted
    – Ignasi
    Commented Mar 31, 2021 at 9:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .