Seite 1 von 1

Tikzpicture - CSV einlesen und als Nodes-Labels ausgeben

Verfasst: Mi 18. Jan 2017, 07:22
von Kappa
Hallo zusammen,

ich habe mir mit tikzpicture eine Matrix erstellt (s.u.). Ich stelle eine 10x10 Matrix dar und möchte das auch so mit nodes belassen. Anstatt jeden Node einzeln anzugeben möchte ich sie einlesen, ebenfalls im Minimalbeispiel.

Allerdings ändert sich durch die Verwendung von axis meine Größeneinheit...In meiner .csv-Datei stehen (i,j,val) Werte, die automatisch in
\node at(i,j){val}
generiert werden sollen.

Mein Skalierungsproblem mit axis sieht wie folgt aus:
\documentclass[12pt,a4paper,oneside,listof=totoc,bibliography=totoc,BCOR=4mm,DIV=12,]{scrartcl}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

So sieht meine Matrix aus: \\[5mm]
\begin{tikzpicture}
\node at (0.2,0.2) {0};\node at (0.2,0.6) {1};\node at (0.2,1) {1};\node at (0.2,1.4) {1};\node at (0.2,1.8) {1};\node at (0.2,2.2) {1};\node at (0.2,2.6) {1};\node at (0.2,3) {0};\node at (0.2,3.4) {1};\node at (0.2,3.8) {1};
\node at (0.6,0.2) {1};\node at (0.6,0.6) {1};\node at (0.6,1) {1};\node at (0.6,1.4) {1};\node at (0.6,1.8) {1};\node at (0.6,2.2) {1};\node at (0.6,2.6) {1};\node at (0.6,3) {0};\node at (0.6,3.4) {1};\node at (0.6,3.8) {1};
\node at (1,0.2) {1};\node at (1,0.6) {1};\node at (1,1) {1};\node at (1,1.4) {1};\node at (1,1.8) {1};\node at (1,2.2) {1};\node at (1,2.6) {1};\node at (1,3) {1};\node at (1,3.4) {1};\node at (1,3.8) {1};
\node at (1.4,0.2) {1};\node at (1.4,0.6) {1};\node at (1.4,1) {1};\node at (1.4,1.4) {1};\node at (1.4,1.8) {1};\node at (1.4,2.2) {1};\node at (1.4,2.6) {1};\node at (1.4,3) {1};\node at (1.4,3.4) {1};\node at (1.4,3.8) {1};
\node at (1.8,0.2) {1};\node at (1.8,0.6) {0};\node at (1.8,1) {0};\node at (1.8,1.4) {1};\node at (1.8,1.8) {1};\node at (1.8,2.2) {1};\node at (1.8,2.6) {1};\node at (1.8,3) {1};\node at (1.8,3.4) {1};\node at (1.8,3.8) {1};
\node at (2.2,0.2) {1};\node at (2.2,0.6) {1};\node at (2.2,1) {1};\node at (2.2,1.4) {1};\node at (2.2,1.8) {1};\node at (2.2,2.2) {0};\node at (2.2,2.6) {1};\node at (2.2,3) {1};\node at (2.2,3.4) {1};\node at (2.2,3.8) {1};
\node at (2.6,0.2) {1};\node at (2.6,0.6) {1};\node at (2.6,1) {1};\node at (2.6,1.4) {1};\node at (2.6,1.8) {1};\node at (2.6,2.2) {0};\node at (2.6,2.6) {0};\node at (2.6,3) {1};\node at (2.6,3.4) {1};\node at (2.6,3.8) {1};
\node at (3,0.2) {1};\node at (3,0.6) {1};\node at (3,1) {1};\node at (3,1.4) {1};\node at (3,1.8) {1};\node at (3,2.2) {0};\node at (3,2.6) {1};\node at (3,3) {1};\node at (3,3.4) {1};\node at (3,3.8) {1};
\node at (3.4,0.2) {1};\node at (3.4,0.6) {1};\node at (3.4,1) {1};\node at (3.4,1.4) {1};\node at (3.4,1.8) {1};\node at (3.4,2.2) {0};\node at (3.4,2.6) {1};\node at (3.4,3) {1};\node at (3.4,3.4) {1};\node at (3.4,3.8) {0};
\node at (3.8,0.2) {1};\node at (3.8,0.6) {1};\node at (3.8,1) {1};\node at (3.8,1.4) {1};\node at (3.8,1.8) {1};\node at (3.8,2.2) {1};\node at (3.8,2.6) {1};\node at (3.8,3) {1};\node at (3.8,3.4) {0};\node at (3.8,3.8) {0};		
			\draw (0.1,0)--(0,0)--(0,4)--(0.1,4);
			\draw (3.9,0)--(4,0)--(4,4)--(3.9,4);
\end{tikzpicture}

So war mein Versuch sie einzulesen: \\[5mm]
\begin{tikzpicture}
   \begin{axis}[
	ticks=none,
	axis x line=none,
	axis y line=none,
    ybar,
    xtick=0,
    ymin=0,
	ymax =4,
	xmax = 4,
	xmin = 0,
	width = 4cm,
	height = 4cm,
]

       \addplot+[
            only marks,
            visualization depends on=\thisrow{val} \as \val,
            nodes near coords=\pgfmathprintnumber{\val},
            every node near coord/.append style={
                black,
                inner sep=1pt,
                yshift=0ex
            }
            ] table [col sep=comma] {matrixData.csv};

\end{axis}
			\draw (0.1,0)--(0,0)--(0,4)--(0.1,4);
			\draw (3.9,0)--(4,0)--(4,4)--(3.9,4);
		\end{tikzpicture}
		

\end{document}
Weiß jemand Rat? Danke schonmal für Hilfe :)

PS: csv-Datei im Anhang

Verfasst: Mi 18. Jan 2017, 11:56
von esdd
Ersetze die Vorgaben für width und height durch Vorgaben von Einheiten für x und y :
\documentclass[12pt,a4paper,oneside,listof=totoc,bibliography=totoc,BCOR=4mm,DIV=12,]{scrartcl} 
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document} 
\begin{tikzpicture} 
  \begin{axis}[ 
    ticks=none, 
    axis x line=none, 
    axis y line=none, 
     ybar, 
     xtick=0, 
     ymin=0, 
    ymax =4, 
    xmax = 4, 
    xmin = 0, 
    x = 1cm,% <- geändert
    y = 1cm,% <- geändert
  ]
  \addplot+[ 
       only marks, 
       visualization depends on=\thisrow{val} \as \val, 
       nodes near coords=\pgfmathprintnumber{\val}, 
       every node near coord/.append style={ 
           black, 
           inner sep=1pt, 
           yshift=0ex 
       } 
       ] table [col sep=comma] {matrixData.csv}; 
  \end{axis} 
  \draw (0.1,0.1)--(0,0.1)--(0,4.2)--(0.1,4.2); 
  \draw (3.9,0.1)--(4,0.1)--(4,4.2)--(3.9,4.2); 
\end{tikzpicture}
\end{document}
Allerdings erscheint mir das irgendwie sehr, sehr umständlich. Wenn Du das wirklich mit den Vorgaben der einzelnen Koordinaten machen möchtest, kannst Du auch csvsimple verwenden:
\documentclass[12pt,a4paper,oneside,listof=totoc,bibliography=totoc,BCOR=4mm,DIV=12,]{scrartcl} 
\usepackage{tikz} 
\usepackage{csvsimple}

\begin{document} 
\begin{tikzpicture}
  \csvreader
    {matrixData.csv}
    {i=\i,j=\j,val=\val}
    {\node at (\i,\j){\val};}
  \draw (0.1,0)--(0,0)--(0,4)--(0.1,4); 
  \draw (3.9,0)--(4,0)--(4,4)--(3.9,4); 
\end{tikzpicture}
\end{document}
Aus meiner Sicht wäre es aber einfacher, dass gleich als matrix of nodes zu setzen:
\documentclass[12pt,a4paper,oneside,listof=totoc,bibliography=totoc,BCOR=4mm,DIV=12,]{scrartcl} 
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
  \matrix(m)[%
    matrix of nodes,
    inner sep=1pt,
    row sep={.4cm,between origins},
    column sep={.4cm,between origins}
  ]{%
    1&1&1&1&1&1&1&1&0&0\\
    1&1&1&1&1&0&1&1&1&0\\
    1&1&1&1&1&0&1&1&1&1\\
    1&1&1&1&1&0&0&1&1&1\\
    1&1&1&1&1&0&1&1&1&1\\
    1&0&0&1&1&1&1&1&1&1\\
    1&1&1&1&1&1&1&1&1&1\\
    1&1&1&1&1&1&1&1&1&1\\
    1&1&1&1&1&1&1&0&1&1\\
    0&1&1&1&1&1&1&0&1&1\\
  };
  \draw([xshift=.1cm]m.south west)--(m.south west)--(m.north west)--+(.1,0);
  \draw([xshift=-.1cm]m.south east)--(m.south east)--(m.north east)--+(-.1,0);
\end{tikzpicture}
\end{document}

Verfasst: Mi 18. Jan 2017, 12:13
von Kappa
Super Antwort, danke danke danke.

Die Matrizen werden ziemlich groß, deswegen muss ich sie als .csv einlesen.
Gibt es eine Möglichkeit eine tikz->matrix zu erstellen indem jeder (i,j)-Wert eingelesen wird?

Danke nochmals

Verfasst: Mi 18. Jan 2017, 14:04
von esdd
Kappa hat geschrieben: Die Matrizen werden ziemlich groß, deswegen muss ich sie als .csv einlesen.
Gibt es eine Möglichkeit eine tikz->matrix zu erstellen indem jeder (i,j)-Wert eingelesen wird?
Vermutlich verstehe ich nicht, was Du möchtest. Wenn Du die Daten so wie von Dir angegeben verwenden möchtest, dann nimm doch einfach die csvsimple Variante.

Vielleicht hilft Dir aber auch:
\begin{filecontents*}{matrix1.tex}
1&1&1&1&1&1&1&1&0&0\\ 
1&1&1&1&1&0&1&1&1&0\\ 
1&1&1&1&1&0&1&1&1&1\\ 
1&1&1&1&1&0&0&1&1&1\\ 
1&1&1&1&1&0&1&1&1&1\\ 
1&0&0&1&1&1&1&1&1&1\\ 
1&1&1&1&1&1&1&1&1&1\\ 
1&1&1&1&1&1&1&1&1&1\\ 
1&1&1&1&1&1&1&0&1&1\\ 
0&1&1&1&1&1&1&0&1&1
\end{filecontents*}


\documentclass[12pt,a4paper,oneside,listof=totoc,bibliography=totoc,BCOR=4mm,DIV=12,]{scrartcl} 
\usepackage{tikz} 
\usetikzlibrary{matrix} 
\begin{document} 
\begin{tikzpicture} 
   \matrix(m)[% 
     matrix of nodes, 
     inner sep=1pt, 
     row sep={.4cm,between origins}, 
     column sep={.4cm,between origins} 
   ]{\input{matrix1.tex}\\}; 
   \draw([xshift=.1cm]m.south west)--(m.south west)--(m.north west)--+(.1,0); 
   \draw([xshift=-.1cm]m.south east)--(m.south east)--(m.north east)--+(-.1,0); 
\end{tikzpicture}
\end{document}
Zu beachten ist dabei, dass der letzte doppelte Backslash im Argument von \matrix stehen muss, also nicht mit in die Datei matrix1.tex geschrieben werden darf.[/code]

Verfasst: Do 19. Jan 2017, 07:46
von Kappa
Danke nochmals für alles :)