# Unexpected output with TikZ \datavisualization – reading from a csv

I am trying to display a \datavisualization with TikZ, but even after rereading the pgf/TikZ manual (part VI, section 58) and checking everything again, I'm not getting the expected plot at all. I don't get any errors, but the display is messed up.

Having loaded the datavisualization TikZ library, here's what I'm doing:

\begin{tikzpicture}
\datavisualization [school book axes,
visualize as scatter]

;
\end{tikzpicture}


Here 1.csv, 2.csv and 3.csv are existing files in the same directory as the .tex file, and they follow the following format (the [...] indicates it goes on):

x, y
0, 0
0.010000, -0.000200
0.020000, -0.005238
0.030000, -0.017640
0.040000, -0.034847
0.050000, -0.054843
0.060000, -0.076289
0.070000, -0.098301
0.080000, -0.120320
0.090000, -0.142004
[...]


From what I've read in section 57.2 of the pgf manual, under the header Number accuracy, there's no conflict with numeric format or decimal precision.

With all of this, the output is degenerate:

This is not the expected graph at all. y values range from 0 to -1, and there's no repeated x values. I can't figure out what I'm doing wrong!

# MWE

I've included the whole ugly preamble just in case there's something that's messing things up.

% ---DOCUMENT CLASS---
\documentclass[11pt, a4paper]{article}
\usepackage[margin=2.5cm]{geometry}
\usepackage[utf8]{inputenc}

% ---MINTED---
\usepackage{mdframed}
%\usepackage{minted}

%\BeforeBeginEnvironment{minted}{\begin{mdframed}}
%\AfterEndEnvironment{minted}{\end{mdframed}}

% ---MISC. PACKAGES---
\usepackage{setspace}
\usepackage{amssymb}
\usepackage{multirow}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{bigints}
\usepackage{float}
\usepackage{pgfplots}
\usepackage{filecontents}

% ---FOOTNOTES---
\usepackage[symbol]{footmisc}
\renewcommand{\thefootnote}{\fnsymbol{footnote}}

% ---NEW MATH COMMANDS---
\newcommand*{\QED}{\hfill\ensuremath{\square}}
\newcommand{\z}{\mathbb{Z}}

% ---AMSTHM---
\usepackage{amsthm}
\newtheorem{cor}{Corollary}
\newtheoremstyle{big}% hname
{4em}% hSpace above
{4em}% hSpace below
{\itshape}% hBody font
{}% hIndent amount
{}%
\theoremstyle{big}
\newtheorem{prop}{Proposition}

\usepackage{amsfonts}
\usepackage{chngcntr}
\usepackage[labelfont=bf]{caption}
\captionsetup[listing]{position=top}

% ---LISTINGS---
%\renewcommand{\listingscaption}{Code listing}
\usepackage[title]{appendix}

% ---TIKZ---
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\usetikzlibrary{datavisualization}
\usetikzlibrary{positioning}
\tikzstyle{startstop} = [rectangle,
rounded corners,
minimum width=3cm,
minimum height=1cm,
text centered,
draw=black,
fill=red!30]
\tikzstyle{io} = [trapezium,
trapezium left angle=70,
trapezium right angle=110,
minimum width=0.5cm,
minimum height=1cm,
text centered,
draw=black,
fill=blue!10]
\tikzstyle{process} = [rectangle,
minimum width=3cm,
minimum height=1cm,
text centered,
draw=black,
fill=orange!30]
\tikzstyle{decision} = [diamond,
minimum width=3cm,
minimum height=1cm,
text centered,
draw=black,
fill=green!30]
\tikzstyle{arrow} = [thick, ->, >=stealth]

% ---PLOTS---
\pgfplotsset{compat=1.12}

% ---XCOLOR---
\usepackage{xcolor}
\definecolor{bg}{rgb}{0.95,0.95,0.95}
\definecolor{dgreen}{rgb}{0.1, 0.5, 0.2}

% ---HYPERREF---
\usepackage{hyperref}
\hypersetup{
filecolor=magenta,
urlcolor=cyan,
citecolor=dark blue
}

\newcommand{\propautorefname}{Proposition}
\newcommand{\corautorefname}{Corollary}
\newcommand{\listingautorefname}{Code Listing}

\begin{document}

\begin{tikzpicture}
\datavisualization [school book axes,
visualize as scatter]

;
\end{tikzpicture}

\end{document}

• First thing I would test is change the filenames (fi,le)_(1).csv is likely to bork any file handler. why not use 1.csv 2.csv 3.csv ? – user170109 Jan 4 at 15:13
• @KJO Done; still same result. – Anakhand Jan 4 at 15:18
• I think you want to add headline={x, y} (i.e. changing data[read from file={1.csv}] to data[headline={x, y}, read from file={1.csv}]). You might need to remove the x y at the top of the csv files also. – whatisit Jan 4 at 17:45
• @whatisit That sems to fix it. But why? The explicit format is documented to work in the manual. – Anakhand Jan 4 at 18:36
• @Anakhand unfortunately, I don't know... I'm looking at the pgf/tikz manual and it looks like your method should work fine...but it doesn't work for me either. Explicitly using headline key does work, however. Perhaps a but introduced in a more recent update? – whatisit Jan 4 at 18:40

Most of the preamble has no affect on this issue. I've kept only the essential portions.

It is not clear why the headline (i.e. top line of the data .csv file) is not being read/used properly. To circumvent this problem, explicitly use the headline key and specify {x, y} as the value.

By using the headline key, you must also remove x, y in the data file! If you do not, it will be treated as a data point.

You can find more about headline at: http://mirrors.ctan.org/graphics/pgf/base/doc/pgfmanual.pdf#pgf.headline (page 770)

% ---DOCUMENT CLASS---
\documentclass[11pt, a4paper]{article}
\usepackage[margin=2.5cm]{geometry}

% ---MISC. PACKAGES---
\usepackage{pgfplots}

% ---TIKZ---
\usepackage{tikz}
\usetikzlibrary{datavisualization}

% ---PLOTS---
\pgfplotsset{compat=1.16}

\begin{document}

\begin{tikzpicture}
\datavisualization [school book axes,
all axes={length=6cm},
x axis={min value=0,max value=1,ticks={step=0.5,minor steps between steps=4}},
y axis={min value=-1,max value=0,ticks={step=0.5,minor steps between steps=4}},
visualize as scatter]


As mentioned in the comments... this is strange behavior. Even the pgf/tikz manual shows that you were formatting your data file correctly. They have multiple examples which agree. I'm guessing this is a bug that was accidentally introduced? But I'm not entirely certain.
In case you are wondering, I picked up on this issue when looking around at other examples of reading data files with \datavisualization. This answer from a very similar question ended up helping me identify the issue.