I would like to insert a vertical bar (light blue) from Time = 500 to Time = 650 to highlight that section of the curve. I believe patch is the tool to use. I followed examples provided on MathWorks, but my curve disappears when I use the recommended code. How can I incorporate patch into the code below?
plot(t,B);
xlabel('Time (in days)');
ylabel('Mosquitoes Searching for a Blood Meal');
h_xlabel = get(gca,'XLabel');
set(h_xlabel,'FontSize',14);
h_ylabel = get(gca,'YLabel');
set(h_ylabel,'FontSize',14);
set(gca,'fontName','Helvetica');
hold on;
I know that I should use patch before plotting my curve so that the vertical bar does not cover the curve.

 채택된 답변

Star Strider
Star Strider 2018년 3월 10일

0 개 추천

Try this:
t = linspace(350, 750); % Create Data
y = sin(2*pi*t/200 + 2) .* cos(2*pi*t/300)*0.2 + 0.2; % Create Data
ptchidx = (t >= 500) & (t <= 650); % Area To Shade
figure(1)
plot(t, y)
hold on
patch([t(ptchidx) fliplr(t(ptchidx))], [y(ptchidx) zeros(size(y(ptchidx)))], [0.6 0.4 0.9], 'FaceAlpha',0.3, 'EdgeColor','none')
hold off
grid

댓글 수: 6

@Star Strider: Thanks for the suggestion! Using the code, I get this plot:
I would like the shaded region to stretch from y = 0 to y = .4. Would I modify the following line of code?
patch([t(ptchidx) fliplr(t(ptchidx))], [y(ptchidx) zeros(size(y(ptchidx)))], [0.6 0.4 0.9], 'FaceAlpha',0.3, 'EdgeColor','none')
Specifically, would I change this section?
[y(ptchidx) zeros(size(y(ptchidx)))]
Thanks!
My pleasiure!
If you just want the shaded box behind the plot, this works:
patch([500 650 650 500], [ 0 0 [1 1]*max(ylim)], [0.6 0.4 0.9], 'FaceAlpha',0.3, 'EdgeColor','none')
It is essentially the same code. The ‘x’-coordinates are now simply the limits of the box, and the ‘y’-coordinates go from 0 to the maximum value of the y-limits, so it will scale automatically if you change them.
If you want to hard-code the limits to 0.4, this will work:
patch([500 650 650 500], [ 0 0 [1 1]*0.4], [0.6 0.4 0.9], 'FaceAlpha',0.3, 'EdgeColor','none')
Experiment with the other properties of the patch function to get the result you want.
árbol
árbol 2018년 3월 11일
@Star Strider: Thank you!
Star Strider
Star Strider 2018년 3월 11일
As always, my pleasure!
Luca Pozzi
Luca Pozzi 2018년 5월 28일
편집: Luca Pozzi 2018년 5월 28일
Thank you for the answer @Star Strider, it's very useful, but now here I come with a more complicated question: I would insert a vertical bar, just as the one @A. Kachalia succedeed to have, wherever the derivatives of the two signals in the plot haven't the same sign.
I thought to make a for (with some if inside) so to have in ptchidx all the intervals I'm looking for. Is it right? Is the rest of the code still valid for my purpose? Thank you very much
Star Strider
Star Strider 2018년 5월 29일
Post this as a new question.
Also, add detail, data, and code.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

질문:

2018년 3월 10일

댓글:

2018년 5월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by