pgfplots Beschriftung einzelner Datenpunkte

Tabellen und Grafiken erstellen und anordnen


monchi
Forum-Anfänger
Forum-Anfänger
Beiträge: 23
Registriert: Fr 21. Dez 2012, 13:24

pgfplots Beschriftung einzelner Datenpunkte

Beitrag von monchi »

Hi,

ich baue mir gerade mit pgfplots eine Diagrammvorlage. Neben den Datenspalten existieren noch zwei zusätzliche Hilfsspalten. In der ersten ("Label") steht entweder 1 oder 0, diese entscheidet ob der Datenpunkt eine Beschriftung erhalten soll. Die zweite Hilfsspalte enthält die Ausrichtung der Beschriftung. Funktioniert auch alles soweit, bis auf das An- bzw. Abschalten der Beschriftung.
\documentclass{scrartcl}
\usepackage{pgfplots,pgfplotstable,filecontents}

\begin{filecontents*}{table.csv}
X,Y,Z,Label,Alng
100,150,2001,1,90
400,390,2002,0,180
600,645,2003,1,90
\end{filecontents*}

\pgfkeys{/pgf/number format/set thousands separator={}}

\begin{document}

	\begin{tikzpicture}[scale=1.4]
			\def\varXmax{700}
			\def\varYmax{\varXmax}
				
			\begin{axis}[
						xlabel={$S_{x,theoretisch} [mm]$},
						ylabel={$S_{x,gemessen} [mm]$},
						xmin={0},
						ymin={0},
						xmax={\varXmax},
						ymax={\varYmax},
						minor tick num=1,
						try min ticks=10,
					]

					\addplot[scatter,only marks,
						visualization depends on={\thisrow{Label} \as \Lblswitch},
						visualization depends on={\thisrow{Alng} \as \Alingmnt},
						visualization depends on={value \thisrow{Z} \as \LblZ},
						nodes near coords={\if{\Lblswitch>0} Beschriftung\ (\LblZ ) \else keine Beschriftung \fi},
						every node near coord/.style={anchor=\Alingmnt}
						] table [x=X,y=Y,col sep=comma] {table.csv};
			
					\addplot [gray, style=dashed, no marks, domain=0:\varXmax]{0.75*x};	% -25%
					\addplot [gray, style=dashed, no marks, domain=0:\varXmax]{x};
					\addplot [gray, style=dashed, no marks, domain=0:\varXmax]{1.25*x};	% +25%		
			
			\end{axis}
	\end{tikzpicture}

\end{document}
Ich denke der Fehler liegt in der If-Abfrage. Hier wird völlig unabhängig davon welchen Wert Lblswitch enthält immer die Else-Anweisung ausgeführt.

Hat jemand eine Idee?

Viele Grüße
Monchi

Epllus
Forum-Guru
Forum-Guru
Beiträge: 470
Registriert: So 23. Sep 2012, 17:31

Beitrag von Epllus »

Wie du richtig vermutest liegt der Fehler an der \if- Abfrage, was \if genau macht, kannst du hier nachlesen.
Du möchtest Zahlenwerte vergleichen, hierzu verwendest du entweder \ifnum oder – mehr oder weniger – \ifdim.
In diesem Fall verwende ich \ifdim, da die Werte als „1.0000000“ und „0.0“ gespeichert werden, und \ifnum keine Dezimahlzahlen vergleichen kann.
Bitte beachte, dass \ifdim nur Längen vergleichen kann, deshalb steht „\Lblswitch pt“ und „0pt“ (achte darauf, dass die Einheiten immer gleich sind!).
\documentclass{scrartcl} 
\usepackage{pgfplots,pgfplotstable,filecontents} 

\begin{filecontents*}{table.csv} 
X,Y,Z,Label,Alng 
100,150,2001,1,90 
400,390,2002,0,180 
600,645,2003,1,90 
\end{filecontents*} 

\pgfkeys{/pgf/number format/set thousands separator={}} 

\begin{document} 

   \begin{tikzpicture}[scale=1.4] 
         \def\varXmax{700} 
         \def\varYmax{\varXmax} 
            
         \begin{axis}[ 
                  xlabel={$S_{x,theoretisch} [mm]$}, 
                  ylabel={$S_{x,gemessen} [mm]$}, 
                  xmin={0}, 
                  ymin={0}, 
                  xmax={\varXmax}, 
                  ymax={\varYmax}, 
                  minor tick num=1, 
                  try min ticks=10, 
               ] 

               \addplot[scatter,only marks, 
                  visualization depends on={\thisrow{Label} \as \Lblswitch}, 
                  visualization depends on={\thisrow{Alng} \as \Alingmnt}, 
                  visualization depends on={value \thisrow{Z} \as \LblZ}, 
                  nodes near coords={\ifdim\Lblswitch pt>0pt Beschriftung\ (\LblZ )\else keine Beschriftung \fi}, 
                  every node near coord/.style={anchor=\Alingmnt} 
                  ] table [x=X,y=Y,col sep=comma] {table.csv}; 
          
               \addplot [gray, style=dashed, no marks, domain=0:\varXmax]{0.75*x};   % -25% 
               \addplot [gray, style=dashed, no marks, domain=0:\varXmax]{x}; 
               \addplot [gray, style=dashed, no marks, domain=0:\varXmax]{1.25*x};   % +25%       
          
         \end{axis} 
   \end{tikzpicture} 

\end{document} 
Hoffe es hilft
Epllus

esdd
Forum-Meister
Forum-Meister
Beiträge: 2561
Registriert: So 7. Feb 2010, 16:36

Beitrag von esdd »

Oder du kannst die ifthenelse Funktion aus pgfmath nutzen, das sowieso von pgfplots geladen wird:
nodes near coords={\pgfmathparse{\Lblswitch > 0 ? "Beschriftung\ (\LblZ )" : "keine Beschriftung"}\pgfmathresult},
Gruß
Elke
Zuletzt geändert von esdd am Mi 11. Sep 2013, 22:15, insgesamt 1-mal geändert.

monchi
Forum-Anfänger
Forum-Anfänger
Beiträge: 23
Registriert: Fr 21. Dez 2012, 13:24

Beitrag von monchi »

Herzlichen Dank! Funktioniert beides 1a!

Ich würde gerne jetzt noch eine Beschriftung "+25%" und "-25%" an die beiden äußeren gestrichelten Linien hinzufügen. Und zwar am oberen rechten Ende der jeweiligen Linie.

Ich würde die vorlage gerne so flexibel halten wie möglich (Brauche sie nachher in verschiedenen Größen). Ich würde daher nur ungern festen Koordinaten verwenden. Geht soetwas überhaupt?

esdd
Forum-Meister
Forum-Meister
Beiträge: 2561
Registriert: So 7. Feb 2010, 16:36

Beitrag von esdd »

Hier ist mal ein Vorschlag mit intersections:
\documentclass{scrartcl} 
\usepackage{pgfplots,pgfplotstable,filecontents}
\pgfplotsset{compat=1.8}
\usetikzlibrary{intersections}

\begin{filecontents*}{table.csv} 
 X,Y,Z,Label,Alng 
 100,150,2001,1,90 
 400,390,2002,0,180 
 600,645,2003,1,90 
\end{filecontents*} 

\pgfkeys{/pgf/number format/set thousands separator={}} 


\begin{document} 

    \begin{tikzpicture}[scale=1.4]
          \def\varXmax{700} 
          \def\varYmax{\varXmax} 
             
          \begin{axis}[ 
                   xlabel={$S_{x,theoretisch} [mm]$}, 
                   ylabel={$S_{x,gemessen} [mm]$}, 
                   xmin={0}, 
                   ymin={0}, 
                   xmax={\varXmax}, 
                   ymax={\varYmax}, 
                   minor tick num=1, 
                   try min ticks=10,
                ] 

                \addplot[scatter,only marks, 
                   visualization depends on={\thisrow{Label} \as \Lblswitch}, 
                   visualization depends on={\thisrow{Alng} \as \Alingmnt}, 
                   visualization depends on={value \thisrow{Z} \as \LblZ},
                   nodes near coords={\pgfmathparse{\Lblswitch > 0 ? "Beschriftung\ (\LblZ )" : "keine Beschriftung"}\pgfmathresult},
                   every node near coord/.style={anchor=\Alingmnt} 
                   ] table [x=X,y=Y,col sep=comma] {table.csv}; 
           
                \addplot [gray, style=dashed, no marks, domain=0:\varXmax,name path global=pe1]{0.75*x};   % -25% 
                \addplot [gray, style=dashed, no marks, domain=0:\varXmax]{x}; 
                \addplot [gray, style=dashed, no marks, domain=0:\varXmax,name path global=pe3]{1.25*x};   % +25%       
                \path[red,name path global=axis1](rel axis cs:0,0)rectangle(rel axis cs:1,1);
          \end{axis} 
          \node[name intersections={of=axis1 and pe1}, right] at (intersection-2){-25\%};
          \node[name intersections={of=axis1 and pe3}, above] at (intersection-2){+25\%};
    \end{tikzpicture} 

\end{document}
Gruß
Elke

monchi
Forum-Anfänger
Forum-Anfänger
Beiträge: 23
Registriert: Fr 21. Dez 2012, 13:24

Beitrag von monchi »

Besten Dank!

Gast123

Beitrag von Gast123 »

die "Beschriftungen der Intersections" wird leider nicht automatisch mit skaliert. Kann man die Schriftgröße irgendwie automatische auf die gleiche Größe wie die Achsenbeschriftung einstellen?

esdd
Forum-Meister
Forum-Meister
Beiträge: 2561
Registriert: So 7. Feb 2010, 16:36

Beitrag von esdd »

Das geht mit der Option transform shape. Ergänze diese bei den einzelnen Noden:
\node[name intersections={of=axis1 and pe1}, right, transform shape] at (intersection-2){-25\%};
\node[name intersections={of=axis1 and pe3}, above, transform shape] at (intersection-2){+25\%}; 
oder, wenn das für alle Noden gelten soll, geht auch
\begin{tikzpicture}[scale=1.4, transform shape]
Gruß
Elke

monchi
Forum-Anfänger
Forum-Anfänger
Beiträge: 23
Registriert: Fr 21. Dez 2012, 13:24

Beitrag von monchi »

funktioniert 1a!

Danke dir!

monchi
Forum-Anfänger
Forum-Anfänger
Beiträge: 23
Registriert: Fr 21. Dez 2012, 13:24

Beitrag von monchi »

Funktioniert leider doch nicht 100%ig.
So lange ich nur ein Diagramm mit der Extrabeschriftung verwende ist alles gut. Sobald ich aber im Dokument mehrere habe ist die Beschriftung nur noch bei dem ersten korrerkt. Alle weiteren haben die "+25%" ca. am Ursprung des Diagramms stehen. Jmand eine idee?
\documentclass{scrartcl}
\usepackage{pgfplots,pgfplotstable,filecontents}
\pgfplotsset{compat=1.8}
\usetikzlibrary{intersections}

\begin{filecontents*}{table.csv}
 X,Y,Z,Label,Alng
 100,150,2001,0,90
 400,390,2002,0,180
 600,645,2003,0,90
\end{filecontents*}

\pgfkeys{/pgf/number format/set thousands separator={}}


\begin{document}

    \begin{tikzpicture}[scale=1.4, transform shape]
          \def\varXmax{700}
          \def\varYmax{\varXmax}
             
          \begin{axis}[
                   xlabel={$S_{x,theoretisch} [mm]$},
                   ylabel={$S_{x,gemessen} [mm]$},
                   xmin={0},
                   ymin={0},
                   xmax={\varXmax},
                   ymax={\varYmax},
                   minor tick num=1,
                   try min ticks=10,
                ]

                \addplot[scatter,only marks,
                   visualization depends on={\thisrow{Label} \as \Lblswitch},
                   visualization depends on={\thisrow{Alng} \as \Alingmnt},
                   visualization depends on={value \thisrow{Z} \as \LblZ},
                   nodes near coords={\pgfmathparse{\Lblswitch > 0 ? "Beschriftung\ (\LblZ )" : ""}\pgfmathresult},
                   every node near coord/.style={anchor=\Alingmnt}
                   ] table [x=X,y=Y,col sep=comma] {table.csv};
           
                \addplot [gray, style=dashed, no marks, domain=0:\varXmax,name path global=pe1]{0.75*x};   % -25%
                \addplot [gray, style=dashed, no marks, domain=0:\varXmax]{x};
                \addplot [gray, style=dashed, no marks, domain=0:\varXmax,name path global=pe3]{1.25*x};   % +25%      
                \path[red,name path global=axis1](rel axis cs:0,0)rectangle(rel axis cs:1,1);
          \end{axis}
          \node[name intersections={of=axis1 and pe1}, right] at (intersection-2){-25\%};
          \node[name intersections={of=axis1 and pe3}, above] at (intersection-2){+25\%};
    \end{tikzpicture}
		
		    
		\begin{tikzpicture}[scale=1.4, transform shape]
        \def\varXmax{700}
        \def\varYmax{\varXmax}
             
          \begin{axis}[
                   xlabel={$S_{x,theoretisch} [mm]$},
                   ylabel={$S_{x,gemessen} [mm]$},
                   xmin={0},
                   ymin={0},
                   xmax={\varXmax},
                   ymax={\varYmax},
                   minor tick num=1,
                   try min ticks=10,
                ]

                \addplot[scatter,only marks,
                   visualization depends on={\thisrow{Label} \as \Lblswitch},
                   visualization depends on={\thisrow{Alng} \as \Alingmnt},
                   visualization depends on={value \thisrow{Z} \as \LblZ},
                   nodes near coords={\pgfmathparse{\Lblswitch > 0 ? "Beschriftung\ (\LblZ )" : ""}\pgfmathresult},
                   every node near coord/.style={anchor=\Alingmnt}
                   ] table [x=X,y=Y,col sep=comma] {table.csv};
           
                \addplot [gray, style=dashed, no marks, domain=0:\varXmax,name path global=pe1]{0.75*x};   % -25%
                \addplot [gray, style=dashed, no marks, domain=0:\varXmax]{x};
                \addplot [gray, style=dashed, no marks, domain=0:\varXmax,name path global=pe3]{1.25*x};   % +25%      
                \path[red,name path global=axis1](rel axis cs:0,0)rectangle(rel axis cs:1,1);
          \end{axis}
          \node[name intersections={of=axis1 and pe1}, right] at (intersection-2){-25\%};
          \node[name intersections={of=axis1 and pe3}, above] at (intersection-2){+25\%};
    \end{tikzpicture}
\end{document}

Antworten