2

I would like to create a data visualization in TikZ. I am using the following code:

\documentclass{standalone}
\usepackage{filecontents}
\usepackage{tikz}
\usetikzlibrary{datavisualization}

\begin{filecontents}{test.csv}
x y
-3 5
-2 7
-1 2
0 5
1 4
2 8
3 3
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\datavisualization
[
scientific axes,
all axes={length=5cm},
x axis={label=x, attribute=x, min value=-2, max value=2},
y axis={label=y, attribute=y, min value=0, max value=10},
visualize as line,
]
data[read from file=test.csv, separator={\space}];
\end{tikzpicture}
\end{document}

How can I achieve that only a range ([-2, 2] in this case) of the csv file is used? It causes drawing errors, and loading unnecessary lines is time consuming when using large csv files.

Drawing errors

braxlan
  • 165
  • 4

1 Answers1

0

You could use pgfplots:

\documentclass{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}

\begin{filecontents}{test.csv}
x y
-3 5
-2 7
-1 2
0 5
1 4
2 8
3 3
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xlabel=x,ylabel=y,height=5cm,width=5cm,
         xmax=2,xmin=-2]
\addplot[no markers] table[x=x,y=y] {test.csv};
\end{axis}
\end{tikzpicture}
\end{document}
  • This works, but it makes the graph look different to all the others made with \datavisualization. – braxlan Sep 23 '16 at 06:43
  • You can customize the graphs with axis options. In fact, why are you using \datavisualization instead of pgfplots (just curious, I didn't know about this tikz's data feature) – Guilherme Zanotelli Sep 23 '16 at 06:53
  • Did a little research on the matter http://tex.stackexchange.com/questions/198528/tikz-pgf-3-0-datavisualization-vs-pgfplots it's more of an opinion sort of answer but it's a fact that pgfplots is older and therefore is more developed than datavisualization. I have used pgfplots a lot and I know that there are MANY customizable options for plotting, especially for standardizing plots (you can use global options) so, IMHO change to pgfplots if you are still in time. – Guilherme Zanotelli Sep 23 '16 at 07:16
  • Thank you for the solution. I need to figure out if it takes more time to switch to pgfplots or to cut the range in the csv file by hand. I chose datavisualization because I did all of my plots with TikZ, so I was just looking up its manual to see how to do plots. – braxlan Sep 23 '16 at 09:08
  • No problem, though Ìt wasn't really a correct answer, since you asked using \datavisualiyation (I looked for it and didn't found any, so posted that one). I would say that for future plots, future you would thank you for switching to pgfplots. But for the current document your doing, it's really up to you. – Guilherme Zanotelli Sep 23 '16 at 13:12