Stretching a rectangle over a date axis

조회 수: 51 (최근 30일)
Avi Michaely
Avi Michaely 2018년 9월 23일
댓글: F S 2020년 9월 17일
Hello, I have a graph with dates as X axis (datetime variables) I'm trying to stretch a rectangle over certain dates (I have the dates stored in a vector), but the problem is that the X axis is datetime and the Y axis is doubles, so MATLAB throws an error. How can I fix this?
for I = 1:1:length(main_idleDateMat(:,1))
startX = main_idleDateMat(I,1);
endX = main_idleDateMat(I,2);
rectangle('Position', [startX idleVecStartHeights(I)-80 (endX - startX) 160]);

채택된 답변

dpb 2018년 9월 23일
편집: dpb 2018년 9월 24일
Ayup, "no can do" it seems...another place TMW either hasn't yet got a round tuit or didn't think it needed to add an overloaded plotting function to take care of the new class object.
All is not lost, however, but it's a regression, indeed. :(
You'll have to revert to the venerable datenum form to plot to subsequently use rectangle or draw the lines directly with plot or line. If you do the former, then will have to also use datetick to format the x-axis as date. All in all, the direct-drawing option may turn out "more better".
Use patch. You'll have to provide X,Y vectors to outline the four corners, but it an aliased version has been created that is datetime-aware so TMW has, to their credit, done the most fundamental routine.
Mayhaps they'll eventually get to the rest...
  댓글 수: 3
dpb 2018년 9월 24일
편집: dpb 2018년 9월 24일
Yeah, the overloaded plot functions know what to do with the datetime or timeseries axes; it's still a work in progress for some of the other specialty routines.
Not clear whether they "just haven't got there yet" or nobody's really thought about such and have simply overlooked the other functions. Probably worth enhancement request/feature shortage/quality of implementation/bug report depending on one's point of view of how to judge the failure.
Avi Michaely
Avi Michaely 2018년 9월 24일
Do you know if I can fill the rectangle somehow?

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

추가 답변 (1개)

Ian 2020년 4월 25일
편집: Ian 2020년 4월 25일
There's a nice solution to drawing a semi-transparent colored rectangle over a plot at
Alternately, you can use area(...) ,setting 'basevalue' to define the bottom of the rectangle:
area([dates(1),dates(2)],[.4,.4],'facecolor',[.8,1,.8], ...
'facealpha',.5,'edgecolor','none', 'basevalue',-.2);
will draw a rectangle with x values between dates(1) and dates(2), and y values between -.2 (basevalue) and .4 .


Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기


Community Treasure Hunt

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

Start Hunting!

Translated by