12

I am trying to color the area under a plot between x = 500 to x = 1000. I am not sure how to get the colored area to show up just under the curve within those limits. Can you help me color the area under the curve between x = 500 to x = 1000?

Here is my code:

\documentclass{book}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.10}
\pgfplotsset{minor grid style={dotted,gray!50}}
\pgfplotsset{major grid style={gray!50!black}}

\newcommand{\datafile}{Noise2.dat}

\begin{filecontents*}{\datafile}
  Freq   Orig   Filt
100.000, -79.374, -119.392
101.801, -79.858, -119.321
103.634, -78.587, -117.497
105.501, -79.077, -117.437
107.401, -78.735, -116.547
109.335, -81.799, -119.065
111.304, -82.298, -119.021
113.309, -82.329, -118.511
115.349, -81.744, -117.389
117.427, -82.113, -117.223
119.542, -83.602, -118.180
121.695, -83.097, -117.147
123.886, -83.461, -116.985
126.117, -82.194, -115.196
128.389, -83.418, -115.902
130.701, -83.187, -115.155
133.055, -83.285, -114.742
135.451, -83.793, -114.742
137.891, -84.710, -115.156
140.374, -85.018, -114.964
142.902, -85.918, -115.369
145.476, -84.668, -113.628
148.096, -83.836, -112.310
150.763, -84.068, -112.061
153.478, -84.302, -111.818
156.242, -84.040, -111.085
159.056, -82.969, -109.548
161.921, -82.205, -108.324
164.837, -83.149, -108.813
167.806, -83.943, -109.158
170.828, -83.769, -108.542
173.904, -84.410, -108.747
177.036, -85.590, -109.498
180.225, -84.582, -108.067
183.470, -87.734, -110.804
186.775, -86.326, -108.989
190.138, -84.893, -107.156
193.563, -86.280, -108.151
197.049, -87.832, -109.319
200.598, -87.776, -108.888
204.210, -87.978, -108.725
207.888, -86.935, -107.325
211.632, -87.181, -107.223
215.443, -88.478, -108.183
219.324, -87.294, -106.672
223.274, -85.716, -104.777
227.295, -85.793, -104.547
231.388, -87.389, -105.848
235.555, -87.906, -106.081
239.798, -87.386, -105.289
244.116, -88.130, -105.772
248.513, -87.121, -104.515
252.988, -87.494, -104.652
257.545, -87.507, -104.441
262.183, -86.936, -103.660
266.905, -86.413, -102.939
271.712, -85.294, -101.635
276.605, -84.552, -100.722
281.587, -83.760, -99.771
286.658, -81.666, -97.533
291.821, -80.942, -96.678
297.076, -81.625, -97.243
302.427, -81.497, -97.011
307.873, -78.332, -93.755
313.418, -78.452, -93.798
319.063, -81.117, -96.400
324.809, -81.431, -96.663
330.659, -80.621, -95.816
336.614, -82.433, -97.603
342.676, -82.169, -97.328
348.848, -79.901, -95.061
355.130, -80.383, -95.556
361.526, -81.490, -96.688
368.037, -82.843, -98.078
374.665, -84.314, -99.597
381.413, -81.159, -96.502
388.282, -83.309, -98.723
395.275, -81.743, -97.238
402.394, -81.915, -97.501
409.641, -82.141, -97.828
417.018, -82.823, -98.621
424.529, -82.646, -98.563
432.174, -83.165, -99.211
439.958, -83.784, -99.967
447.881, -83.598, -99.927
455.947, -84.321, -100.803
464.159, -82.783, -99.426
472.518, -83.921, -100.731
481.028, -85.274, -102.259
489.691, -84.604, -101.770
498.511, -85.588, -102.942
507.489, -84.458, -102.006
516.628, -84.730, -102.477
525.933, -85.516, -103.467
535.405, -83.314, -101.475
545.047, -85.678, -104.054
554.863, -84.979, -103.575
564.856, -85.488, -104.307
575.029, -85.218, -104.265
585.385, -86.101, -105.381
595.928, -84.617, -104.133
606.661, -84.431, -104.186
617.586, -85.522, -105.520
628.709, -86.590, -106.834
640.032, -86.826, -107.320
651.559, -87.389, -108.135
663.293, -86.232, -107.233
675.239, -87.076, -108.335
687.400, -87.267, -108.786
699.780, -86.034, -107.815
712.382, -86.262, -108.308
725.212, -86.480, -108.793
738.273, -84.688, -107.270
751.569, -85.615, -108.468
765.105, -86.937, -110.063
778.884, -85.851, -109.251
792.912, -85.930, -109.606
807.192, -86.156, -110.110
821.729, -86.489, -110.722
836.528, -87.787, -112.301
851.594, -87.913, -112.709
866.931, -85.959, -111.038
882.544, -86.837, -112.200
898.439, -86.963, -112.612
914.619, -86.759, -112.694
931.091, -87.360, -113.583
947.860, -86.890, -113.402
964.931, -87.240, -114.041
982.309, -87.019, -114.111
1000.000, -85.349, -112.732
1018.010, -86.757, -114.432
1036.344, -86.677, -114.645
1055.008, -86.228, -114.490
1074.009, -87.104, -115.660
1093.351, -87.293, -116.144
1113.042, -87.722, -116.869
1133.088, -87.269, -116.712
1153.494, -88.248, -117.988
1174.269, -87.315, -117.353
1195.417, -88.085, -118.421
1216.946, -88.050, -118.684
1238.863, -88.966, -119.899
1261.175, -88.435, -119.668
1283.888, -88.282, -119.815
1307.010, -88.859, -120.693
1330.549, -88.324, -120.459
1354.512, -87.446, -119.882
1378.907, -87.432, -120.170
1403.740, -87.666, -120.706
1429.021, -88.099, -121.442
1454.758, -88.533, -122.180
1480.958, -88.338, -122.288
1507.629, -88.343, -122.598
1534.781, -88.103, -122.663
1562.422, -88.478, -123.343
1590.561, -89.310, -124.482
1619.207, -89.579, -125.058
1648.368, -89.152, -124.938
1678.055, -89.278, -125.373
1708.276, -89.724, -126.129
1739.042, -90.204, -126.920
1770.362, -90.563, -127.591
1802.246, -89.939, -127.281
1834.704, -90.099, -127.757
1867.746, -90.580, -128.556
1901.384, -90.793, -129.089
1935.627, -90.604, -129.224
1970.487, -90.073, -129.020
2005.975, -88.273, -127.551
2042.102, -91.418, -131.032
2078.880, -91.760, -131.716
2116.320, -91.445, -131.750
2154.435, -91.854, -132.516
2193.236, -92.113, -133.141
2232.735, -92.505, -133.911
2272.946, -92.729, -134.526
2313.881, -93.382, -135.585
2355.554, -92.567, -135.194
2397.977, -93.718, -136.791
2441.164, -94.062, -137.604
2485.128, -94.297, -138.336
2529.885, -94.376, -138.944
2575.448, -95.170, -140.304
2621.831, -95.128, -140.867
2669.049, -95.364, -141.755
2717.118, -96.018, -143.110
2766.053, -96.379, -144.227
2815.869, -96.767, -145.430
2866.582, -96.727, -146.268
2918.208, -97.296, -147.780
2970.764, -95.765, -147.261
3024.267, -94.212, -146.788
3078.733, -98.685, -152.410
3134.181, -99.689, -154.632
3190.627, -99.969, -156.195
3248.089, -100.469, -158.041
3306.586, -100.644, -159.622
3366.137, -100.629, -161.068
3426.760, -101.562, -163.512
3488.475, -102.363, -165.870
3551.302, -102.167, -167.272
3615.260, -101.974, -168.714
3680.370, -102.055, -170.462
3746.652, -102.891, -172.993
3814.129, -103.815, -175.637
3882.820, -103.469, -177.031
3952.749, -103.874, -179.195
4023.937, -104.056, -181.152
4096.407, -104.680, -183.563
4170.182, -105.183, -185.865
4245.286, -105.044, -187.535
4321.743, -105.541, -189.849
4399.576, -105.822, -191.954
4478.812, -106.187, -194.148
4559.474, -105.990, -195.786
4641.589, -106.222, -197.856
4725.183, -106.961, -200.437
4810.282, -107.153, -202.474
4896.914, -107.402, -204.570
4985.106, -107.350, -206.368
5074.887, -107.841, -208.710
5166.284, -108.540, -211.261
5259.328, -108.177, -212.752
5354.047, -108.851, -215.281
5450.472, -108.463, -216.748
5548.634, -109.168, -219.310
5648.563, -109.327, -221.326
5750.292, -110.141, -223.997
5853.854, -110.367, -226.081
5959.280, -110.150, -227.723
6066.605, -111.069, -230.500
6175.863, -110.857, -232.147
6287.089, -111.135, -234.284
6400.318, -111.114, -236.122
6515.586, -111.620, -238.488
6632.930, -111.630, -240.357
6752.388, -111.945, -242.532
6873.996, -112.445, -244.892
6997.795, -112.978, -247.284
7123.824, -113.114, -249.280
7252.122, -113.040, -251.066
7382.731, -113.181, -253.067
7515.692, -113.406, -255.152
7651.048, -113.909, -257.515
7788.842, -114.505, -259.972
7929.117, -114.056, -261.383
8071.918, -114.336, -263.523
8217.291, -115.112, -266.159
8365.283, -115.076, -267.983
8515.939, -115.691, -270.458
8669.309, -115.120, -271.748
8825.441, -115.586, -274.074
8984.385, -115.800, -276.148
9146.192, -116.421, -278.629
9310.913, -116.636, -280.705
9478.600, -116.705, -282.634
9649.307, -117.236, -285.025
9823.089, -117.063, -286.713
10000.000, -117.204, -288.714
\end{filecontents*}
% http://tex.stackexchange.com/questions/164991/pgfplots-how-to-fill-bounded-area-under-a-curve-using-addplot-and-fill?rq=1
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[every plot/.append style={very thick}]
\begin{semilogxaxis}[width=14cm,height=10cm,
                    grid = both,
                    every major grid/.style={gray, opacity=0.7},
                    ymin =  -130,
                    ymax = -70,
                    title = {},
                    xlabel={\emph{Offset Frequency (Hz)}},
                    ylabel={\emph{Noise}},
                    extra y ticks = {-70}]

\addplot+ [smooth,blue,name path=A] table[mark = none, x index=0,y index=1,col sep=comma] {\datafile};
\addplot+[draw=none,name path=B] {0}; 
\addplot+[gray] fill between[of=A and B,soft clip={domain=500:1000}];
\end{semilogxaxis}
\end{tikzpicture}
\caption{Response.}
\end{figure}
\end{document} 
1
  • 1
    Remember that the default domain is -5:5, so your zero-line is plotted far to the left of your data. It is also placed above, because all your values are negative. Dec 17, 2015 at 8:31

1 Answer 1

15

The graph with "0" is out-of-bound in your plot ;-). If you change the second addplot+ to

\addplot+[draw=none,name path=B, domain=500:1000, mark=none] {-130}; 

You'll have your shading.

Result

...but BTW, if you use a fill opacity option:

\addplot+[gray, fill opacity=0.4] fill between[of=A and B,soft clip={domain=500:1000}];

the result is much more sexy (in my opinion, at least).

BTW: see also Shaded area under pgfplot, starting with first data point, outputs wrong shaded area

3
  • Thanks for your solution. I like your approach with the fill opacity. I was trying to modify the limits from 100:3000 by using these commands: \addplot+ [draw=none,name path=B, domain=100:3000, mark=none] {-130}; and \addplot+ [gray, fill opacity=0.4] fill between[of=A and B,soft clip={domain=100:3000}]; For some reason, the shaded region did not look right. It was angled to the right. The shaded box basically had coordinates (100, -130), (3000,-130), (10000,-117.204) and (3000, -94). Is this due to the fact that the data file starts from 100? If so, what is the work around?
    – Joe
    Dec 17, 2015 at 16:45
  • @Joe, I think this is fodder for another question... put a MWE there with an image and a link here too. I'll have a look at it anyway.
    – Rmano
    Dec 17, 2015 at 19:18
  • Seems a bug to me. Definitely post a question. Anyway, using soft clip={domain=100.01:3000} works around the problem. @torbjørn-t?
    – Rmano
    Dec 17, 2015 at 19:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .