Modification of rectangle plot

I would like to modify this plot. (see the code below) Instead of coloring the rectangles based on the x-axis, i'd like to color based on y-asix. For example, from 0-470 (y-axis) I'd like the rectangles to be colored blue. And from 470-800 colored yellow. And 800-1000 red. Thank you!
x = [3 3 2 3 2 3 2 3 2 3 4 3 4 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 4 3 4 3 4 3 4 3 4 3 4 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2];
y = [0 136 179 190 191 195 196 203 204 262 266 272 274 461 462 471 473 482 483 520 522 588 590 598 600 608 610 618 620 628 636 642 645 660 666 670 674 678 680 682 684 688 694 696 702 706 712 733 741 743 751 753 762 766 771 796 801 805 810 812 819 838 841 848 850 858 860 867 870 876 881 886 888 929 935 940 944 950 955 958 964 966 968 971 978 980 984 994 996 998 1000];
stairs(x,y, 'LineWidth',1,'Color',[0 0 0]);
set(gca, 'YDir', 'reverse')
set(gca, 'XGrid','on','XTick',[0 1 2 3 4])
xlim([0 5])
ylim([0 1000])
colors = {rgb('pale blue') rgb('sky blue') rgb('medium blue') rgb('off blue')}; %in order of x = 1,2,3,4
xl = xlim();
for i = 1:length(x)-1
rh = rectangle('Position', [xl(1), y(i), x(i+1), (y(i+1)-y(i))]);
rh.EdgeColor = 'none';
rh.FaceColor = colors{x(i+1)};
end

回答(2 个)

Here is the code the colors the plot values w.r.t y values:
clearvars; clc; close all
x = [3 3 2 3 2 3 2 3 2 3 4 3 4 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 4 3 4 3 4 3 4 3 4 3 4 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2];
y = [0 136 179 190 191 195 196 203 204 262 266 272 274 461 462 471 473 482 483 520 522 588 590 598 600 608 610 618 620 628 636 642 645 660 666 670 674 678 680 682 684 688 694 696 702 706 712 733 741 743 751 753 762 766 771 796 801 805 810 812 819 838 841 848 850 858 860 867 870 876 881 886 888 929 935 940 944 950 955 958 964 966 968 971 978 980 984 994 996 998 1000];
Colors = [{'blue'}, {'yellow'}, {'red'}];
%%
for ii = 1:length(x)-1
if y(ii)>=0 && y(ii) < 470
stairs(x(ii),y(ii), 'LineWidth',1,'Color',[0 0 0]);
rh = rectangle('Position', [x(ii), y(ii), x(ii+1), (y(ii+1)-y(ii))]);
rh.EdgeColor = 'none';
rh.FaceColor = Colors{1}; hold on
elseif y(ii)>=470 && y(ii) < 800
stairs(x(ii),y(ii), 'LineWidth',1,'Color',[0 0 0]);
rh = rectangle('Position', [x(ii), y(ii), x(ii+1), (y(ii+1)-y(ii))]);
rh.EdgeColor = 'none';
rh.FaceColor = Colors{2};
else
stairs(x(ii),y(ii), 'LineWidth',1,'Color',[0 0 0]);
rh = rectangle('Position', [x(ii), y(ii), x(ii+1), (y(ii+1)-y(ii))]);
rh.EdgeColor = 'none';
rh.FaceColor = Colors{3};
end
end
axis ij
set(gca, 'XGrid','on','XTick',[0 1 2 3 4])
ylim([0 1000])
hold off; shg

1 个评论

Thank you for your quick response! Your answer is close.... it seems the colors are coloring the wrong portion of the plot. What is white on the plot is what I would like colored and what is colored is what I would like to be white/not colored. Do you know how to fix this? Again, thank you for your help!
MatlabQ_2.JPG

请先登录,再进行评论。

Change the ordering of x(ii)'s and x(ii+1)'s. Good luck.

1 个评论

I'm sorry but I do not understand... could you explain further? Thank you!

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by