4

The goal is to fill the regions where the curve is in [-1, 1], which should look something like the green rectangles below:

enter image description here

Currently, I'm trying to do it by drawing vertical lines of the preselected x coordinates for which the curve is outside [-1, 1]. (Commented code which is not working.)

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \centering
    \begin{axis}[
        xlabel = $ka / \pi$,
        xmin = -0, xmax = 5,
        ymin = -2, ymax = 3
    ]

    \addplot [color = red] table[x index = 0, y index = 1] {data/periodic_delta.txt};
     %\addplot [color = blue] table[x index = 0, variable = \y , domain = -1 : 1, smooth] plot ({data/periodic_deltaF.txt}, {\y});

    \addplot [black, dashed, variable = \x , domain = 0 : 5, smooth] plot ({\x},{1});   
    \addplot [black, dashed, variable = \x , domain = 0 : 5, smooth] plot ({\x},{-1});

    \addlegendentry{$P\frac{\sin(ka)}{ka} + \cos(ka)$}; 
    \addlegendentry{$x = 1$};
    \addlegendentry{$x = -1$};
    \end{axis}
\end{tikzpicture} 
\end{document}

The result is:

enter image description here

How to draw vertical lines for y in [-1, 1], with the x coordinate read from a file? Is there a better/easier way to fill in the regions for which the function is outside(/inside) of [-1, 1]?


periodic_delta.txt

 0.0000000000000000e+000 NaN
 1.0000000000000000e-002 2.5992433839026097e+000
 2.0000000000000000e-002 2.5969741784120179e+000
 2.9999999999999999e-002 2.5931943113425362e+000
 4.0000000000000001e-002 2.5879069937504835e+000
 5.0000000000000003e-002 2.5811167169848361e+000
 5.9999999999999998e-002 2.5728292485620510e+000
 7.0000000000000007e-002 2.5630516268689565e+000
 8.0000000000000002e-002 2.5517921546987248e+000
 8.9999999999999997e-002 2.5390603916260446e+000
 1.0000000000000001e-001 2.5248671452286993e+000
 1.1000000000000000e-001 2.5092244611638641e+000
 1.2000000000000000e-001 2.4921456121085006e+000
 1.3000000000000000e-001 2.4736450855743142e+000
 1.4000000000000001e-001 2.4537385706088002e+000
 1.4999999999999999e-001 2.4324429433949533e+000
 1.6000000000000000e-001 2.4097762517632635e+000
 1.7000000000000001e-001 2.3857576986306386e+000
 1.7999999999999999e-001 2.3604076243819243e+000
 1.9000000000000000e-001 2.3337474882106655e+000
 2.0000000000000001e-001 2.3057998484367701e+000
 2.0999999999999999e-001 2.2765883418196631e+000
 2.2000000000000000e-001 2.2461376618865030e+000
 2.3000000000000001e-001 2.2144735362959338e+000
 2.3999999999999999e-001 2.1816227032587729e+000
 2.5000000000000000e-001 2.1476128870379174e+000
 2.6000000000000001e-001 2.1124727725506185e+000
 2.7000000000000002e-001 2.0762319790971220e+000
 2.8000000000000003e-001 2.0389210332404892e+000
 2.8999999999999998e-001 2.0005713408632260e+000
 2.9999999999999999e-001 1.9612151584270969e+000
 3.1000000000000000e-001 1.9208855634632791e+000
 3.2000000000000001e-001 1.8796164243207092e+000
 3.3000000000000002e-001 1.8374423692011859e+000
 3.4000000000000002e-001 1.7943987545104574e+000
 3.5000000000000003e-001 1.7505216325551549e+000
 3.5999999999999999e-001 1.7058477186160661e+000
 3.7000000000000000e-001 1.6604143574287933e+000
 3.8000000000000000e-001 1.6142594891034323e+000
 3.9000000000000001e-001 1.5674216145153914e+000
 4.0000000000000002e-001 1.5199397601999989e+000
 4.1000000000000003e-001 1.4718534427839729e+000
 4.1999999999999998e-001 1.4232026329872958e+000
 4.2999999999999999e-001 1.3740277192293844e+000
 4.4000000000000000e-001 1.3243694708738611e+000
 4.5000000000000001e-001 1.2742690011465252e+000
 4.6000000000000002e-001 1.2237677297614415e+000
 4.7000000000000003e-001 1.1729073452903183e+000
 4.7999999999999998e-001 1.1217297673105935e+000
 4.8999999999999999e-001 1.0702771083678198e+000
 5.0000000000000000e-001 1.0185916357881299e+000
 5.1000000000000001e-001 9.6671573337667371e-001
 5.2000000000000002e-001 9.1469186303802241e-001
 5.3000000000000003e-001 8.6256252635459485e-001
 5.4000000000000004e-001 8.1037022615918264e-001
 5.5000000000000004e-001 7.5815742813764631e-001
 5.6000000000000005e-001 7.0596652249781089e-001
 5.7000000000000006e-001 6.5383978574051360e-001
 5.7999999999999996e-001 6.0181934256863689e-001
 5.8999999999999997e-001 5.4994712796982981e-001
 5.9999999999999998e-001 4.9826484950841976e-001
 6.0999999999999999e-001 4.4681394986182182e-001
 6.2000000000000000e-001 3.9563556963649399e-001
 6.3000000000000000e-001 3.4477051049821100e-001
 6.4000000000000001e-001 2.9425919865111089e-001
 6.5000000000000002e-001 2.4414164869962712e-001
 6.6000000000000003e-001 1.9445742792703036e-001
 6.7000000000000004e-001 1.4524562102391070e-001
 6.8000000000000005e-001 9.6544795299455921e-002
 6.9000000000000006e-001 4.8392966407950433e-002
 7.0000000000000007e-001 8.2756462236566186e-004
 7.0999999999999996e-001 -4.6114598313579003e-002
 7.1999999999999997e-001 -9.2397361724100957e-002
 7.2999999999999998e-001 -1.3798524790058797e-001
 7.3999999999999999e-001 -1.8284349333179450e-001
 7.5000000000000000e-001 -2.2693807923609083e-001
 7.6000000000000001e-001 -2.7023576136750821e-001
 7.7000000000000002e-001 -3.1270409906804519e-001
 7.8000000000000003e-001 -3.5431148353943426e-001
 7.9000000000000004e-001 -3.9502716530830528e-001
 8.0000000000000004e-001 -4.3482128085949162e-001
 8.1000000000000005e-001 -4.7366487841300686e-001
 8.2000000000000006e-001 -5.1152994282105935e-001
 8.3000000000000007e-001 -5.4838941956233167e-001
 8.3999999999999997e-001 -5.8421723781159707e-001
 8.4999999999999998e-001 -6.1898833256367669e-001
 8.5999999999999999e-001 -6.5267866579161349e-001
 8.7000000000000000e-001 -6.8526524661990840e-001
 8.8000000000000000e-001 -7.1672615049458122e-001
 8.9000000000000001e-001 -7.4704053733280951e-001
 9.0000000000000002e-001 -7.7618866863587099e-001
 9.1000000000000003e-001 -8.0415192355010900e-001
 9.2000000000000004e-001 -8.3091281386166638e-001
 9.3000000000000005e-001 -8.5645499791174262e-001
 9.4000000000000006e-001 -8.8076329342017623e-001
 9.5000000000000007e-001 -9.0382368920620648e-001
 9.5999999999999996e-001 -9.2562335579631105e-001
 9.6999999999999997e-001 -9.4615065491010708e-001
 9.7999999999999998e-001 -9.6539514781635849e-001
 9.8999999999999999e-001 -9.8334760255222786e-001
 1.0000000000000000e+000 -1.0000000000000002e+000
 1.0100000000000000e+000 -1.0153455388165920e+000
 1.0200000000000000e+000 -1.0293786392122666e+000
 1.0300000000000000e+000 -1.0420949455760740e+000
 1.0400000000000000e+000 -1.0534913279466320e+000
 1.0500000000000000e+000 -1.0635658823279805e+000
 1.0600000000000001e+000 -1.0723179298513319e+000
 1.0700000000000001e+000 -1.0797480147846490e+000
 1.0800000000000001e+000 -1.0858579013930825e+000
 1.0900000000000001e+000 -1.0906505696543918e+000
 1.1000000000000001e+000 -1.0941302098345669e+000
 1.1100000000000001e+000 -1.0963022159299554e+000
 1.1200000000000001e+000 -1.0971731779832781e+000
 1.1300000000000001e+000 -1.0967508732819840e+000
 1.1400000000000001e+000 -1.0950442564484661e+000
 1.1500000000000001e+000 -1.0920634484327050e+000
 1.1599999999999999e+000 -1.0878197244189531e+000
 1.1699999999999999e+000 -1.0823255006591044e+000
 1.1799999999999999e+000 -1.0755943202464078e+000
 1.1899999999999999e+000 -1.0676408378441917e+000
 1.2000000000000000e+000 -1.0584808033852511e+000
 1.2100000000000000e+000 -1.0481310447585286e+000
 1.2200000000000000e+000 -1.0366094495006719e+000
 1.2300000000000000e+000 -1.0239349455109954e+000
 1.2400000000000000e+000 -1.0101274808092879e+000
 1.2500000000000000e+000 -9.9520800235682128e-001
 1.2600000000000000e+000 -9.7919843396178519e-001
 1.2700000000000000e+000 -9.6212165329123978e-001
 1.2800000000000000e+000 -9.4400146801252083e-001
 1.2900000000000000e+000 -9.2486259108783875e-001
 1.3000000000000000e+000 -9.0473061524661713e-001
 1.3100000000000001e+000 -8.8363198666087528e-001
 1.3200000000000001e+000 -8.6159397784971459e-001
 1.3300000000000001e+000 -8.3864465983967107e-001
 1.3400000000000001e+000 -8.1481287360841104e-001
 1.3500000000000001e+000 -7.9012820083988977e-001
 1.3600000000000001e+000 -7.6462093401974696e-001
 1.3700000000000001e+000 -7.3832204590031658e-001
 1.3800000000000001e+000 -7.1126315836520437e-001
 1.3900000000000001e+000 -6.8347651072395266e-001
 1.4000000000000001e+000 -6.5499492746781929e-001
 1.4099999999999999e+000 -6.2585178551819687e-001
 1.4199999999999999e+000 -5.9608098099966089e-001
 1.4299999999999999e+000 -5.6571689557005422e-001
 1.4399999999999999e+000 -5.3479436234043298e-001
 1.4500000000000000e+000 -5.0334863141804664e-001
 1.4600000000000000e+000 -4.7141533510586753e-001
 1.4700000000000000e+000 -4.3903045279249209e-001
 1.4800000000000000e+000 -4.0623027556648522e-001
 1.4900000000000000e+000 -3.7305137058950349e-001
 1.5000000000000000e+000 -3.3953054526271026e-001
 1.5100000000000000e+000 -3.0570481122117510e-001
 1.5200000000000000e+000 -2.7161134819109056e-001
 1.5300000000000000e+000 -2.3728746774471943e-001
 1.5400000000000000e+000 -2.0277057698808740e-001
 1.5500000000000000e+000 -1.6809814221643554e-001
 1.5600000000000001e+000 -1.3330765257246716e-001
 1.5700000000000001e+000 -9.8436583742388228e-002
 1.5800000000000001e+000 -6.3522361724654669e-002
 1.5900000000000001e+000 -2.8602326706270775e-002
 1.6000000000000001e+000 6.2863029186843722e-003
 1.6100000000000001e+000 4.1106466353776905e-002
 1.6200000000000001e+000 7.5821295993859394e-002
 1.6300000000000001e+000 1.1039415277356313e-001
 1.6400000000000001e+000 1.4478866123680578e-001
 1.6500000000000001e+000 1.7896874429381243e-001
 1.6600000000000001e+000 2.1289865763245536e-001
 1.6699999999999999e+000 2.4654302375110881e-001
 1.6799999999999999e+000 2.7986686558057539e-001
 1.6899999999999999e+000 3.1283563966310335e-001
 1.7000000000000000e+000 3.4541526885695206e-001
 1.7100000000000000e+000 3.7757217453544933e-001
 1.7200000000000000e+000 4.0927330825002495e-001
 1.7300000000000000e+000 4.4048618282721441e-001
 1.7400000000000000e+000 4.7117890287023934e-001
 1.7500000000000000e+000 5.0132019463635247e-001
 1.7600000000000000e+000 5.3087943526177184e-001
 1.7700000000000000e+000 5.5982668130669755e-001
 1.7800000000000000e+000 5.8813269659356626e-001
 1.7900000000000000e+000 6.1576897931243091e-001
 1.8000000000000000e+000 6.4270778836807874e-001
 1.8100000000000001e+000 6.6892216894425294e-001
 1.8200000000000001e+000 6.9438597726114526e-001
 1.8300000000000001e+000 7.1907390450310327e-001
 1.8400000000000001e+000 7.4296149989435056e-001
 1.8500000000000001e+000 7.6602519290134785e-001
 1.8600000000000001e+000 7.8824231454129445e-001
 1.8700000000000001e+000 8.0959111777716675e-001
 1.8800000000000001e+000 8.3005079698057593e-001
 1.8900000000000001e+000 8.4960150644466981e-001
 1.9000000000000001e+000 8.6822437793023044e-001
 1.9100000000000001e+000 8.8590153722907994e-001
 1.9199999999999999e+000 9.0261611972987743e-001
 1.9299999999999999e+000 9.1835228497236687e-001
 1.9399999999999999e+000 9.3309523017714147e-001
 1.9500000000000000e+000 9.4683120273899191e-001
 1.9600000000000000e+000 9.5954751167292684e-001
 1.9700000000000000e+000 9.7123253800298681e-001
 1.9800000000000000e+000 9.8187574408500145e-001
 1.9900000000000000e+000 9.9146768185549605e-001
 2.0000000000000000e+000 1.0000000000000002e+000
 2.0100000000000002e+000 1.0074654500350693e+000
 2.0200000000000000e+000 1.0138578912993979e+000
 2.0300000000000002e+000 1.0191722948504611e+000
 2.0400000000000000e+000 1.0234047462642035e+000
 2.0499999999999998e+000 1.0265524473363499e+000
 2.0600000000000001e+000 1.0286137166850002e+000
 2.0699999999999998e+000 1.0295879892552278e+000
 2.0800000000000001e+000 1.0294758147274807e+000
 2.0899999999999999e+000 1.0282788548326460e+000
 2.1000000000000001e+000 1.0259998795777034e+000
 2.1099999999999999e+000 1.0226427623869554e+000
 2.1200000000000001e+000 1.0182124741648693e+000
 2.1299999999999999e+000 1.0127150762876165e+000
 2.1400000000000001e+000 1.0061577125314352e+000
 2.1499999999999999e+000 9.9854859994696665e-001
 2.1600000000000001e+000 9.8989701868974489e-001
 2.1699999999999999e+000 9.8021330081801650e-001
 2.1800000000000002e+000 9.6950881807008094e-001
 2.1899999999999999e+000 9.5779596863431515e-001
 2.2000000000000002e+000 9.4508816292602194e-001
 2.2100000000000000e+000 9.3139980838620340e-001
 2.2200000000000002e+000 9.1674629331829227e-001
 2.2300000000000000e+000 9.0114396977981337e-001
 2.2400000000000002e+000 8.8461013554684276e-001
 2.2500000000000000e+000 8.6716301517003269e-001
 2.2600000000000002e+000 8.4882174014183087e-001
 2.2700000000000000e+000 8.2960632819538604e-001
 2.2800000000000002e+000 8.0953766175645447e-001
 2.2900000000000000e+000 7.8863746557043113e-001
 2.3000000000000003e+000 7.6692828352742370e-001
 2.3100000000000001e+000 7.4443345470903766e-001
 2.3199999999999998e+000 7.2117708868130159e-001
 2.3300000000000001e+000 6.9718404005885570e-001
 2.3399999999999999e+000 6.7247988236623280e-001
 2.3500000000000001e+000 6.4709088122272207e-001
 2.3599999999999999e+000 6.2104396687793573e-001
 2.3700000000000001e+000 5.9436670612582887e-001
 2.3799999999999999e+000 5.6708727362548750e-001
 2.3900000000000001e+000 5.3923442265756649e-001
 2.3999999999999999e+000 5.1083745534578873e-001
 2.4100000000000001e+000 4.8192619237339684e-001
 2.4199999999999999e+000 4.5253094222494761e-001
 2.4300000000000002e+000 4.2268246998424708e-001
 2.4399999999999999e+000 3.9241196571965020e-001
 2.4500000000000002e+000 3.6175101248832520e-001
 2.4600000000000000e+000 3.3073155399141080e-001
 2.4700000000000002e+000 2.9938586191233918e-001
 2.4800000000000000e+000 2.6774650297085145e-001
 2.4900000000000002e+000 2.3584630572549661e-001
 2.5000000000000000e+000 2.0371832715762547e-001
 2.5100000000000002e+000 1.7139581907005813e-001
 2.5200000000000000e+000 1.3891219433378743e-001
 2.5300000000000002e+000 1.0630099301616862e-001
 2.5400000000000000e+000 7.3595848424152871e-002
 2.5500000000000003e+000 4.0830453096173475e-002
 2.5600000000000001e+000 8.0385247762984335e-003
 2.5699999999999998e+000 -2.4746227595717124e-002
 2.5800000000000001e+000 -5.7490137844922573e-002
 2.5899999999999999e+000 -9.0159616438709789e-002
 2.6000000000000001e+000 -1.2272118424801615e-001
 2.6099999999999999e+000 -1.5514150615895586e-001
 2.6200000000000001e+000 -1.8738742450180629e-001
 2.6299999999999999e+000 -2.1942599226452117e-001
 2.6400000000000001e+000 -2.5122450605811975e-001
 2.6499999999999999e+000 -2.8275053880163681e-001
 2.6600000000000001e+000 -3.1397197209458322e-001
 2.6699999999999999e+000 -3.4485702824521458e-001
 2.6800000000000002e+000 -3.7537430192326982e-001
 2.6899999999999999e+000 -4.0549279140623601e-001
 2.7000000000000002e+000 -4.3518192938862577e-001
 2.7100000000000000e+000 -4.6441161332420555e-001
 2.7200000000000002e+000 -4.9315223527159363e-001
 2.7300000000000000e+000 -5.2137471121418866e-001
 2.7400000000000002e+000 -5.4905050982587844e-001
 2.7500000000000000e+000 -5.7615168065460531e-001
 2.7600000000000002e+000 -6.0265088169642389e-001
 2.7700000000000000e+000 -6.2852140633332643e-001
 2.7800000000000002e+000 -6.5373720960875437e-001
 2.7900000000000000e+000 -6.7827293381539122e-001
 2.8000000000000003e+000 -7.0210393337053123e-001
 2.8100000000000001e+000 -7.2520629895503963e-001
 2.8199999999999998e+000 -7.4755688089265993e-001
 2.8300000000000001e+000 -7.6913331174721700e-001
 2.8399999999999999e+000 -7.8991402811601052e-001
 2.8500000000000001e+000 -8.0987829159854796e-001
 2.8599999999999999e+000 -8.2900620892056898e-001
 2.8700000000000001e+000 -8.4727875119417528e-001
 2.8799999999999999e+000 -8.6467777229574105e-001
 2.8900000000000001e+000 -8.8118602634416998e-001
 2.8999999999999999e+000 -8.9678718426296211e-001
 2.9100000000000001e+000 -9.1146584941047659e-001
 2.9199999999999999e+000 -9.2520757226369754e-001
 2.9300000000000002e+000 -9.3799886414178024e-001
 2.9399999999999999e+000 -9.4982720995657954e-001
 2.9500000000000002e+000 -9.6068107997836327e-001
 2.9600000000000000e+000 -9.7054994060588329e-001
 2.9700000000000002e+000 -9.7942426413096428e-001
 2.9800000000000000e+000 -9.8729553748878329e-001
 2.9900000000000002e+000 -9.9415626998600959e-001
 3.0000000000000000e+000 -9.9999999999999989e-001
 3.0100000000000002e+000 -1.0048213006432629e+000
 3.0200000000000000e+000 -1.0086157843884289e+000
 3.0300000000000002e+000 -1.0113801066500054e+000
 3.0400000000000000e+000 -1.0131119683202148e+000
 3.0500000000000003e+000 -1.0138101172572640e+000
 3.0600000000000001e+000 -1.0134743487254212e+000
 3.0700000000000003e+000 -1.0121055047873189e+000
 3.0800000000000001e+000 -1.0097054726499322e+000
 3.0899999999999999e+000 -1.0062771819667229e+000
 3.1000000000000001e+000 -1.0018246010994616e+000
 3.1099999999999999e+000 -9.9635273234427668e-001
 3.1200000000000001e+000 -9.8986760612749147e-001
 3.1299999999999999e+000 -9.8237627417782858e-001
 3.1400000000000001e+000 -9.7388680168257646e-001
 3.1499999999999999e+000 -9.6440825843630040e-001
 3.1600000000000001e+000 -9.5395070899168122e-001
 3.1699999999999999e+000 -9.4252520182304078e-001
 3.1800000000000002e+000 -9.3014375751408540e-001
 3.1899999999999999e+000 -9.1681935598235498e-001
 3.2000000000000002e+000 -9.0256592275381153e-001
 3.2100000000000000e+000 -8.8739831430193106e-001
 3.2200000000000002e+000 -8.7133230246658289e-001
 3.2300000000000000e+000 -8.5438455796886736e-001
 3.2400000000000002e+000 -8.3657263303899354e-001
 3.2500000000000000e+000 -8.1791494317511404e-001
 3.2600000000000002e+000 -7.9843074805190983e-001
 3.2700000000000000e+000 -7.7814013159852546e-001
 3.2800000000000002e+000 -7.5706398126628238e-001
 3.2900000000000000e+000 -7.3522396650737554e-001
 3.3000000000000003e+000 -7.1264251648652888e-001
 3.3100000000000001e+000 -6.8934279704833989e-001
 3.3200000000000003e+000 -6.6534868696373928e-001
 3.3300000000000001e+000 -6.4068475347973064e-001
 3.3399999999999999e+000 -6.1537622719721541e-001
 3.3500000000000001e+000 -5.8944897630237114e-001
 3.3599999999999999e+000 -5.6292948017767885e-001
 3.3700000000000001e+000 -5.3584480241928634e-001
 3.3799999999999999e+000 -5.0822256328797000e-001
 3.3900000000000001e+000 -4.8009091162149786e-001
 3.3999999999999999e+000 -4.5147849623671688e-001
 3.4100000000000001e+000 -4.2241443685017976e-001
 3.4199999999999999e+000 -3.9292829454655714e-001
 3.4300000000000002e+000 -3.6305004182456568e-001
 3.4399999999999999e+000 -3.3281003225048567e-001
 3.4500000000000002e+000 -3.0223896974974707e-001
 3.4600000000000000e+000 -2.7136787756739156e-001
 3.4700000000000002e+000 -2.4022806692852802e-001
 3.4800000000000000e+000 -2.0885110543017968e-001
 3.4900000000000002e+000 -1.7726878519616551e-001
 3.5000000000000000e+000 -1.4551309082687436e-001
 3.5100000000000002e+000 -1.1361616717598702e-001
 3.5200000000000000e+000 -8.1610286986314756e-002
 3.5300000000000002e+000 -4.9527818417107461e-002
 3.5400000000000000e+000 -1.7401192495210105e-002
 3.5500000000000003e+000 1.4737129477461586e-002
 3.5600000000000001e+000 4.6854688527640631e-002
 3.5700000000000003e+000 7.8919060593147156e-002
 3.5800000000000001e+000 1.1089788905160744e-001
 3.5899999999999999e+000 1.4275891716308831e-001
 3.6000000000000001e+000 1.7447002039438750e-001
 3.6099999999999999e+000 2.0599923859284403e-001
 3.6200000000000001e+000 2.3731480797773785e-001
 3.6299999999999999e+000 2.6838519291748530e-001
 3.6400000000000001e+000 2.9917911746112891e-001
 3.6499999999999999e+000 3.2966559659284506e-001
 3.6600000000000001e+000 3.5981396717849901e-001
 3.6699999999999999e+000 3.8959391857359504e-001
 3.6800000000000002e+000 4.1897552286232587e-001
 3.6899999999999999e+000 4.4792926469780625e-001
 3.7000000000000002e+000 4.7642607071399135e-001
 3.7100000000000000e+000 5.0443733848020866e-001
 3.7200000000000002e+000 5.3193496496973780e-001
 3.7300000000000000e+000 5.5889137451431292e-001
 3.7400000000000002e+000 5.8527954621700429e-001
 3.7500000000000000e+000 6.1107304079645652e-001
 3.7600000000000002e+000 6.3624602683604814e-001
 3.7700000000000000e+000 6.6077330641214149e-001
 3.7800000000000002e+000 6.8463034007622381e-001
 3.7900000000000000e+000 7.0779327116639346e-001
 3.8000000000000003e+000 7.3023894942432621e-001
 3.8100000000000001e+000 7.5194495389454696e-001
 3.8200000000000003e+000 7.7288961508359066e-001
 3.8300000000000001e+000 7.9305203635732835e-001
 3.8399999999999999e+000 8.1241211455555562e-001
 3.8500000000000001e+000 8.3095055980369592e-001
 3.8599999999999999e+000 8.4864891450229185e-001
 3.8700000000000001e+000 8.6548957147577876e-001
 3.8799999999999999e+000 8.8145579126288276e-001
 3.8900000000000001e+000 8.9653171853184566e-001
 3.8999999999999999e+000 9.1070239760455041e-001
 3.9100000000000001e+000 9.2395378707453457e-001
 3.9199999999999999e+000 9.3627277350475191e-001
 3.9300000000000002e+000 9.4764718419189919e-001
 3.9399999999999999e+000 9.5806579898503363e-001
 3.9500000000000002e+000 9.6751836114716694e-001
 3.9600000000000000e+000 9.7599558724946778e-001
 3.9700000000000002e+000 9.8348917608867603e-001
 3.9800000000000000e+000 9.8999181661930802e-001
 3.9900000000000002e+000 9.9549719489320831e-001
 4.0000000000000000e+000 1.0000000000000002e+000
 4.0099999999999998e+000 1.0034959290029808e+000
 4.0200000000000005e+000 1.0059816908660018e+000
 4.0300000000000002e+000 1.0074550093678900e+000
 4.0400000000000000e+000 1.0079146250019630e+000
 4.0499999999999998e+000 1.0073602958592081e+000
 4.0600000000000005e+000 1.0057927974946990e+000
 4.0700000000000003e+000 1.0032139217778420e+000
 4.0800000000000001e+000 9.9962647472804478e-001
 4.0899999999999999e+000 9.9503427333841443e-001
 4.0999999999999996e+000 9.8944214139109343e-001
 4.1100000000000003e+000 9.8285590426883851e-001
 4.1200000000000001e+000 9.7528238276845258e-001
 4.1299999999999999e+000 9.6672938592265967e-001
 4.1399999999999997e+000 9.5720570283799744e-001
 4.1500000000000004e+000 9.4672109355728040e-001
 4.1600000000000001e+000 9.3528627895614813e-001
 4.1699999999999999e+000 9.2291292968416860e-001
 4.1799999999999997e+000 9.0961365416191553e-001
 4.1900000000000004e+000 8.9540198564636542e-001
 4.2000000000000002e+000 8.8029236837789071e-001
 4.2100000000000000e+000 8.6430014282301393e-001
 4.2199999999999998e+000 8.4744153002800604e-001
 4.2300000000000004e+000 8.2973361509926735e-001
 4.2400000000000002e+000 8.1119432982729955e-001
 4.2500000000000000e+000 7.9184243447192193e-001
 4.2599999999999998e+000 7.7169749872721005e-001
 4.2700000000000005e+000 7.5077988188544031e-001
 4.2800000000000002e+000 7.2911071222011603e-001
 4.2900000000000000e+000 7.0671186560891308e-001
 4.2999999999999998e+000 6.8360594341814362e-001
 4.3100000000000005e+000 6.5981624967102903e-001
 4.3200000000000003e+000 6.3536676752282650e-001
 4.3300000000000001e+000 6.1028213506648366e-001
 4.3399999999999999e+000 5.8458762049318314e-001
 4.3500000000000005e+000 5.5830909663275641e-001
 4.3600000000000003e+000 5.3147301489955812e-001
 4.3700000000000001e+000 5.0410637866997166e-001
 4.3799999999999999e+000 4.7623671611826868e-001
 4.3899999999999997e+000 4.4789205253807801e-001
 4.4000000000000004e+000 4.1910088217722363e-001
 4.4100000000000001e+000 3.8989213961413643e-001
 4.4199999999999999e+000 3.6029517070454292e-001
 4.4299999999999997e+000 3.3033970312749494e-001
 4.4400000000000004e+000 3.0005581656022418e-001
 4.4500000000000002e+000 2.6947391251165359e-001
 4.4600000000000000e+000 2.3862468384472632e-001
 4.4699999999999998e+000 2.0753908401801052e-001
 4.4800000000000004e+000 1.7624829607730613e-001
 4.4900000000000002e+000 1.4478370142822689e-001
 4.5000000000000000e+000 1.1317684842090213e-001
 4.5099999999999998e+000 8.1459420778178637e-002
 4.5200000000000005e+000 4.9663205898786311e-002
 4.5300000000000002e+000 1.7820063067080560e-002
 4.5400000000000000e+000 -1.4038108398956084e-002
 4.5499999999999998e+000 -4.5879401029718547e-002
 4.5600000000000005e+000 -7.7671931084237922e-002
 4.5700000000000003e+000 -1.0938387039695348e-001
 4.5800000000000001e+000 -1.4098347815930501e-001
 4.5899999999999999e+000 -1.7243913260451499e-001
 4.6000000000000005e+000 -2.0371936256407458e-001
 4.6100000000000003e+000 -2.3479287886452471e-001
 4.6200000000000001e+000 -2.6562860553335266e-001
 4.6299999999999999e+000 -2.9619571078299189e-001
 4.6399999999999997e+000 -3.2646363774215126e-001
 4.6500000000000004e+000 -3.5640213490396366e-001
 4.6600000000000001e+000 -3.8598128626073419e-001
 4.6699999999999999e+000 -4.1517154109538867e-001
 4.6799999999999997e+000 -4.4394374340007758e-001
 4.6900000000000004e+000 -4.7226916089275950e-001
 4.7000000000000002e+000 -5.0011951360303020e-001
 4.7100000000000000e+000 -5.2746700199884311e-001
 4.7199999999999998e+000 -5.5428433462629556e-001
 4.7300000000000004e+000 -5.8054475523510340e-001
 4.7400000000000002e+000 -6.0622206936292922e-001
 4.7500000000000000e+000 -6.3129067035226516e-001
 4.7599999999999998e+000 -6.5572556477414978e-001
 4.7700000000000005e+000 -6.7950239723359174e-001
 4.7800000000000002e+000 -7.0259747453220101e-001
 4.7900000000000000e+000 -7.2498778916416018e-001
 4.7999999999999998e+000 -7.4665104212237232e-001
 4.8100000000000005e+000 -7.6756566499226320e-001
 4.8200000000000003e+000 -7.8771084131147961e-001
 4.8300000000000001e+000 -8.0706652717443306e-001
 4.8399999999999999e+000 -8.2561347106140437e-001
 4.8500000000000005e+000 -8.4333323287270046e-001
 4.8600000000000003e+000 -8.6020820214914939e-001
 4.8700000000000001e+000 -8.7622161546103594e-001
 4.8799999999999999e+000 -8.9135757294841000e-001
 4.8899999999999997e+000 -9.0560105399654500e-001
 4.9000000000000004e+000 -9.1893793203120422e-001
 4.9100000000000001e+000 -9.3135498841922071e-001
 4.9199999999999999e+000 -9.4283992546082496e-001
 4.9299999999999997e+000 -9.5338137846103677e-001
 4.9400000000000004e+000 -9.6296892686836455e-001
 4.9500000000000002e+000 -9.7159310446998937e-001
 4.9600000000000000e+000 -9.7924540863354237e-001
 4.9699999999999998e+000 -9.8591830858654406e-001
 4.9800000000000004e+000 -9.9160525272552591e-001
 4.9900000000000002e+000 -9.9630067494782215e-001
 5.0000000000000000e+000 -9.9999999999999989e-001

periodic_deltaF.txt Sample from the above. The x for which y not in [-1, 1]:

 1.0000000000000000e-002
 2.0000000000000000e-002
 2.9999999999999999e-002
 4.0000000000000001e-002
 5.0000000000000003e-002
 5.9999999999999998e-002
 7.0000000000000007e-002
 8.0000000000000002e-002
 8.9999999999999997e-002
 1.0000000000000001e-001
 1.1000000000000000e-001
 1.2000000000000000e-001
 1.3000000000000000e-001
 1.4000000000000001e-001
 1.4999999999999999e-001
 1.6000000000000000e-001
 1.7000000000000001e-001
 1.7999999999999999e-001
 1.9000000000000000e-001
 2.0000000000000001e-001
 2.0999999999999999e-001
 2.2000000000000000e-001
 2.3000000000000001e-001
 2.3999999999999999e-001
5

Here is one possible way to do that.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{backgrounds}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
    \centering
    \begin{axis}[
        xlabel = $ka / \pi$,
        xmin = -0, xmax = 5,
        ymin = -2, ymax = 3
    ]

    \addplot [color = red,name path=wave] table[x index = 0, y index = 1] {data/periodic_delta.txt};
     %\addplot [color = blue] table[x index = 0, variable = \y , domain = -1 : 1, smooth] plot ({data/periodic_deltaF.txt}, {\y});

    \addplot [name path=upper,black, dashed, variable = \x , domain = 0 : 5, smooth] plot ({\x},{1});   
    \addplot [name path=lower,black, dashed, variable = \x , domain = 0 : 5, smooth] plot ({\x},{-1});
    \path[name intersections={of=wave and upper,name=upper,total=\n}]
    \pgfextra{\xdef\TotNum{\n}};
    \path[name intersections={of=wave and lower,name=lower}];
    \addlegendentry{$P\frac{\sin(ka)}{ka} + \cos(ka)$}; 
    \addlegendentry{$x = 1$};
    \addlegendentry{$x = -1$};
    \end{axis}
    \begin{scope}[on background layer]
    \foreach \X in {1,...,\TotNum}
    \fill[blue!20] (upper-\X) rectangle (lower-\X);
    \end{scope}
\end{tikzpicture} 
\end{document}

enter image description here

  • 1
    \usepgfplotslibrary{fillbetween} is not necessary here. I loaded it because I tried something else first, but I ended up with a solution that does not rely on this nice library. All my attempts to achieve the same within the axis environment lead to slightly more complex codes because the order in which pgfplots expands and does things is a bit nontrivial. – user121799 Sep 13 '18 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.