Hi,
I have the attached data (conc.mat) where each row corresponds to y axis value for a given x value(t=[0;30;60;120;300;600;900]). I am doing uncertainty analysis here. I also have the actual data set (single y value for single x value), which should construct a firm plot in the same graph. So, in summary, I should have a firm line from the actual data set which I have not shared here, but also I should be able to generate a shaded area from the attached data. The final product should be something like the attached file (Capture.JPG). Please suggest some solutions. Thanks.

 採用された回答

Star Strider
Star Strider 2021 年 10 月 22 日

0 投票

Try this —
LD = load('conc.mat');
conc = LD.conc;
t=[0;30;60;120;300;600;900];
figure
plot(t, conc)
hold on
shadeWidth = 0.01;
patch([t; flipud(t)], [min(conc,[],2)-shadeWidth; flipud(max(conc,[],2))+shadeWidth], [1 1 1]*0.5, 'FaceAlpha',0.25, 'EdgeColor','none')
hold off
Experiment to get different results.
.

12 件のコメント

Shahriar Mahmud
Shahriar Mahmud 2021 年 10 月 22 日
THanks a lot. It worked. But, how do I remove the firm lines in the middle (see attached), I just want the shade. I am gonna plot the firm line in the middle with a different set of data later.
Also, how do I change the shade color, thanks again.
Star Strider
Star Strider 2021 年 10 月 22 日
My pleasure!
That is straightforward — just don’t plot them (the posted example had the centre trace, so I thought it was to be included) —
LD = load('conc.mat');
conc = LD.conc;
t=[0;30;60;120;300;600;900];
figure
% plot(t, conc)
hold on
shadeWidth = 0.01;
patch([t; flipud(t)], [min(conc,[],2)-shadeWidth; flipud(max(conc,[],2))+shadeWidth], [1 1 1]*0.5, 'FaceAlpha',0.25, 'EdgeColor','none')
hold off
As for the colour, choose whatever colour that can be defined. See C and FaceColor for some of them. Search the documentation for other options.
.
Shahriar Mahmud
Shahriar Mahmud 2021 年 10 月 27 日
I have anothr question, if you look at the last image I attached, the shading is not smooth. How can I smooth the shading?
Thanks.
Star Strider
Star Strider 2021 年 10 月 27 日
I do not understand.
The data in the file are plotted correctly, and the shaded portion extends from a given distance below the lowest data to the same distance above the highest data.
To plot it around the mean instead —
patch([t; flipud(t)], [mean(conc,2)-shadeWidth; flipud(mean(conc,2))+shadeWidth], [1 1 1]*0.5, 'FaceAlpha',0.25, 'EdgeColor','none')
or the median
patch([t; flipud(t)], [median(conc,2)-shadeWidth; flipud(median(conc,2))+shadeWidth], [1 1 1]*0.5, 'FaceAlpha',0.25, 'EdgeColor','none')
Those vectors are directly derived from the data, and the ‘shadeWidth’ is constant for the entire line. It may not appear to be constant because ‘shadeWidth’ is plotted in the ‘y’ direction, and that will appear different in the first part of the curve where the slope is greatest than in the last part of the curve where the slope is least. It is neverhteless the same throughout the curve.
Consider a sine curve —
x = linspace(-pi, pi);
s = sin(x);
figure
plot(x,s,'-r')
hold on
patch([x flip(x)], [s-0.1 flip(s)+0.1], 'b', 'FaceAlpha',0.25)
hold off
grid
xlim([min(x) max(x)])
The difference between the curves is the same throughout, however the shading appears to be thicker at the extreme ‘y’ values than it does near 0.
.
Shahriar Mahmud
Shahriar Mahmud 2021 年 11 月 8 日
編集済み: Shahriar Mahmud 2021 年 11 月 8 日
Hi,
I was wondering how can I add multiple patches in one plot. Something like the one attached.
Thanks.
Note: Different patches may have different vector lengths (t, conc)
Star Strider
Star Strider 2021 年 11 月 8 日
Sure!
x1 = 0:7;
y1 = 1-exp(-0.5*x1) + 0.04 + randn(size(x1))*0.035;
s1 = 0.04*[-1;1] + y1;
x2 = 0:9;
y2 = 1-exp(-0.2*x2) + 0.08 + randn(size(x2))*0.035;
s2 = 0.08*[-1;1] + y2;
figure
patch([x1 flip(x1)], [s1(1,:) flip(s1(2,:))], 'r', 'FaceAlpha',0.2, 'EdgeColor','none')
hold on
hp1 = plot(x1, y1, '-r', 'LineWidth',2, 'DisplayName','Function #1');
patch([x2 flip(x2)], [s2(1,:) flip(s2(2,:))], 'b', 'FaceAlpha',0.2, 'EdgeColor','none')
hp2 = plot(x2, y2, '-b', 'LineWidth',2, 'DisplayName','Function #2');
hold off
grid
legend([hp1; hp2], 'Location','best')
xlim([0 10])
Use the data available in place of the data I created for the plot. This does not automatically assign the respective colours (although it could be changed to do that if required), so that must be hard-coded.
.
Shahriar Mahmud
Shahriar Mahmud 2021 年 11 月 17 日
Hi, in terms of smoothing the curve, I saw your response to a post where you suggested using interp1 function (see link below). Do we have something for the shaded portion to make it smooth around the edges?
Thanks.
Star Strider
Star Strider 2021 年 11 月 17 日
It depends on the data, the noise spectrum, and the desired result. The interp1 function is an option in some instances, however if the data are noisy, the smoothdata function (versions >= R2017a) may be a better choice.
Shahriar Mahmud
Shahriar Mahmud 2021 年 11 月 17 日
I have max 7 data points for each plot. Please see attached screenshots of plots with and without using smoothdata function . Ofcourse the smoothdata function didnt work very well.
Also, while smoothdata is not working well, I dont know how to use it for pathc i.e. the shaded portion.
Thanks a lot for your continuous support, highly appreciate it.
Star Strider
Star Strider 2021 年 11 月 17 日
As always, my pleasure!
There is not much noise, so smoothdata is likely not effective.
To create more points (that would appear to be desired), my favourite approach, assuming that ‘x’ is the independent variable and ‘y’ is the dependent variable, is:
N = 250;
xq = linspace(min(x), max(x), N);
yq = interp1(x, y, xq, 'pchip');
figure
plot(xq, yq)
grid
The ‘pchip’ interpolation method creates a smooth, nonlinear interpolation, without all the wild extremes that a spline interpolation would produce. It is the one I usually use for such problems. Explore other options as well, to get the desired result.
Using the interpolated values should not require changing the rest of the original code.
.
Shahriar Mahmud
Shahriar Mahmud 2021 年 11 月 17 日
Thanks again.
It looks ok to me for the time being.
Please see attached. As you can see the shaded portions created by the patch function (See code above you suggested) needs to be smoothened as well. Any suggestions?
Star Strider
Star Strider 2021 年 11 月 17 日
As always, my pleasure!
.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by