Legende eines Diagramms verschieben

Tabellen und Grafiken erstellen und anordnen


Gast

Legende eines Diagramms verschieben

Beitrag von Gast »

Hallo

Ich wollte die Legende meines Diagramms verschieben, da sonst nicht alle Linien zu sehen sind oder das Diagramm zu gross wird.
Ich habe es mit
legend style={anchor=north west}


versucht...

Wie kann ich sie nach links oben verschieben?
\documentclass[fontsize=12pt]{scrreprt}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest} 
\usetikzlibrary{arrows.meta}
\definecolor{myorange}{rgb}{1,0.3,0}  
\usepackage[ngerman]{babel} 
\usepackage[utf8]{inputenc} 
\usepackage{blindtext}
\usepackage{showframe}

\begin{document} 
\blindtext

   \begin{center}
\begin{minipage}{\linewidth}
\begin{tikzpicture} 

\pgfplotstableread{ 
  Jahr        a      b		c	d	e	f		g	h	i	j	k	l 
{2011}      36		8		2	28	10	2		1	13	0	0	29	26		
{2012}  	37		16		2	37	9	2		1	11	0	0  	26	32	
{2013}      39		15		0	34	8	3		5	13	0	0	25	27	
{2014}     	35		15		4	35	8	3		6	11	2	0	22	28	
{2015}     	33		19		4	43	8	2		10	10	3	0	18	31	
{2016}		32		16		6	47	9	4		11	10	5	0	22	42	
{2017}		33		15		8	56	7	4		12	11	7	2	26	41		
}{\data}

\begin{axis}[ 
  width=.95\textwidth, 
  height=20cm,
  axis lines=left, 
  ylabel={Anzahl}, 
  ymin=0,ymax=70, 
  legend style={anchor=north west}
  grid=major,
  xlabel={Jahr}, 
  xtick=data,% <- ergänzt
  xticklabel style={/pgf/number format/1000 sep=}
] 
\addplot [myorange, mark=*] table [x=Jahr, y=a] {\data};
\addlegendentry{a}
\addplot [black, mark=*] table [x=Jahr, y=b] {\data};
\addlegendentry{b}
\addplot [violet, mark=*] table [x=Jahr, y=c] {\data};
\addlegendentry{c}
\addplot [teal, mark=*] table [x=Jahr, y=d] {\data};
\addlegendentry{d}
\addplot [purple, mark=*] table [x=Jahr, y=e] {\data};
\addlegendentry{e}
\addplot [blue, mark=*] table [x=Jahr, y=f] {\data};
\addlegendentry{f}
\addplot [olive, mark=*] table [x=Jahr, y=g] {\data};
\addlegendentry{g}
\addplot [lime, mark=*] table [x=Jahr, y=h] {\data};
\addlegendentry{h}
\addplot [brown, mark=*] table [x=Jahr, y=i] {\data};
\addlegendentry{i}
\addplot [magenta, mark=*] table [x=Jahr, y=j] {\data};
\addlegendentry{j}
\addplot [cyan, mark=*] table [x=Jahr, y=k] {\data};
\addlegendentry{k}
\addplot [gray, mark=*] table [x=Jahr, y=l] {\data};
\addlegendentry{l}
\end{axis} 
\end{tikzpicture} 
\captionof{figure}{Text.}
\end{minipage}
\end{center}

\blindtext
\end{document}

Bartman
Forum-Meister
Forum-Meister
Beiträge: 2456
Registriert: Do 16. Jul 2009, 21:41
Wohnort: Hessische Provinz

Beitrag von Bartman »

Bei legend style fehlt am Ende das Komma.

Versuche stattdessen legend pos=north west.
\documentclass[fontsize=12pt]{scrreprt}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest} 
\usetikzlibrary{arrows.meta}
\definecolor{myorange}{rgb}{1,0.3,0}  
\usepackage[ngerman]{babel} 
\usepackage[utf8]{inputenc} 
\usepackage{blindtext}
\usepackage{showframe}

\begin{document} 
\blindtext

\begin{center}
\begin{minipage}{\linewidth}
\begin{tikzpicture} 

\pgfplotstableread{ 
Jahr    a   b   c  d   e   f  g   h   i  j  k   l 
{2011}  36   8  2  28  10  2   1  13  0  0  29  26      
{2012}  37  16  2  37   9  2   1  11  0  0  26  32   
{2013}  39  15  0  34   8  3   5  13  0  0  25  27   
{2014}  35  15  4  35   8  3   6  11  2  0  22  28   
{2015}  33  19  4  43   8  2  10  10  3  0  18  31   
{2016}  32  16  6  47   9  4  11  10  5  0  22  42   
{2017}  33  15  8  56   7  4  12  11  7  2  26  41      
}{\data}

\begin{axis}[ 
  width=.97\textwidth, 
  height=20cm,
  axis lines=left, 
  ylabel={Anzahl}, 
  ymin=0,ymax=70, 
  legend pos=north west,% <- eingefügt
  grid=major,
  xlabel={Jahr}, 
  xtick=data,
  every axis plot/.append style={mark=*},% <- eingefügt
  xticklabel style={/pgf/number format/1000 sep=}
] 
\addplot [myorange] table [y=a] {\data};
\addplot [black]    table [y=b] {\data};
\addplot [violet]   table [y=c] {\data};
\addplot [teal]     table [y=d] {\data};
\addplot [purple]   table [y=e] {\data};
\addplot [blue]     table [y=f] {\data};
\addplot [olive]    table [y=g] {\data};
\addplot [lime]     table [y=h] {\data};
\addplot [brown]    table [y=i] {\data};
\addplot [magenta]  table [y=j] {\data};
\addplot [cyan]     table [y=k] {\data};
\addplot [gray]     table [y=l] {\data};
\legend{a,b,c,d,e,f,g,h,i,j,k,l}
\end{axis} 
\end{tikzpicture} 
\captionof{figure}{Text.}
\end{minipage}
\end{center}

\blindtext
\end{document}

Gast

Beitrag von Gast »

Herzlichen Dank!! :)

Bartman
Forum-Meister
Forum-Meister
Beiträge: 2456
Registriert: Do 16. Jul 2009, 21:41
Wohnort: Hessische Provinz

Beitrag von Bartman »

Der die Zeichnung betreffende Quelltext lässt sich offenbar auch noch etwas schlanker gestalten.
\documentclass[fontsize=12pt]{scrreprt}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest} 
\usetikzlibrary{arrows.meta}
\definecolor{myorange}{rgb}{1,0.3,0}  
\usepackage[ngerman]{babel} 
\usepackage[utf8]{inputenc} 
\usepackage{showframe}

\begin{document} 
\begin{center}
\begin{minipage}{\linewidth}
\begin{tikzpicture} 

\pgfplotstableread{ 
Jahr    a   b   c  d   e   f  g   h   i  j  k   l 
{2011}  36   8  2  28  10  2   1  13  0  0  29  26      
{2012}  37  16  2  37   9  2   1  11  0  0  26  32   
{2013}  39  15  0  34   8  3   5  13  0  0  25  27   
{2014}  35  15  4  35   8  3   6  11  2  0  22  28   
{2015}  33  19  4  43   8  2  10  10  3  0  18  31   
{2016}  32  16  6  47   9  4  11  10  5  0  22  42   
{2017}  33  15  8  56   7  4  12  11  7  2  26  41      
}{\data}

\pgfplotscreateplotcyclelist{color list}{
	myorange,black,violet,teal,purple,blue,olive,lime,brown,magenta,cyan,gray
}

\begin{axis}[ 
  width=.97\textwidth, 
  height=20cm,
  axis lines=left, 
  ylabel={Anzahl}, 
  ymin=0,ymax=70, 
  legend pos=north west,% <- eingefügt
  grid=major,
  xlabel={Jahr}, 
  xtick=data,
  every axis plot/.append style={mark=*},% <- eingefügt
  xticklabel style={/pgf/number format/1000 sep=},
  cycle list name=color list
] 
\foreach \columnindex in {1,...,12} {
	\addplot table [y index=\columnindex] {\data};	
}
\legend{a,b,c,d,e,f,g,h,i,j,k,l}
\end{axis} 
\end{tikzpicture} 
\captionof{figure}{Text.}
\end{minipage}
\end{center}
\end{document}

Antworten