Highlight a Section of a Plot

16 次查看(过去 30 天)
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
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 个评论
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 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by