pgfplots: How to align legends of multigraph diagram?












3















Assumed we have the following MWE to display various graphs in one single diagram.





Minimum working example (MWE):



documentclass[border=5mm]{standalone}
usepackage{pgfplots}

pgfplotsset{compat=newest, scale only axis}

begin{document}

begin{tikzpicture}
begin{axis}[
axis y line* = left,
legend style = {at={(0.5,-0.125)},
anchor = north,
legend columns = 3,
/tikz/every even column/.append style = {column sep=0.5cm},
/tikz/every odd column/.append style = {column sep=0.15cm},
},
]%
addplot coordinates {(0,0) (1,1)};
legend{Graph 1};
end{axis}%
%
begin{axis}[
axis x line = none,
axis y line* = right,
legend style = {at={(0.5,-0.125)},
anchor = north,
legend columns = 3,
/tikz/every even column/.append style = {column sep=0.5cm},
/tikz/every odd column/.append style = {column sep=0.15cm},
},
]%
addplot coordinates {(0,1) (1,0)};
addplot coordinates {(0,0.5) (1,0.5)};
legend{Graph 1, Graph 2};
end{axis}%
%
begin{axis}[
axis x line = none,
axis y line* = right,
legend style = {at={(0.5,-0.125)},
anchor = north,
legend columns = 3,
/tikz/every even column/.append style = {column sep=0.5cm},
/tikz/every odd column/.append style = {column sep=0.15cm},
},
]%
pgfplotsset%
{%
every outer y axis line/.style = {xshift=2cm},
every tick/.style = {xshift=2cm},
every y tick label/.style = {xshift=2cm},
}%
addplot coordinates {(0.5,0) (0.5,1)};
legend{Graph 4};
end{axis}%

end{tikzpicture}

end{document}




Screenshot of the result:



Screenshot of the result





Description of the issue:



As you can see, the legends are looking very ugly. How can I adjust the legends centered below the plot?





Update:



I've posted a further question about how to align the legend items in two rows or columns to let them appear nicely.










share|improve this question





























    3















    Assumed we have the following MWE to display various graphs in one single diagram.





    Minimum working example (MWE):



    documentclass[border=5mm]{standalone}
    usepackage{pgfplots}

    pgfplotsset{compat=newest, scale only axis}

    begin{document}

    begin{tikzpicture}
    begin{axis}[
    axis y line* = left,
    legend style = {at={(0.5,-0.125)},
    anchor = north,
    legend columns = 3,
    /tikz/every even column/.append style = {column sep=0.5cm},
    /tikz/every odd column/.append style = {column sep=0.15cm},
    },
    ]%
    addplot coordinates {(0,0) (1,1)};
    legend{Graph 1};
    end{axis}%
    %
    begin{axis}[
    axis x line = none,
    axis y line* = right,
    legend style = {at={(0.5,-0.125)},
    anchor = north,
    legend columns = 3,
    /tikz/every even column/.append style = {column sep=0.5cm},
    /tikz/every odd column/.append style = {column sep=0.15cm},
    },
    ]%
    addplot coordinates {(0,1) (1,0)};
    addplot coordinates {(0,0.5) (1,0.5)};
    legend{Graph 1, Graph 2};
    end{axis}%
    %
    begin{axis}[
    axis x line = none,
    axis y line* = right,
    legend style = {at={(0.5,-0.125)},
    anchor = north,
    legend columns = 3,
    /tikz/every even column/.append style = {column sep=0.5cm},
    /tikz/every odd column/.append style = {column sep=0.15cm},
    },
    ]%
    pgfplotsset%
    {%
    every outer y axis line/.style = {xshift=2cm},
    every tick/.style = {xshift=2cm},
    every y tick label/.style = {xshift=2cm},
    }%
    addplot coordinates {(0.5,0) (0.5,1)};
    legend{Graph 4};
    end{axis}%

    end{tikzpicture}

    end{document}




    Screenshot of the result:



    Screenshot of the result





    Description of the issue:



    As you can see, the legends are looking very ugly. How can I adjust the legends centered below the plot?





    Update:



    I've posted a further question about how to align the legend items in two rows or columns to let them appear nicely.










    share|improve this question



























      3












      3








      3








      Assumed we have the following MWE to display various graphs in one single diagram.





      Minimum working example (MWE):



      documentclass[border=5mm]{standalone}
      usepackage{pgfplots}

      pgfplotsset{compat=newest, scale only axis}

      begin{document}

      begin{tikzpicture}
      begin{axis}[
      axis y line* = left,
      legend style = {at={(0.5,-0.125)},
      anchor = north,
      legend columns = 3,
      /tikz/every even column/.append style = {column sep=0.5cm},
      /tikz/every odd column/.append style = {column sep=0.15cm},
      },
      ]%
      addplot coordinates {(0,0) (1,1)};
      legend{Graph 1};
      end{axis}%
      %
      begin{axis}[
      axis x line = none,
      axis y line* = right,
      legend style = {at={(0.5,-0.125)},
      anchor = north,
      legend columns = 3,
      /tikz/every even column/.append style = {column sep=0.5cm},
      /tikz/every odd column/.append style = {column sep=0.15cm},
      },
      ]%
      addplot coordinates {(0,1) (1,0)};
      addplot coordinates {(0,0.5) (1,0.5)};
      legend{Graph 1, Graph 2};
      end{axis}%
      %
      begin{axis}[
      axis x line = none,
      axis y line* = right,
      legend style = {at={(0.5,-0.125)},
      anchor = north,
      legend columns = 3,
      /tikz/every even column/.append style = {column sep=0.5cm},
      /tikz/every odd column/.append style = {column sep=0.15cm},
      },
      ]%
      pgfplotsset%
      {%
      every outer y axis line/.style = {xshift=2cm},
      every tick/.style = {xshift=2cm},
      every y tick label/.style = {xshift=2cm},
      }%
      addplot coordinates {(0.5,0) (0.5,1)};
      legend{Graph 4};
      end{axis}%

      end{tikzpicture}

      end{document}




      Screenshot of the result:



      Screenshot of the result





      Description of the issue:



      As you can see, the legends are looking very ugly. How can I adjust the legends centered below the plot?





      Update:



      I've posted a further question about how to align the legend items in two rows or columns to let them appear nicely.










      share|improve this question
















      Assumed we have the following MWE to display various graphs in one single diagram.





      Minimum working example (MWE):



      documentclass[border=5mm]{standalone}
      usepackage{pgfplots}

      pgfplotsset{compat=newest, scale only axis}

      begin{document}

      begin{tikzpicture}
      begin{axis}[
      axis y line* = left,
      legend style = {at={(0.5,-0.125)},
      anchor = north,
      legend columns = 3,
      /tikz/every even column/.append style = {column sep=0.5cm},
      /tikz/every odd column/.append style = {column sep=0.15cm},
      },
      ]%
      addplot coordinates {(0,0) (1,1)};
      legend{Graph 1};
      end{axis}%
      %
      begin{axis}[
      axis x line = none,
      axis y line* = right,
      legend style = {at={(0.5,-0.125)},
      anchor = north,
      legend columns = 3,
      /tikz/every even column/.append style = {column sep=0.5cm},
      /tikz/every odd column/.append style = {column sep=0.15cm},
      },
      ]%
      addplot coordinates {(0,1) (1,0)};
      addplot coordinates {(0,0.5) (1,0.5)};
      legend{Graph 1, Graph 2};
      end{axis}%
      %
      begin{axis}[
      axis x line = none,
      axis y line* = right,
      legend style = {at={(0.5,-0.125)},
      anchor = north,
      legend columns = 3,
      /tikz/every even column/.append style = {column sep=0.5cm},
      /tikz/every odd column/.append style = {column sep=0.15cm},
      },
      ]%
      pgfplotsset%
      {%
      every outer y axis line/.style = {xshift=2cm},
      every tick/.style = {xshift=2cm},
      every y tick label/.style = {xshift=2cm},
      }%
      addplot coordinates {(0.5,0) (0.5,1)};
      legend{Graph 4};
      end{axis}%

      end{tikzpicture}

      end{document}




      Screenshot of the result:



      Screenshot of the result





      Description of the issue:



      As you can see, the legends are looking very ugly. How can I adjust the legends centered below the plot?





      Update:



      I've posted a further question about how to align the legend items in two rows or columns to let them appear nicely.







      pgfplots horizontal-alignment vertical-alignment positioning legend






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 19 at 16:24







      Dave

















      asked Mar 19 at 14:13









      DaveDave

      949619




      949619






















          1 Answer
          1






          active

          oldest

          votes


















          4














          You could use legend to name for that.



          documentclass[border=5mm]{standalone}
          usepackage{pgfplots}
          usetikzlibrary{positioning}
          pgfplotsset{compat=newest, scale only axis}
          begin{document}

          begin{tikzpicture}
          begin{scope}[local bounding box=plots]
          begin{axis}[
          axis y line* = left,
          legend to name = legA,
          legend style = {
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,0) (1,1)};
          legend{Graph 1};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legB,
          legend style = {
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,1) (1,0)};
          addplot coordinates {(0,0.5) (1,0.5)};
          legend{Graph 1, Graph 2};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legC,
          legend style = {
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          pgfplotsset%
          {%
          every outer y axis line/.style = {xshift=2cm},
          every tick/.style = {xshift=2cm},
          every y tick label/.style = {xshift=2cm},
          }%
          addplot coordinates {(0.5,0) (0.5,1)};
          legend{Graph 4};
          end{axis}%
          end{scope}
          node[below=0.5em of plots.south] (legB) {ref{legB}};
          node[left=1em of legB] (legA) {ref{legA}};
          node[right=1em of legB] (legC) {ref{legC}};
          end{tikzpicture}

          end{document}


          enter image description here



          All legends in one box:



          documentclass[border=5mm]{standalone}
          usepackage{pgfplots}
          usetikzlibrary{positioning}
          pgfplotsset{compat=newest, scale only axis}
          begin{document}

          begin{tikzpicture}
          begin{scope}[local bounding box=plots]
          begin{axis}[
          axis y line* = left,
          legend to name = legA,
          legend style = {draw=none,
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,0) (1,1)};
          legend{Graph 1};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legB,
          legend style = {draw=none,
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,1) (1,0)};
          addplot coordinates {(0,0.5) (1,0.5)};
          legend{Graph 1, Graph 2};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legC,
          legend style = {draw=none,
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          pgfplotsset%
          {%
          every outer y axis line/.style = {xshift=2cm},
          every tick/.style = {xshift=2cm},
          every y tick label/.style = {xshift=2cm},
          }%
          addplot coordinates {(0.5,0) (0.5,1)};
          legend{Graph 4};
          end{axis}%
          end{scope}
          node[below=0.5em of plots.south] (legB) {ref{legB}};
          node[left=0.2em of legB] (legA) {ref{legA}};
          node[right=0.2em of legB] (legC) {ref{legC}};
          draw (legA.south west) rectangle (legC.north east);
          end{tikzpicture}
          end{document}


          enter image description here






          share|improve this answer


























          • Thanks a lot! But would it be possible to add all legend items into one single legend? So that it looks "the classic way" with all items within one single border?

            – Dave
            Mar 19 at 15:06











          • @Dave Yes. I added one way of doing that. (Of course, in the present plot you could just draw all plots in one axis and just build up one legend. I am assuming that your real life example has plots with different coordinate systems combined.)

            – marmot
            Mar 19 at 15:11













          • Ah, thanks a lot for your great help! :-) I just posted a new screenshot to explain it better... Would it be possible to add the legends in two rows? Because right now the command legend columns = 3 does not work anymore. I think this command is not usable anymore with your solution, right?

            – Dave
            Mar 19 at 15:13













          • Yes exactly, in the real coordinate system I have different axis scalings, e.g. 0 - 100 for the first axis and 0 - 20 for the second one etc.

            – Dave
            Mar 19 at 15:14














          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "85"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f480282%2fpgfplots-how-to-align-legends-of-multigraph-diagram%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          You could use legend to name for that.



          documentclass[border=5mm]{standalone}
          usepackage{pgfplots}
          usetikzlibrary{positioning}
          pgfplotsset{compat=newest, scale only axis}
          begin{document}

          begin{tikzpicture}
          begin{scope}[local bounding box=plots]
          begin{axis}[
          axis y line* = left,
          legend to name = legA,
          legend style = {
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,0) (1,1)};
          legend{Graph 1};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legB,
          legend style = {
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,1) (1,0)};
          addplot coordinates {(0,0.5) (1,0.5)};
          legend{Graph 1, Graph 2};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legC,
          legend style = {
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          pgfplotsset%
          {%
          every outer y axis line/.style = {xshift=2cm},
          every tick/.style = {xshift=2cm},
          every y tick label/.style = {xshift=2cm},
          }%
          addplot coordinates {(0.5,0) (0.5,1)};
          legend{Graph 4};
          end{axis}%
          end{scope}
          node[below=0.5em of plots.south] (legB) {ref{legB}};
          node[left=1em of legB] (legA) {ref{legA}};
          node[right=1em of legB] (legC) {ref{legC}};
          end{tikzpicture}

          end{document}


          enter image description here



          All legends in one box:



          documentclass[border=5mm]{standalone}
          usepackage{pgfplots}
          usetikzlibrary{positioning}
          pgfplotsset{compat=newest, scale only axis}
          begin{document}

          begin{tikzpicture}
          begin{scope}[local bounding box=plots]
          begin{axis}[
          axis y line* = left,
          legend to name = legA,
          legend style = {draw=none,
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,0) (1,1)};
          legend{Graph 1};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legB,
          legend style = {draw=none,
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,1) (1,0)};
          addplot coordinates {(0,0.5) (1,0.5)};
          legend{Graph 1, Graph 2};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legC,
          legend style = {draw=none,
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          pgfplotsset%
          {%
          every outer y axis line/.style = {xshift=2cm},
          every tick/.style = {xshift=2cm},
          every y tick label/.style = {xshift=2cm},
          }%
          addplot coordinates {(0.5,0) (0.5,1)};
          legend{Graph 4};
          end{axis}%
          end{scope}
          node[below=0.5em of plots.south] (legB) {ref{legB}};
          node[left=0.2em of legB] (legA) {ref{legA}};
          node[right=0.2em of legB] (legC) {ref{legC}};
          draw (legA.south west) rectangle (legC.north east);
          end{tikzpicture}
          end{document}


          enter image description here






          share|improve this answer


























          • Thanks a lot! But would it be possible to add all legend items into one single legend? So that it looks "the classic way" with all items within one single border?

            – Dave
            Mar 19 at 15:06











          • @Dave Yes. I added one way of doing that. (Of course, in the present plot you could just draw all plots in one axis and just build up one legend. I am assuming that your real life example has plots with different coordinate systems combined.)

            – marmot
            Mar 19 at 15:11













          • Ah, thanks a lot for your great help! :-) I just posted a new screenshot to explain it better... Would it be possible to add the legends in two rows? Because right now the command legend columns = 3 does not work anymore. I think this command is not usable anymore with your solution, right?

            – Dave
            Mar 19 at 15:13













          • Yes exactly, in the real coordinate system I have different axis scalings, e.g. 0 - 100 for the first axis and 0 - 20 for the second one etc.

            – Dave
            Mar 19 at 15:14


















          4














          You could use legend to name for that.



          documentclass[border=5mm]{standalone}
          usepackage{pgfplots}
          usetikzlibrary{positioning}
          pgfplotsset{compat=newest, scale only axis}
          begin{document}

          begin{tikzpicture}
          begin{scope}[local bounding box=plots]
          begin{axis}[
          axis y line* = left,
          legend to name = legA,
          legend style = {
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,0) (1,1)};
          legend{Graph 1};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legB,
          legend style = {
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,1) (1,0)};
          addplot coordinates {(0,0.5) (1,0.5)};
          legend{Graph 1, Graph 2};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legC,
          legend style = {
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          pgfplotsset%
          {%
          every outer y axis line/.style = {xshift=2cm},
          every tick/.style = {xshift=2cm},
          every y tick label/.style = {xshift=2cm},
          }%
          addplot coordinates {(0.5,0) (0.5,1)};
          legend{Graph 4};
          end{axis}%
          end{scope}
          node[below=0.5em of plots.south] (legB) {ref{legB}};
          node[left=1em of legB] (legA) {ref{legA}};
          node[right=1em of legB] (legC) {ref{legC}};
          end{tikzpicture}

          end{document}


          enter image description here



          All legends in one box:



          documentclass[border=5mm]{standalone}
          usepackage{pgfplots}
          usetikzlibrary{positioning}
          pgfplotsset{compat=newest, scale only axis}
          begin{document}

          begin{tikzpicture}
          begin{scope}[local bounding box=plots]
          begin{axis}[
          axis y line* = left,
          legend to name = legA,
          legend style = {draw=none,
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,0) (1,1)};
          legend{Graph 1};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legB,
          legend style = {draw=none,
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,1) (1,0)};
          addplot coordinates {(0,0.5) (1,0.5)};
          legend{Graph 1, Graph 2};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legC,
          legend style = {draw=none,
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          pgfplotsset%
          {%
          every outer y axis line/.style = {xshift=2cm},
          every tick/.style = {xshift=2cm},
          every y tick label/.style = {xshift=2cm},
          }%
          addplot coordinates {(0.5,0) (0.5,1)};
          legend{Graph 4};
          end{axis}%
          end{scope}
          node[below=0.5em of plots.south] (legB) {ref{legB}};
          node[left=0.2em of legB] (legA) {ref{legA}};
          node[right=0.2em of legB] (legC) {ref{legC}};
          draw (legA.south west) rectangle (legC.north east);
          end{tikzpicture}
          end{document}


          enter image description here






          share|improve this answer


























          • Thanks a lot! But would it be possible to add all legend items into one single legend? So that it looks "the classic way" with all items within one single border?

            – Dave
            Mar 19 at 15:06











          • @Dave Yes. I added one way of doing that. (Of course, in the present plot you could just draw all plots in one axis and just build up one legend. I am assuming that your real life example has plots with different coordinate systems combined.)

            – marmot
            Mar 19 at 15:11













          • Ah, thanks a lot for your great help! :-) I just posted a new screenshot to explain it better... Would it be possible to add the legends in two rows? Because right now the command legend columns = 3 does not work anymore. I think this command is not usable anymore with your solution, right?

            – Dave
            Mar 19 at 15:13













          • Yes exactly, in the real coordinate system I have different axis scalings, e.g. 0 - 100 for the first axis and 0 - 20 for the second one etc.

            – Dave
            Mar 19 at 15:14
















          4












          4








          4







          You could use legend to name for that.



          documentclass[border=5mm]{standalone}
          usepackage{pgfplots}
          usetikzlibrary{positioning}
          pgfplotsset{compat=newest, scale only axis}
          begin{document}

          begin{tikzpicture}
          begin{scope}[local bounding box=plots]
          begin{axis}[
          axis y line* = left,
          legend to name = legA,
          legend style = {
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,0) (1,1)};
          legend{Graph 1};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legB,
          legend style = {
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,1) (1,0)};
          addplot coordinates {(0,0.5) (1,0.5)};
          legend{Graph 1, Graph 2};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legC,
          legend style = {
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          pgfplotsset%
          {%
          every outer y axis line/.style = {xshift=2cm},
          every tick/.style = {xshift=2cm},
          every y tick label/.style = {xshift=2cm},
          }%
          addplot coordinates {(0.5,0) (0.5,1)};
          legend{Graph 4};
          end{axis}%
          end{scope}
          node[below=0.5em of plots.south] (legB) {ref{legB}};
          node[left=1em of legB] (legA) {ref{legA}};
          node[right=1em of legB] (legC) {ref{legC}};
          end{tikzpicture}

          end{document}


          enter image description here



          All legends in one box:



          documentclass[border=5mm]{standalone}
          usepackage{pgfplots}
          usetikzlibrary{positioning}
          pgfplotsset{compat=newest, scale only axis}
          begin{document}

          begin{tikzpicture}
          begin{scope}[local bounding box=plots]
          begin{axis}[
          axis y line* = left,
          legend to name = legA,
          legend style = {draw=none,
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,0) (1,1)};
          legend{Graph 1};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legB,
          legend style = {draw=none,
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,1) (1,0)};
          addplot coordinates {(0,0.5) (1,0.5)};
          legend{Graph 1, Graph 2};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legC,
          legend style = {draw=none,
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          pgfplotsset%
          {%
          every outer y axis line/.style = {xshift=2cm},
          every tick/.style = {xshift=2cm},
          every y tick label/.style = {xshift=2cm},
          }%
          addplot coordinates {(0.5,0) (0.5,1)};
          legend{Graph 4};
          end{axis}%
          end{scope}
          node[below=0.5em of plots.south] (legB) {ref{legB}};
          node[left=0.2em of legB] (legA) {ref{legA}};
          node[right=0.2em of legB] (legC) {ref{legC}};
          draw (legA.south west) rectangle (legC.north east);
          end{tikzpicture}
          end{document}


          enter image description here






          share|improve this answer















          You could use legend to name for that.



          documentclass[border=5mm]{standalone}
          usepackage{pgfplots}
          usetikzlibrary{positioning}
          pgfplotsset{compat=newest, scale only axis}
          begin{document}

          begin{tikzpicture}
          begin{scope}[local bounding box=plots]
          begin{axis}[
          axis y line* = left,
          legend to name = legA,
          legend style = {
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,0) (1,1)};
          legend{Graph 1};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legB,
          legend style = {
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,1) (1,0)};
          addplot coordinates {(0,0.5) (1,0.5)};
          legend{Graph 1, Graph 2};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legC,
          legend style = {
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          pgfplotsset%
          {%
          every outer y axis line/.style = {xshift=2cm},
          every tick/.style = {xshift=2cm},
          every y tick label/.style = {xshift=2cm},
          }%
          addplot coordinates {(0.5,0) (0.5,1)};
          legend{Graph 4};
          end{axis}%
          end{scope}
          node[below=0.5em of plots.south] (legB) {ref{legB}};
          node[left=1em of legB] (legA) {ref{legA}};
          node[right=1em of legB] (legC) {ref{legC}};
          end{tikzpicture}

          end{document}


          enter image description here



          All legends in one box:



          documentclass[border=5mm]{standalone}
          usepackage{pgfplots}
          usetikzlibrary{positioning}
          pgfplotsset{compat=newest, scale only axis}
          begin{document}

          begin{tikzpicture}
          begin{scope}[local bounding box=plots]
          begin{axis}[
          axis y line* = left,
          legend to name = legA,
          legend style = {draw=none,
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,0) (1,1)};
          legend{Graph 1};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legB,
          legend style = {draw=none,
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          addplot coordinates {(0,1) (1,0)};
          addplot coordinates {(0,0.5) (1,0.5)};
          legend{Graph 1, Graph 2};
          end{axis}%
          %
          begin{axis}[
          axis x line = none,
          axis y line* = right,
          legend to name = legC,
          legend style = {draw=none,
          legend columns = 3,
          /tikz/every even column/.append style = {column sep=0.5cm},
          /tikz/every odd column/.append style = {column sep=0.15cm},
          },
          ]%
          pgfplotsset%
          {%
          every outer y axis line/.style = {xshift=2cm},
          every tick/.style = {xshift=2cm},
          every y tick label/.style = {xshift=2cm},
          }%
          addplot coordinates {(0.5,0) (0.5,1)};
          legend{Graph 4};
          end{axis}%
          end{scope}
          node[below=0.5em of plots.south] (legB) {ref{legB}};
          node[left=0.2em of legB] (legA) {ref{legA}};
          node[right=0.2em of legB] (legC) {ref{legC}};
          draw (legA.south west) rectangle (legC.north east);
          end{tikzpicture}
          end{document}


          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 19 at 15:10

























          answered Mar 19 at 14:37









          marmotmarmot

          113k5144273




          113k5144273













          • Thanks a lot! But would it be possible to add all legend items into one single legend? So that it looks "the classic way" with all items within one single border?

            – Dave
            Mar 19 at 15:06











          • @Dave Yes. I added one way of doing that. (Of course, in the present plot you could just draw all plots in one axis and just build up one legend. I am assuming that your real life example has plots with different coordinate systems combined.)

            – marmot
            Mar 19 at 15:11













          • Ah, thanks a lot for your great help! :-) I just posted a new screenshot to explain it better... Would it be possible to add the legends in two rows? Because right now the command legend columns = 3 does not work anymore. I think this command is not usable anymore with your solution, right?

            – Dave
            Mar 19 at 15:13













          • Yes exactly, in the real coordinate system I have different axis scalings, e.g. 0 - 100 for the first axis and 0 - 20 for the second one etc.

            – Dave
            Mar 19 at 15:14





















          • Thanks a lot! But would it be possible to add all legend items into one single legend? So that it looks "the classic way" with all items within one single border?

            – Dave
            Mar 19 at 15:06











          • @Dave Yes. I added one way of doing that. (Of course, in the present plot you could just draw all plots in one axis and just build up one legend. I am assuming that your real life example has plots with different coordinate systems combined.)

            – marmot
            Mar 19 at 15:11













          • Ah, thanks a lot for your great help! :-) I just posted a new screenshot to explain it better... Would it be possible to add the legends in two rows? Because right now the command legend columns = 3 does not work anymore. I think this command is not usable anymore with your solution, right?

            – Dave
            Mar 19 at 15:13













          • Yes exactly, in the real coordinate system I have different axis scalings, e.g. 0 - 100 for the first axis and 0 - 20 for the second one etc.

            – Dave
            Mar 19 at 15:14



















          Thanks a lot! But would it be possible to add all legend items into one single legend? So that it looks "the classic way" with all items within one single border?

          – Dave
          Mar 19 at 15:06





          Thanks a lot! But would it be possible to add all legend items into one single legend? So that it looks "the classic way" with all items within one single border?

          – Dave
          Mar 19 at 15:06













          @Dave Yes. I added one way of doing that. (Of course, in the present plot you could just draw all plots in one axis and just build up one legend. I am assuming that your real life example has plots with different coordinate systems combined.)

          – marmot
          Mar 19 at 15:11







          @Dave Yes. I added one way of doing that. (Of course, in the present plot you could just draw all plots in one axis and just build up one legend. I am assuming that your real life example has plots with different coordinate systems combined.)

          – marmot
          Mar 19 at 15:11















          Ah, thanks a lot for your great help! :-) I just posted a new screenshot to explain it better... Would it be possible to add the legends in two rows? Because right now the command legend columns = 3 does not work anymore. I think this command is not usable anymore with your solution, right?

          – Dave
          Mar 19 at 15:13







          Ah, thanks a lot for your great help! :-) I just posted a new screenshot to explain it better... Would it be possible to add the legends in two rows? Because right now the command legend columns = 3 does not work anymore. I think this command is not usable anymore with your solution, right?

          – Dave
          Mar 19 at 15:13















          Yes exactly, in the real coordinate system I have different axis scalings, e.g. 0 - 100 for the first axis and 0 - 20 for the second one etc.

          – Dave
          Mar 19 at 15:14







          Yes exactly, in the real coordinate system I have different axis scalings, e.g. 0 - 100 for the first axis and 0 - 20 for the second one etc.

          – Dave
          Mar 19 at 15:14




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f480282%2fpgfplots-how-to-align-legends-of-multigraph-diagram%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?