2

I have two graphes, one horizontal (y1) and one vertical (y2) line. For a better identification in the legend, I want the vertical graph (y2) also to be represented by a vertical line in the legend. In other words: left to y2 should be a vertical dashed line (rotation of 90°), while the y1 legend entry stays as it is.

Is there any way to customize the appearance of the legend entry symbols like that? Thank you very much for your help!

the handdrawn is how the legend entry should look like (maybe a bit more professional)

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0,
xmax=5,
ymin=0,
ymax=5,
axis background/.style={fill=white},
legend entries={y1,y2},
]
\addplot [color=black, dashdotted]
  table[row sep=crcr]{%
0   3\\
5   3\\
};
\addplot [color=black, dashed]
  table[row sep=crcr]{%
0   -100\\
2   100\\
};
\end{axis}
\end{tikzpicture}
\end{document}

2 Answers 2

2

Welcome! You can create whatever legend style you want using legend image code. I created a style called vline legend that can be added to the options of the plot to make the corresponding legend entry vertical.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16,
    /pgfplots/vline legend/.style={
        legend image code/.code={
            \draw [mark repeat=2,mark phase=2,##1]
             plot coordinates {
            (0.3cm,-0.3cm) (0.3cm,0cm)    (0.3cm,0.3cm)
            };
}, },
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0,
xmax=5,
ymin=0,
ymax=5,
axis background/.style={fill=white},
legend entries={y1,y2},
]
\addplot [color=black, dashdotted]
  table[row sep=crcr]{%
0   3\\
5   3\\
};
\addplot [color=black, dashed,vline legend]
  table[row sep=crcr]{%
0   -100\\
2   100\\
};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

1
  • Thank you very much for the quick response, exactly what I needed!
    – Sral
    Oct 9, 2019 at 15:50
1

Another way in which the legend entry can be rotated.

legend image post style={rotate=90,xshift=-0.3cm, yshift=-0.3cm}

enter image description here

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0,
xmax=5,
ymin=0,
ymax=5,
axis background/.style={fill=white},
legend entries={y1,y2},
]
\addplot [color=black, dashdotted]
  table[row sep=crcr]{%
0   3\\
5   3\\
};
\addplot [color=black, dashed,legend image post style={rotate=90, yshift=-0.3cm} ]
  table[row sep=crcr]{%
0   -100\\
2   100\\
};
\end{axis}
\end{tikzpicture}
\end{document}

PS: @Schrödinger's cat's solution is better.

2
  • Try legend image post style={rotate=90,xshift=-0.3cm, yshift=-0.3cm}.
    – user194703
    Oct 9, 2019 at 16:58
  • @Schrödinger'scat that looks better.
    – nidhin
    Oct 9, 2019 at 17:11

You must log in to answer this question.

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