# How to draw and scale for large data values as a sparklines-like diagram with datatool & TikZ?

I want to draw rectangles by the measure of ++(1pt, 4pt) at (x, y) where x and y are based on external data, but I am unsure on how I can proceed to get a proper graph for this.

The obvious ideal example would be

but like I said in the title I'm not strictly after 'sparklines', I guess it could also be called a scatterplot.

So this is the text in the .csv-file. As you can see, I used the opportunity to include all sorts of columns and data in the .csv file and name the headers, which I find really useful. At least I think this is possible and I would like to continue doing it this way; I hope I didn't make an error along the way. I did this to create soem fake data to be able to get something out of it in TikZ because it said the dimensions were too large originally.

MWE

\documentclass{scrartcl}

\usepackage{
tikz,
relsize,
amsmath
}

\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{
datatool,
databar
}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small]
\DTLforeach*{Zeitreihen-BalanceLargeBank-1999-2013}{\x=xnum, \y=y}{\draw[fill=black] (\x*2 pt,\y cm) rectangle ++(1pt,4pt);}
\DTLforeach*{Zeitreihen-BalanceLargeBank-1999-2013}{\x=xnum, \y=xjahr}{\draw (\x*2 pt,0) node[anchor=north] {\y};}
\end{tikzpicture}
\end{center}
\end{document}

-
So are you trying to draw a scatterplot with rectangles for the markers? –  Jake Jul 3 '13 at 14:55
@Jake Yes, I suppose that would be the basic task I want to do. On top of that I would like to alter on add some things to indicate the overall change over time and so forth. –  henry Jul 3 '13 at 15:04
I hope I'm not just being slow to catch on, but I'm having a hard time visualizing what you're going for. Could you find an image of that ideal-ideal visualization of the data? (Any data, it obviously doesn't have to be yours ;). ) –  Sean Allred Jul 3 '13 at 15:07
The best I can think of is this. I just would like to display some y-values over some x-values. The rectangle command is rather arbitrary in this context. The catch is that the original values aren't really "TikZ-friendly". Hence, I converted the data and added some "fake" data (columns 'xnum' and 'y') to be able to plot all the original values (columns: 'xjahr', 'original values'). –  henry Jul 3 '13 at 15:46
Is [this weatherdata sparklines example][1] rather what you want? Otherwise you could just use pgfplots with '\addplot [mark={square*},mark options={mark size=1pt,yscale=4},draw=none] {somefile.txt};' for the marker specification [1]: texample.net/tikz/examples/weather-stations-data –  user24276 Jul 3 '13 at 15:49

On the off chance that all you're trying to do is plot your data, here's how you would do it using PGFPlots (which takes care of scaling the data and creating the axes, and allows you to present the data in a variety of ways).

For setting an overall width of about 10cm, set width=10cm. For getting rid of the top and right axis lines, set axis lines*=left. For using the xjahr column for plotting the data, you can set a x coord trafo/.code for transforming the dates to pure numbers:

\documentclass{scrartcl}

\usepackage{
pgfplots,
amsmath,
filecontents
}

\usepackage[T1]{fontenc}
\usepackage{lmodern}

\begin{filecontents}{data.csv}
xnum,xjahr,y,actually y/50000,,original values
1,1999-01,17.308,,,865387
2,1999-02,16.530,,,826493
3,1999-03,16.708,,,835423
4,1999-04,16.956,,,847800
5,1999-05,17.038,,,851910
6,1999-06,16.900,,,844993
7,1999-07,16.810,,,840493
8,1999-08,16.880,,,843976
9,1999-09,16.408,,,820417
10,1999-10,16.587,,,829335
11,1999-11,17.029,,,851461
12,1999-12,16.501,,,825069
13,2000-01,16.997,,,849872
14,2000-02,17.334,,,866708
15,2000-03,18.122,,,906082
16,2000-04,18.446,,,922312
17,2000-05,18.866,,,943315
18,2000-06,18.154,,,907699
19,2000-07,17.989,,,899459
20,2000-08,18.309,,,915436
21,2000-09,18.617,,,930840
22,2000-10,18.875,,,943742
23,2000-11,19.233,,,961654
24,2000-12,19.396,,,969783
25,2001-01,19.868,,,993401
26,2001-02,20.527,,,1026344
27,2001-03,21.668,,,1083414
28,2001-04,21.948,,,1097404
29,2001-05,22.027,,,1101367
30,2001-06,20.942,,,1047088
31,2001-07,20.442,,,1022115
32,2001-08,20.527,,,1026337
33,2001-09,20.555,,,1027772
34,2001-10,20.621,,,1031049
35,2001-11,20.664,,,1033212
36,2001-12,20.536,,,1026819
37,2002-01,20.245,,,1012261
38,2002-02,20.032,,,1001623
39,2002-03,20.462,,,1023104
40,2002-04,20.187,,,1009366
41,2002-05,20.773,,,1038632
42,2002-06,20.212,,,1010581
43,2002-07,20.116,,,1005803
44,2002-08,20.251,,,1012531
45,2002-09,20.849,,,1042436
46,2002-10,20.852,,,1042580
47,2002-11,21.794,,,1089675
48,2002-12,21.124,,,1056182
49,2003-01,21.169,,,1058460
50,2003-02,21.048,,,1052413
51,2003-03,21.109,,,1055453
52,2003-04,21.288,,,1064424
53,2003-05,22.035,,,1101768
54,2003-06,21.639,,,1081971
55,2003-07,21.261,,,1063031
56,2003-08,21.030,,,1051506
57,2003-09,21.220,,,1060986
58,2003-10,21.102,,,1055099
59,2003-11,20.989,,,1049437
60,2003-12,20.895,,,1044735
61,2004-01,21.137,,,1056866
62,2004-02,21.151,,,1057574
63,2004-03,21.567,,,1078342
64,2004-04,21.957,,,1097832
65,2004-05,21.791,,,1089546
66,2004-06,21.053,,,1052646
67,2004-07,21.183,,,1059131
68,2004-08,21.228,,,1061391
69,2004-09,21.660,,,1083010
70,2004-10,21.869,,,1093465
71,2004-11,22.445,,,1122244
72,2004-12,24.349,,,1217452
73,2005-01,25.029,,,1251463
74,2005-02,25.256,,,1262780
75,2005-03,24.728,,,1236378
76,2005-04,26.789,,,1339462
77,2005-05,26.458,,,1322900
78,2005-06,24.832,,,1241620
79,2005-07,24.915,,,1245746
80,2005-08,24.939,,,1246927
81,2005-09,24.593,,,1229663
82,2005-10,25.041,,,1252056
83,2005-11,25.655,,,1282731
84,2005-12,24.539,,,1226940
85,2006-01,25.302,,,1265120
86,2006-02,25.286,,,1264304
87,2006-03,25.412,,,1270592
88,2006-04,25.825,,,1291228
89,2006-05,26.252,,,1312608
90,2006-06,25.588,,,1279378
91,2006-07,25.074,,,1253699
92,2006-08,25.192,,,1259575
93,2006-09,25.749,,,1287470
94,2006-10,25.659,,,1282956
95,2006-11,26.107,,,1305365
96,2006-12,25.938,,,1296889
97,2007-01,26.266,,,1313293
98,2007-02,26.744,,,1337178
99,2007-03,26.880,,,1344000
100,2007-04,27.685,,,1384240
101,2007-05,28.065,,,1403246
102,2007-06,27.531,,,1376571
103,2007-07,27.465,,,1373268
104,2007-08,27.652,,,1382620
105,2007-09,28.396,,,1419808
106,2007-10,27.762,,,1388094
107,2007-11,28.321,,,1416058
108,2007-12,28.078,,,1403914
109,2008-01,28.779,,,1438948
110,2008-02,29.006,,,1450275
111,2008-03,29.265,,,1463257
112,2008-04,30.278,,,1513904
113,2008-05,30.187,,,1509339
114,2008-06,28.637,,,1431864
115,2008-07,28.864,,,1443179
116,2008-08,29.152,,,1457623
117,2008-09,29.991,,,1499535
118,2008-10,30.414,,,1520699
119,2008-11,30.121,,,1506038
120,2008-12,29.330,,,1466516
121,2009-01,29.655,,,1482739
122,2009-02,28.941,,,1447034
123,2009-03,28.822,,,1441075
124,2009-04,29.206,,,1460275
125,2009-05,27.943,,,1397169
126,2009-06,27.302,,,1365080
127,2009-07,26.964,,,1348188
128,2009-08,26.997,,,1349834
129,2009-09,26.586,,,1329284
130,2009-10,26.708,,,1335384
131,2009-11,26.768,,,1338391
132,2009-12,25.848,,,1292388
133,2010-01,26.179,,,1308947
134,2010-02,25.964,,,1298190
135,2010-03,25.594,,,1279697
136,2010-04,26.939,,,1346967
137,2010-05,27.497,,,1374860
138,2010-06,26.597,,,1329827
139,2010-07,26.217,,,1310865
140,2010-08,27.087,,,1354348
141,2010-09,25.695,,,1284736
142,2010-10,26.489,,,1324447
143,2010-11,27.365,,,1368229
144,2010-12,41.658,,,2082896
145,2011-01,40.145,,,2007247
146,2011-02,39.748,,,1987420
147,2011-03,37.512,,,1875608
148,2011-04,38.648,,,1932378
149,2011-05,39.539,,,1976959
150,2011-06,37.166,,,1858310
151,2011-07,39.240,,,1962024
152,2011-08,42.761,,,2138066
153,2011-09,44.646,,,2232276
154,2011-10,42.490,,,2124486
155,2011-11,42.414,,,2120717
156,2011-12,43.045,,,2152254
157,2012-01,43.541,,,2177035
158,2012-02,43.026,,,2151277
159,2012-03,42.252,,,2112619
160,2012-04,43.287,,,2164348
161,2012-05,47.271,,,2363567
162,2012-06,43.491,,,2174547
163,2012-07,45.590,,,2279517
164,2012-08,45.064,,,2253197
165,2012-09,43.462,,,2173093
166,2012-10,42.871,,,2143564
167,2012-11,42.915,,,2145738
168,2012-12,41.239,,,2061955
169,2013-01,40.208,,,2010401
170,2013-02,40.485,,,2024226
171,2013-03,40.116,,,2005807
172,2013-04,41.069,,,2053430
\end{filecontents}

\def\parsedate#1-#2!{%
\pgfmathparse{#1+1/12*(1#2-101)}%
}

\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
width=10cm, height=5cm,
axis lines*=left,
ymin=0,
enlargelimits=upper,
/pgf/number format/1000 sep={},
x coord trafo/.code={\expandafter\parsedate#1!}
]
\addplot [no markers] table [x=xjahr, y=y] {\datatable};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

-
Hey, looks good so far. Got some questions though: 1) how can I change the labels on the x axis to display every fifth or tenth value of the column xjahr? (adding x=xjahr does not help, pgfplots wants to parse something then) 2) How can I get the graphic to be roughly 10 cm wide? 3) How can I get rid of the upper x-axis and right y-axis? I am somewhat reluctant to use pgfplots because of those questions as I am reluctant to put in the time to find out. TikZ was good enough so far imho. –  henry Jul 3 '13 at 15:51
@henry: I've edited my answer. I'm almost sure that whatever you can do with TikZ for plotting data, you can do much more easily using PGFPlots. The learning curve isn't very steep. –  Jake Jul 3 '13 at 16:33
@henry: Note that this data is really too dense to use a scatter plot (that's why I used a line plot). You can use a scatter plot by setting only marks instead of no markers, and influence the mark size by setting mark size=1 (or something like that). –  Jake Jul 3 '13 at 16:38
Another question: where exactly does axis lines*=left come from? I looked for at least 15 minutes now, but I still can't find anything that remotely explains how that line came to be. There is subsubsection 7.4.3 on p. 48 in the manual but that's not it. –  henry Jul 6 '13 at 10:24
I need to correct myself: my Windows 7 "finder" was always pointing me towards a very old version of the pgfplots manual. Although I must have glanced on the title page and noticed that it was version 1.1, it didn't really register. So sorry about that, something like this usually doesn't happen to me. I just looked at the manual for 1.8 from 2013-03-17 and it's all good. Thanks again. –  henry Jul 6 '13 at 11:55