Fill std error for one curve

21 views (last 30 days)
JB
JB on 27 Mar 2018
Answered: Patrick Haus on 4 Aug 2023
I have this (X,Y) dataset and I want to plot with the std.error marked as shading. However, I have have problem when the std is 0. Here is a simple code is use for testing:
x = Data(:,1);
y = Data(:,2);
dy = 0.3*y;
fill([x;flipud(x)],[y-dy;flipud(y+dy)],[.2 .9 .9],'linestyle','none');
line(x,y);
alpha(0.1);
The dy is my std. error which is working fine, but when I change this to 0 it is looks strange (it should be equal the line)
Here is my dataset:
Data = [
260.0000 0
259.5000 -0.0166
259.0000 -0.0487
258.5000 -0.0445
258.0000 -0.0437
257.5000 -0.0638
257.0000 -0.0583
256.5000 -0.0880
256.0000 -0.0961
255.5000 -0.0706
255.0000 -0.0863
254.5000 -0.1051
254.0000 -0.1140
253.5000 -0.1329
253.0000 -0.1307
252.5000 -0.1433
252.0000 -0.1625
251.5000 -0.1366
251.0000 -0.1359
250.5000 -0.1413
250.0000 -0.1438
249.5000 -0.1538
249.0000 -0.1352
248.5000 -0.1844
248.0000 -0.2098
247.5000 -0.2066
247.0000 -0.2031
246.5000 -0.2036
246.0000 -0.2479
245.5000 -0.2791
245.0000 -0.3187
244.5000 -0.3629
244.0000 -0.4218
243.5000 -0.5147
200.0000 2.5618
199.5000 3.9747
199.0000 5.4836
198.5000 7.0462
198.0000 8.4347
197.5000 9.7647
197.0000 11.1262
196.5000 12.2604
196.0000 13.3529
195.5000 14.4072
195.0000 15.3222
194.5000 16.1851
194.0000 16.9095
193.5000 17.4813
193.0000 17.8846
192.5000 18.1166
192.0000 18.3644
191.5000 18.5597
191.0000 18.5822
190.5000 18.5643
190.0000 18.3095
189.5000 17.9620
189.0000 17.6198
188.5000 17.1708
188.0000 16.8142
187.5000 16.4826
187.0000 16.1231
186.5000 15.1229
186.0000 14.6209
185.5000 13.4553
185.0000 12.5914
184.5000 10.4794
184.0000 8.5036
183.5000 6.4980
183.0000 4.7882
182.5000 3.7990
182.0000 3.4504
181.5000 0.5280
181.0000 -0.9536
180.5000 -3.2450
180.0000 -4.9457]

Answers (2)

Pawel Jastrzebski
Pawel Jastrzebski on 27 Mar 2018
Edited: Pawel Jastrzebski on 27 Mar 2018
Look at you dy. I think you've changed it to dy = y. This is when you get a weird shape.
See line 80:
When dy = 0, you actually get a line:
  4 Comments
JB
JB on 27 Mar 2018
That is strange. I still get the same wrong graph as showen above. I am using matlab 2015.
Pawel Jastrzebski
Pawel Jastrzebski on 28 Mar 2018

OK, try this. Maybe with this code you'll be able to track down what's plotting and how. The changes I've made are listed below, it's mostly cosmetics, but maybe Matlab will read this syntax better and produce the right plot for you:

  • Moved out the x,y vales for the fill to a separate variables for readability, it's no xfill and yfill
  • Used xfill and yfill to plot the chart using the plot function and dashed red line to see how it plots - this is the boundary of your fill plot
  • added figure to initiate a new figure
  • added hold on to keep all of your plots in a newly created figure
  • swapped your line plot for plot plot
  • added hold off at the end of the figure
x = Data(:,1);
y = Data(:,2);
dy = 0.3*y; % or dy = 0, it's the same thing
xfill = [x; flipud(x)];
yfill = [y-dy; flipud(y+dy)];
figure
plot(xfill,yfill,'--r')
hold on
fill(xfill,yfill,[1 0 0],'LineStyle','none');
plot(x,y,'k.');
alpha(0.1);
title('std.dev = 0.3')
hold off

The output:

Sign in to comment.


Patrick Haus
Patrick Haus on 4 Aug 2023
Did you ever solve this?
I was trying to create the exact same error plotting routine and stumbled on this issue using Matlab 2015b. I happened to notice that it works as expected in Matlab 2022b. I had varied success in 2015b by using epsilon (eps) instead of 0 for your dy.

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!