Plot scatter plot as shade
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
2021년 10월 22일
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
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
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
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.
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.
.
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
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
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
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
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
2021년 11월 17일
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)
