How can I shade a range of time on a time series plot?

95 views (last 30 days)
I'm plotting a mortality curve time series and I am trying to shade a range of time (x-axis) to highlight the target time for determining the diagnostic dose for my experiment. In the plot area, I need the time between 30 minutes and 60 minutes shaded a different color to emphasize the target time.
I tried creating a rectangle object and snapping it to the axis, but could not figure out how move it behind the plotted data.
The code for my plot is below:
function createfigure(PPro_ts1, YMatrix1)
%CREATEFIGURE(PPRO_TS1, YMATRIX1)
% PPRO_TS1: vector of x data
% YMATRIX1: matrix of y data
% Auto-generated by MATLAB on 24-Feb-2015 12:27:42
% Create figure
figure1 = figure;
% Create axes
axes1 = axes('Parent',figure1,'XTick',[0 15 30 35 40 45 60 75 90 105 120]);
box(axes1,'on');
hold(axes1,'on');
% Create ylabel
ylabel('Percent (%) Mortality');
% Create xlabel
xlabel('Time (minutes)');
% Create title
title('P. papatasi Propoxur');
% Create multiple lines using matrix input to plot
plot1 = plot(PPro_ts1,YMatrix1,'MarkerSize',8,'LineWidth',2,'Parent',axes1);
set(plot1(1),'DisplayName','1ug','Marker','^','LineStyle',':');
set(plot1(2),'DisplayName','2ug','Marker','o');
set(plot1(3),'DisplayName','3ug','Marker','*','LineStyle','--');
% Create legend
legend1 = legend(axes1,'show');
set(legend1,...
'Position',[0.741071428571428 0.129151785714286 0.150810033404636 0.123080357142857],...
'FontSize',11);
% code
end
Any help is appreciated!

Answers (1)

Star Strider
Star Strider on 24 Feb 2015
Edited: Star Strider on 24 Feb 2015
This plots a gray background from 0 to whatever the maximum ylim is, from 30 to 60, then overplots a blue exponential over it. It autoranges according to whatever ylim is, so it ‘knows’ that already.
You should be able to use it without much modification. It’s relatively easy to change the colour of the patch object. See the documentation on patch for details.
The code:
x = linspace(0,90);
y = exp(-0.05*x);
figure(1)
patch([30 60 60 30], [0 0 max(ylim)*[1 1]], [0.8 0.8 0.8])
hold on
plot(x, y, '-b')
hold off
grid
The example plot:
  2 Comments
Conor Reese
Conor Reese on 24 Feb 2015
Thank you for your reply!
I'm having trouble figuring out how I can insert the rectangular patch behind the lines that I am already plotting.
This is what happens:
The Code:
function createfigure1(PPro_ts1, YMatrix1, YData1, XData1)
%CREATEFIGURE1(PPRO_TS1, YMATRIX1, YDATA1, XDATA1)
% PPRO_TS1: vector of x data
% YMATRIX1: matrix of y data
% YDATA1: patch ydata
% XDATA1: patch xdata
% Auto-generated by MATLAB on 24-Feb-2015 14:28:43
% Create figure
figure1 = figure;
% Create axes
axes1 = axes('Parent',figure1,'XTick',[0 15 30 35 40 45 60 75 90 105 120]);
box(axes1,'on');
grid(axes1,'on');
hold(axes1,'on');
% Create ylabel
ylabel('Percent (%) Mortality','FontSize',11);
% Create xlabel
xlabel('Time (minutes)','FontSize',11);
% Create title
title('P. papatasi Propoxur','FontSize',11);
% Create multiple lines using matrix input to plot
plot1 = plot(PPro_ts1,YMatrix1,'MarkerSize',8,'LineWidth',2,'Parent',axes1);
set(plot1(1),'DisplayName','1ug','Marker','^','LineStyle',':');
set(plot1(2),'DisplayName','2ug','Marker','o');
set(plot1(3),'DisplayName','3ug','Marker','*','LineStyle','--');
% Create patch
patch('Parent',axes1,'YData',YData1,'XData',XData1,...
'FaceColor',[0.8 0.8 0.8]);
% Create legend
legend1 = legend(axes1,'show');
set(legend1,...
'Position',[0.741071428571428 0.14622422235023 0.150810033404636 0.0889354838709678],...
'FontSize',11);
I imagine that if moved '$ Create Patch' before '% Create Multiple...' the lines would be plotted on top of the rectangle. I tried this, but I haven't been able to run the code (section): I receive the following error:
function createfigure1(PPro_ts1, YMatrix1, YData1, XData1)
|
Error: Function definitions are not permitted in this context.
Star Strider
Star Strider on 24 Feb 2015
My pleasure!
My code for the patch object should work without much, if any, modification in your application. (The y-limit values will scale automatically as I wrote my code.) You have to plot the patch object first, as I did, for it to be in the background. Then plot the curves afterwards so they will plot on top of it. Order is important. (The MATLAB-generated code for your figure doesn’t tell me what your actual code for it is or does.)
As for the error you’re getting, save your function to its own .m-file, specifically, createfigure1.m on your MATLAB search path. Then call it.
Also, if you want the Greek lower-case letter mu, use \mu in your legend. So '1ug' becomes '1\mug' and so forth.
Good luck in your fight against leishmaniasis!

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!