# Import and interpolate data with pgfplots (and a solution for plotting the chi square distribution)

I have to draw a chi-square distribution, and this is quite a challenge with pgfplots. I found some solutions but I discarded them all for different reasons and I choose the lazy solution:

1. I generated the curve using Octave/MATLAB, then I saved the points in a CSV file (downloadable here):

x = .1:.1:8;
pdf1 = chi2pdf(x,1);
pdf2 = chi2pdf(x,2);
m = [x' pdf1' pdf2' pdf3' pdf4' pdf5' pdf6' pdf7' pdf8'];
csvwrite ('chisquare.csv', m);

2. I imported them in LaTeX using \pgfplotstableread[col sep=comma]{chisquare.csv}\dataChiSquare:

This is the plot of the chi-square distributions for K=1, ..., 8:

 \begin{tikzpicture}
\begin{axis}[%
no markers,
height=5cm, width=10cm,
smooth,
axis x line=bottom, axis y line=left,
xtick=\empty, ytick=\empty,
clip=false,
enlargelimits=upper,
restrict x to domain = 0:7,
restrict y to domain = 0:0.8,
]
\addplot table[x = x, y = pdf1] from \dataChiSquare;
\addplot table[x = x, y = pdf2] from \dataChiSquare;
% .........
\addplot table[x = x, y = pdf8] from \dataChiSquare;
\end{axis}
\end{tikzpicture}


My question is: how can I compute points of the plots, as I asked here? Is there any way to (easily) define a function that interpolates the points using tikz or pgfplots? I need to draw some vertical lines from x axis to the plot, but I also need the coordinates for other purposes.

The preamble of my other question can also be used to have a MWE for this one.

Thank you!

• You could get the coordinates from intersections. My problem is that I do not have MatLab, so I cannot create the .csv files. Any chance you want to post a really minimal compilable example that people (and ducks and marmots) can play with? – user121799 Jun 18 '18 at 2:45
• I added the link to the .csv file on my Dropbox. I can't check right now if the code is compilable, however it should be exactly the same code in my other question linked above, except for the tikzpicture that has to be replaced with the one in this question – Taekwondavide Jun 18 '18 at 2:53
• PS: @marmot thank you for helping with all of my pgf questions! – Taekwondavide Jun 18 '18 at 2:55

Here is an example using intersections and calc. The y value is stored in \yQ. I just made up some data but it should work with your data as well. (I could not use \pgfplotspointgetcoordinates because of expansion issues but it is straightforward to get the coordinate with calc.)

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{chisquare.csv}
x,pdf1,pdf2,pdf3,pdf4,pdf5,pdf6,pdf7,pdf8
0,1000,0.5,0,0,0,0,0,0
0.1,1.200038948430136,0.475614712250357,0.1200038948430136,0.02378073561251787,0.004000129828100455,0.0005945183903129469,8.000259656200907e-005,9.908639838549101e-006
0.2,0.8071711293576808,0.4524187090179798,0.1614342258715362,0.04524187090179798,0.01076228172476909,0.0022620935450899,0.0004304912689907634,7.540311816966337e-005
0.3,0.6269100992275207,0.4303539882125289,0.1880730297682562,0.06455309823187934,0.01880730297682562,0.004841482367390948,0.001128438178609537,0.0002420741183695475
0.4,0.5164415474672782,0.4093653765389909,0.2065766189869113,0.08187307530779819,0.02754354919825484,0.008187307530779821,0.002203483935860388,0.0005458205020519878
0.5,0.4393912894677224,0.3894003915357024,0.2196956447338612,0.09735009788392562,0.03661594078897687,0.0121687622354907,0.003661594078897689,0.001014063519624225
0.6000000000000001,0.381545289384093,0.3704091103408589,0.2289271736304558,0.1111227331022577,0.04578543472609117,0.01666840996533865,0.005494252167130943,0.001666840996533865
0.7000000000000001,0.3360144677267703,0.3523440448593567,0.2352101274087392,0.1233204157007749,0.05488236306203916,0.02158107274763561,0.007683530828685487,0.00251779182055749
0.8,0.298983539918205,0.3351600230178197,0.2391868319345639,0.1340640092071279,0.06378315518255041,0.02681280184142558,0.01020530482920807,0.003575040245523413
0.9,0.268136721052083,0.3188140758108867,0.2413230489468747,0.143466334114899,0.07239691468406238,0.03227992517585227,0.01303144464313124,0.004841988776377838
1,0.2419707245191433,0.3032653298563167,0.2419707245191434,0.1516326649281584,0.08065690817304777,0.0379081662320396,0.01613138163460956,0.006318027705339933
1.1,0.2194581724133437,0.2884749051902433,0.2414039896546781,0.1586611978546339,0.08851479620671532,0.04363182941002433,0.01947325516547737,0.007999168725171124
1.2,0.1998677639017332,0.2744058180470132,0.2398413166820799,0.1646434908282079,0.09593652667283198,0.04939304724846241,0.02302476640147969,0.009878609449692479
1.3,0.1826614817951091,0.261022888380508,0.2374599263336419,0.1696648774473302,0.1028993014112448,0.05514108517038233,0.02675381836692366,0.01194723512024951
1.4,0.1674325573450835,0.2482926518957047,0.2344055802831169,0.1738048563269934,0.1093892707987879,0.06083169971444766,0.03062899582366061,0.01419406326670446
1.5,0.1538663228054553,0.2361832763705073,0.2307994842081829,0.1771374572778805,0.1153997421040915,0.06642654647920521,0.03461992263122744,0.0166066366198013
1.6,0.1417145653062239,0.2246644820586108,0.2267433044899583,0.1797315856468887,0.1209297623946444,0.0718926342587555,0.03869752396628622,0.01917136913566813
1.7,0.1307781819238881,0.2137074659743633,0.2223229092706098,0.1816513460782088,0.1259829819200122,0.07720182208323877,0.04283421385280416,0.02187384959025099
1.8,0.1208951224732049,0.2032848298702996,0.2176112204517688,0.1829563468832696,0.1305667322710613,0.08233035609747134,0.04700402361758206,0.02469910682924141
1.9,0.1119318050861699,0.1933705117272506,0.2126704296637229,0.1837019861408881,0.1346912721203578,0.08725844341692184,0.05118268340573599,0.02763184041535858
2,0.1037768743551487,0.1839397205857212,0.2075537487102974,0.1839397205857211,0.1383691658068649,0.09196986029286061,0.05534766632274598,0.03065662009762019
2.1,0.09633657731357953,0.1749688745555777,0.2023068123585171,0.1837173182833566,0.1416147686509619,0.09645159209876222,0.059478202833404,0.03375805723456678
2.2,0.08953128037314291,0.1664355418490398,0.1969688168209144,0.1830790960339438,0.1444437990020039,0.1006935028186691,0.06355527156088171,0.03692095103351201
2.3,0.0832928061175755,0.1583183846895266,0.1915734540704237,0.1820661423929556,0.1468729814539915,0.1046880318759495,0.06756157146883611,0.04013041221911396
2.4,0.07756236924025954,0.150597105956101,0.1861496861766229,0.1807165271473213,0.1489197489412983,0.1084299162883928,0.07148147949182322,0.04337196651535711
2.5,0.07228895706727252,0.143252398430095,0.1807223926681813,0.1790654980376188,0.1506019938901511,0.1119159362735118,0.07530099694507554,0.04663164011396324
2.6,0.06742804459323157,0.1362658965170063,0.1753129159424021,0.1771456654721082,0.1519378604834151,0.1151446825568703,0.07900768745137589,0.04989602910797716
2.7,0.06294056442554451,0.1296201303229458,0.1699395239489702,0.1749871759359768,0.1529455715540732,0.1181163437567843,0.08259060863919951,0.05315235469055297
2.8,0.05879207325254447,0.1232984819708032,0.1646178051071245,0.1726178747591245,0.1536432847666496,0.1208325123313872,0.08604023946932379,0.05638850575464736
2.9,0.05495207145796054,0.1172851440468988,0.1593610072280856,0.1700634588680033,0.1540489736538161,0.1232960076793024,0.08934840471921335,0.0595930703783295
3,0.05139344326792309,0.1115650800742149,0.1541803298037693,0.1673476201113224,0.1541803298037693,0.1255107150834918,0.0925081978822616,0.0627553575417459
3.1,0.0480919926380412,0.1061239869133715,0.1490851771779277,0.1644921797157259,0.1540546830838587,0.1274814392796876,0.09551390351199233,0.06586541029450528
3.2,0.04502605584019208,0.1009482589973277,0.1440833786886146,0.1615172143957243,0.1536889372678556,0.1292137715165795,0.09836091985142761,0.06891401147550903
3.3,0.04217617598652614,0.09602495431037704,0.1391813807555363,0.1584411746121221,0.1530995188310899,0.1307139690550008,0.1010456824285194,0.07189268298025039
3.4,0.03952482794294565,0.0913417620263673,0.1343844150060152,0.1552809954448245,0.1523023370068173,0.1319888461281008,0.1035655891646358,0.0747936794725905
3.5,0.03705618452374813,0.08688697172522257,0.1296966458331184,0.1520522005191395,0.1513127534719715,0.1330456754542471,0.1059189274303801,0.07760997734831081
3.6,0.03475591672713835,0.08264944411079327,0.125121300217698,0.1487689993994279,0.1501455602612377,0.1338920994594851,0.1081048033880911,0.08033525967569108
3.7,0.03261102221401006,0.0786185831568138,0.1206607821918372,0.1454443788401056,0.1488149647032659,0.1345360504270977,0.1101230738804168,0.08296389776337691
3.8,0.03060967735558654,0.07478430961131752,0.1163167739512288,0.1420901882615033,0.1473345803382232,0.1349856788484282,0.1119742810570497,0.0854909299373378
3.9,0.02874110905657778,0.07113703579325677,0.1120903253206534,0.1387172197968507,0.1457174229168494,0.1352492893019295,0.1136595898751425,0.08791203804625421
4,0.02699548325659403,0.06766764161830635,0.1079819330263761,0.1353352832366127,0.1439759107018348,0.1353352832366127,0.1151807285614679,0.09022352215774178
4.100000000000001,0.02536380756620646,0.06436745179390209,0.1039916110214465,0.1319532761774993,0.1421218683959769,0.1352521080819368,0.1165399320847011,0.09242227385599021
4.2,0.023837845937227,0.06122821412649095,0.1001189529363534,0.128579249665631,0.1401665341108948,0.1350082121489126,0.1177398886531517,0.0945057485042388
4.3,0.0224100436236817,0.05824207888674848,0.09636318758183131,0.1252204696065092,0.1381205688672915,0.1346120048269975,0.1187836892258708,0.09647193679268147
4.4,0.02107346097903018,0.05540157918116694,0.09272322830773279,0.1218834741985672,0.1359940681846747,0.134071821618424,0.1196747800025138,0.09831933585351095
4.5,0.01982171487060489,0.05269961228093217,0.08919771691772202,0.1185741276320974,0.1337965753765831,0.1333958935861096,0.1204169178389248,0.1000469201895822
4.600000000000001,0.01864892668496918,0.05012942186140185,0.08578506275085825,0.1152976702812243,0.1315370962179827,0.1325923208234079,0.1210141285205441,0.1016541126312795
4.7,0.01754967605644785,0.04768458110777481,0.08248347746530486,0.1120587656032708,0.1292241146956443,0.1316690495838432,0.1214706678139056,0.1031407555073439
4.800000000000001,0.01651895958214552,0.04535897664470624,0.07929100599429849,0.108861543947295,0.1268656095908776,0.130633852736754,0.1217909852072426,0.1045070821894032
4.9,0.01555215389559439,0.04314679324968525,0.07620555408841254,0.1057096434617288,0.1244690716777404,0.1294943132406179,0.1219796902441857,0.1057536891465046
5,0.01464498256192649,0.0410424993119494,0.07322491280963242,0.1026062482798735,0.1220415213493874,0.1282578103498418,0.1220415213493874,0.1068815086248682
5.100000000000001,0.01379348633346337,0.03904083300057656,0.0703467803006632,0.09955412415147029,0.1195895265111275,0.1269315082931246,0.12198131704135,0.1078917820491559
5.2,0.012993996368508,0.03713678910716694,0.06756878111624158,0.09655565167863402,0.1171192206014854,0.1255223471822242,0.1218039894255449,0.1087860342245943
5.300000000000001,0.01224311007004337,0.03532560653021478,0.06488848337122986,0.09361285730506919,0.1146363206225061,0.1240370359292167,0.1215144998598565,0.1095660484041414
5.4,0.01153766924671384,0.03360275636987488,0.06230341393225473,0.09072744219866219,0.1121461450780586,0.122482046968194,0.1211178366843032,0.1102338422713746
5.5,0.01087474033728314,0.03196393060335379,0.05981107185505726,0.0879008091592229,0.1096536317342717,0.1208636125939315,0.1206189949076989,0.1107916448777706
5.600000000000001,0.01025159647287075,0.03040503131260898,0.05740894024807616,0.08513408767530517,0.1071633551297422,0.1191877227454272,0.1200229577453113,0.1112418745623988
5.7,0.009665701179594184,0.02892216043741923,0.05509449672368685,0.08242815724664482,0.1046795437750051,0.1174601240764689,0.1193346799035058,0.1115871178726455
5.800000000000001,0.009114693548557632,0.0275116100282036,0.05286522258163426,0.07978366908179048,0.1022060969911596,0.1156863201685962,0.1185590725097452,0.1118301094963097
5.9,0.008596374721056833,0.02616985297421619,0.0507186108542353,0.07720106627393776,0.09974660134666277,0.1138715727540582,0.1177009895890621,0.1119737132081573
6,0.008108695554940244,0.02489353418393197,0.04865217332964145,0.07468060255179593,0.09730434665928291,0.1120209038276939,0.1167652159911395,0.1120209038276939
6.100000000000001,0.00764974535371112,0.02367946219557045,0.04666344665763784,0.07222235969648989,0.09488234153719693,0.1101390985371471,0.1157564566753803,0.1119747501794329
6.2,0.0072177415535363,0.0225246011967789,0.04474999763192506,0.0698262637100146,0.09248332843931178,0.1082307087505227,0.1146793272647467,0.1118383990422068
6.300000000000001,0.006811020275148059,0.02142606343352008,0.04290942773343277,0.06749209981558829,0.09010979824020883,0.1063000572095515,0.1135383457826631,0.1116150600700292
6.4,0.006428027657939708,0.02038110198918311,0.04113937701081413,0.06521952636538594,0.0877640042897368,0.1043512421846175,0.1123379254908632,0.111307991663592
6.5,0.006067311902576734,0.019387103915861,0.03943752736674877,0.06300808772654828,0.085447975961289,0.1023881425556409,0.1110823687496757,0.110920487768611
6.600000000000001,0.005727515956354745,0.01844158370062,0.03780160531194131,0.060857226212046,0.08316353168627089,0.1004144232498759,0.1097758618258776,0.1104558655748635
6.7,0.005407370782485125,0.01754217705042251,0.03622938424265033,0.05876629311891542,0.08091229147525246,0.09843354097418335,0.1084224705768383,0.1099174540878381
6.800000000000001,0.005105689160609535,0.01668663498016303,0.03471868629214483,0.05673455893255431,0.07869568892886164,0.09644875018534235,0.1070261369432519,0.109308583543388
6.9,0.004821359971245364,0.01587281818903397,0.033267383801593,0.05476122275216719,0.07651498274366392,0.09446310924748844,0.1055906761862563,0.1086325756346117
7,0.004553342921640174,0.01509869171115925,0.0318734004514812,0.05284542098905737,0.07437126772012283,0.09247948673085041,0.1041197748081719,0.1078927345193254
7.100000000000001,0.004300663674745038,0.01436231982711971,0.03053471209068977,0.05098623538627498,0.07226548528129911,0.09050056781063809,0.1026169890994448,0.1070923385759218
7.2,0.004062409346773289,0.01366186122364628,0.02924934729676768,0.04918270040512661,0.07019843351224242,0.0885288607292279,0.1010857442576291,0.1062346328750735
7.300000000000001,0.00383772434215234,0.01299556438937767,0.02801538769771208,0.0474338100212285,0.06817077673109938,0.086566703288742,0.09952933402740517,0.1053228223346361
7.4,0.003625806497653241,0.01236176323516969,0.02683096808263397,0.04573852397012787,0.06618305460383049,0.08461626934473659,0.09795092081366917,0.1043600655251752
7.5,0.003425903510139483,0.01175887292800455,0.02569427632604612,0.04409577348001709,0.06423569081511527,0.08267957527503202,0.09635353622267297,0.10334946909379
7.600000000000001,0.003237309624752144,0.0111853859280828,0.02460355314811629,0.04250446652671462,0.0623290013085613,0.08075848640075782,0.09474008198901322,0.1022940827742932
7.7,0.003059362562475526,0.01063986821918858,0.02355709173106154,0.04096349264387606,0.06046320210972462,0.0788547233394614,0.09311333124897592,0.1011968949523088
7.800000000000001,0.002891440667935384,0.01012095572290219,0.022553237209896,0.03947172731931856,0.05863841674572961,0.0769698682726712,0.09147593012333818,0.1000608287544726
7.9,0.002732960259995687,0.00962735088769346,0.02159038605396593,0.03802803600638919,0.05685468327544363,0.07510537111261865,0.08983039957520095,0.09888873863161463
8,0.002583373169261507,0.009157819444367089,0.02066698535409205,0.03663127777746836,0.05511196094424543,0.0732625555549367,0.08817913751079275,0.09768340740658221
\end{filecontents*}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{intersections,calc}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
no markers,
height=5cm, width=10cm,
smooth,
axis x line=bottom, axis y line=left,
xtick=\empty, ytick=\empty,
clip=false,
enlargelimits=upper,
restrict x to domain = 0:7,
restrict y to domain = 0:0.8,
]
\addplot[name path=plot1] table[x = x, y = pdf1]  \dataChiSquare;
\path[name path=y0] (0,0) -- (4,0);
\path[name path=p1] (1.2,0) -- (1.2,0.8);
\coordinate[overlay] (X) at (1,1);
\end{axis}
\path [name intersections={of=plot1 and p1, by=Q}];
\path let \p1=(X),\p2=(Q) in \pgfextra{\pgfmathsetmacro{\yQ}{\y2/\y1}
\typeout{y\space coordinate\space of\space Q\space is\space \yQ}};
\draw [name intersections={of=y0 and p1, by=P}]   (P) -- (Q);
\end{tikzpicture}
\end{document}


• Thank you. It works but I can't understand the code, could you please explain me how does the \path let ... \typeout ...; part work? – Taekwondavide Jun 19 '18 at 1:10
• @Taekwondavide Sure. I put a coordinate (X) at (1,1) such that I know how much pgfplots stretches the coordinates. Then I compute the absolute coordinates for (X) and the intersection point (Q), and divide the absolute y coordinate of (Q) by the one of (X) such that I know the y coordinate in pgfplots units. In principle, one should be able to do that with \pgfplotspointgetcoordinates but there are the usual expansion issues that make this too cumbersome (for me). – user121799 Jun 19 '18 at 1:22
• Thank you! I managed to do that with a few instruction less, starting from your code, by putting this after axis environment: \path[name path=Qv] (1.2,0) -- (1.2,0.8); \coordinate[overlay] (X) at (1,1); \path [name intersections={of=plot1 and Qv, by=Q}]; \node[coordinate] (Q0) at (axis cs: 1.2,0) {}; \draw (Q0) -- (Q); – Taekwondavide Jun 22 '18 at 15:43
• @Taekwondavide Sure. I thought you wanted the value \yQ`. Anyway, if you are happy, everything is good. ;-) – user121799 Jun 22 '18 at 15:45
• Whops, sorry, my fault... I wrote that I needed the coordinates but I ment the node :-( However you provided me everything I need! – Taekwondavide Jun 22 '18 at 15:51