2

In the image below I have outlined the legend in red. I don't like this legend, because the pattern is in the mark box left of the text, however the color isn't in the box.

Legend with 4 tables

The code for the legend:

\documentclass[border=5pt,12pt]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.16}
\usepackage{pgfplotstable}
\usepackage{xcolor}
\definecolor{ElmerGreen}{RGB}{77,175,74}
\definecolor{ValserRed}{RGB}{228,26,28}
\definecolor{LBlue}{RGB}{55,126,184}
\begin{document}
\pgfkeys{/pgf/number format/.cd,1000 sep={\,}}
\begin{tikzpicture}
  \centering
  \begin{axis}[
   hide axis,
   xmin=10,
   xmax=50,
    ymin=0,
ymax=0.4,
    legend style={draw=black,legend cell align=center,mark size=6pt},
]
\addlegendimage{only marks, mark=square*, mark options={pattern=crosshatch dots}}
    \addlegendentry[ElmerGreen]{\textbf{Elmer}};
\addlegendimage{only marks, mark=square*, mark options={pattern=grid}}
    \addlegendentry[ValserRed]{\textbf{Valser}};
\addlegendimage{only marks, mark=square*, mark options={pattern=north east lines}}
    \addlegendentry[LBlue] {\textbf{Leitungswasser}};    
\end{axis}
  \end{tikzpicture}
\end{document}

The code for the 4 pgfplot graphs isn't important. I used the package pgfplots to generate them and subfig to align them on a landscape A4, with the legend 1\textwidth.

I want the legend to be in regular text, i.e. 12pt and the pattern and the color of the corresponding data to be the same as the graphs.

Any other tips concerning the graphs are much appreciated.

Torbjørn T.
  • 206,688
Max R
  • 308
  • Welcome to TeX.SE! When you are saying regular text, you still want to keep it boldface? I also find that your code does not compile on my machine. I guess you need to add some plots. –  Jan 14 '19 at 17:17
  • Hi @marmot, when I mean regular text, I don't mean boldface. In the original legend it was bold in order to increase legibility of the colored text. Correct, I am using a structure system, where in the folder, every plot is made and the pdf is then loaded, along with the legend, into the main .tex which then includes the pdfs with \includegraphics[]{name.pdf} and the subfig - package. – Max R Jan 14 '19 at 18:40

2 Answers2

3

Or

\documentclass[border=5pt,12pt]{standalone}
\usepackage{xcolor}
\definecolor{ElmerGreen}{RGB}{77,175,74}
\definecolor{ValserRed}{RGB}{228,26,28}
\definecolor{LBlue}{RGB}{55,126,184}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.16}
\pgfplotsset{my legend/.style args={#1 and pattern #2}{
    legend image code/.code={
        \draw [#1] (0cm,-0.2cm) rectangle (0.6cm,0.4cm);
        \path [pattern=#2] (0cm,-0.2cm) rectangle (0.6cm,0.4cm);
    },
}}
\begin{document}
\pgfkeys{/pgf/number format/.cd,1000 sep={\,}}
\centering
\begin{tikzpicture}
  \begin{axis}[
   hide axis,
   xmin=10,
   xmax=50,
    ymin=0,
ymax=0.4,
    legend style={draw=black,legend cell align=center,mark size=6pt}]
\addlegendimage{my legend={{fill=ElmerGreen} and pattern {crosshatch dots}}}
    \addlegendentry[ElmerGreen]{\textbf{Elmer}};
\addlegendimage{my legend={{fill=ValserRed} and pattern {grid}}}
    \addlegendentry[ValserRed]{\textbf{Valser}};
\addlegendimage{my legend={{fill=LBlue} and pattern {north east lines}}}
    \addlegendentry[LBlue]{\textbf{Leitungswasser}};    
 \end{axis}
  \end{tikzpicture}
\end{document}

enter image description here

Or without boldface (and with the left alignment from Torbjørn's nice answer which I hope to be restored).

\documentclass[border=5pt,12pt]{standalone}
\usepackage{xcolor}
\definecolor{ElmerGreen}{RGB}{77,175,74}
\definecolor{ValserRed}{RGB}{228,26,28}
\definecolor{LBlue}{RGB}{55,126,184}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.16}
\pgfplotsset{my legend/.style args={#1 and pattern #2}{
    legend image code/.code={
        \draw [#1] (0cm,-0.2cm) rectangle (0.6cm,0.4cm);
        \path [pattern=#2] (0cm,-0.2cm) rectangle (0.6cm,0.4cm);
    },
}}
\begin{document}
\pgfkeys{/pgf/number format/.cd,1000 sep={\,}}
\centering
\begin{tikzpicture}
  \begin{axis}[
   hide axis,
   xmin=10,
   xmax=50,
    ymin=0,
ymax=0.4,
    legend style={draw=black,legend cell align=center,mark size=6pt,legend cell align=left}]
\addlegendimage{my legend={{fill=ElmerGreen} and pattern {crosshatch dots}}}
    \addlegendentry[ElmerGreen]{Elmer};
\addlegendimage{my legend={{fill=ValserRed} and pattern {grid}}}
    \addlegendentry[ValserRed]{Valser};
\addlegendimage{my legend={{fill=LBlue} and pattern {north east lines}}}
    \addlegendentry[LBlue]{Leitungswasser};    
 \end{axis}
  \end{tikzpicture}
\end{document}

enter image description here

  • Thank you very much! This is exactly what I wanted. – Max R Jan 15 '19 at 06:05
  • @MaxR Glad to hear! If you like an answer to your question, you can accept it by clicking on the check mark left of it. I think you should accept Torbjørn's nice answer, which is more elegant than mine. –  Jan 15 '19 at 06:08
1

With

legend image code/.code={
        \draw [#1] (0cm,-0.2cm) rectangle (0.5cm,0.3cm);
    },
legend cell align=left % remove if you don't like it

so the legend images becomes a square and entries left aligned, and

\addlegendimage{pattern=crosshatch dots,preaction={fill=ElmerGreen}}

etc., you get

enter image description here

I would perhaps consider adding legend columns=-1 as well, which will place all the legend entries in a single row.

Complete code:

\documentclass[border=5pt,12pt]{standalone}
\usepackage{pgfplotstable} % loads pgfplots which loads tikz which loads xcolor
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.16}
\definecolor{ElmerGreen}{RGB}{77,175,74}
\definecolor{ValserRed}{RGB}{228,26,28}
\definecolor{LBlue}{RGB}{55,126,184}
\begin{document}
\pgfkeys{/pgf/number format/.cd,1000 sep={\,}}
\begin{tikzpicture}
  \centering
  \begin{axis}[
   hide axis,
   xmin=10,
   xmax=50,
    ymin=0,
ymax=0.4,
    legend style={draw=black,legend cell align=center,mark size=6pt},
    legend image code/.code={
        \draw [#1] (0cm,-0.2cm) rectangle (0.5cm,0.3cm);
    },
    legend cell align=left,
    % legend columns=-1 % consider using this
]
\addlegendimage{pattern=crosshatch dots,preaction={fill=ElmerGreen}}
    \addlegendentry[ElmerGreen]{Elmer};
\addlegendimage{pattern=grid,preaction={fill=ValserRed}}
    \addlegendentry[ValserRed]{Valser};
\addlegendimage{pattern=north east lines,preaction={fill=LBlue}}
    \addlegendentry[LBlue] {Leitungswasser};    
\end{axis}
  \end{tikzpicture}
\end{document}
Torbjørn T.
  • 206,688