Seite 1 von 2

pgfplots Beschriftung einzelner Datenpunkte

Verfasst: Mi 11. Sep 2013, 13:09
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

Verfasst: Mi 11. Sep 2013, 13:35
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

Verfasst: Mi 11. Sep 2013, 14:54
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

Verfasst: Mi 11. Sep 2013, 15:13
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?

Verfasst: Mi 11. Sep 2013, 16:04
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

Verfasst: So 15. Sep 2013, 20:47
von monchi
Besten Dank!

Verfasst: So 15. Sep 2013, 20:59
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?

Verfasst: So 15. Sep 2013, 21:24
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

Verfasst: Di 17. Sep 2013, 18:46
von monchi
funktioniert 1a!

Danke dir!

Verfasst: Mi 25. Sep 2013, 15:47
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}