Plot scatter plot as shade
显示 更早的评论
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.
采纳的回答
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 个评论
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.
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.
.
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.
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
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)
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.
.
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.
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.
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.
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.
.
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?
As always, my pleasure!
.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
