pgfplots: How to align legends of multigraph diagram?
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:
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
add a comment |
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:
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
add a comment |
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:
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
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:
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
pgfplots horizontal-alignment vertical-alignment positioning legend
edited Mar 19 at 16:24
Dave
asked Mar 19 at 14:13
DaveDave
949619
949619
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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}
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}
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 commandlegend 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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}
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}
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 commandlegend 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
add a comment |
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}
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}
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 commandlegend 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
add a comment |
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}
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}
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}
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}
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 commandlegend 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
add a comment |
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 commandlegend 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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