![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178184/image.png)
how can I divide x-axis background into different colored zones?
57 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Amr Hashem
am 24 Aug. 2015
Kommentiert: Star Strider
am 7 Sep. 2015
I want to divide x-axis background into different colored\grid zones
to have a shape like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/148409/image.jpeg)
I use:
plot(A,B,'k');
hold on;
area(A(1:13),B(1:13),'basevalue',0,'FaceColor','g');
area(A(13:end),B(13:end),'basevalue',0,'FaceColor','r');
but it produced this shape:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/148410/image.jpeg)
I am only want to have three separate zones.
HOW CAN I DO THIS ?
0 Kommentare
Akzeptierte Antwort
Star Strider
am 25 Aug. 2015
Bearbeitet: Star Strider
am 25 Aug. 2015
I will leave it to you to define the colours, but otherwise this code should work for you without modification in your code, except for the plot(x,y) statements:
x = linspace(0, 3E+5);
y = cumsum(rand(size(x)));
figure(1)
plot(x, y)
hold on
patch([0 1E+5 1E+5 0], [max(ylim) max(ylim) 0 0], 'r')
patch([1E+5 2E+5 2E+5 1E+5], [max(ylim) max(ylim) 0 0], 'g')
patch([2E+5 3E+5 3E+5 2E+5], [max(ylim) max(ylim) 0 0], 'b')
plot(x, y, 'k', 'LineWidth',1.5)
hold off
NOTE — You have to re-plot the original line after plotting the patches, because the patches overplot the data line. The first plot call is necessary to define the axes and the figure.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178184/image.png)
6 Kommentare
Star Strider
am 7 Sep. 2015
See hatch.m in the File Exchange. MATLAB itself has no functions for this. Another option may be crosshatch_poly, since I used patch objects in my code. (I have no experience with either of these functions, and have not looked at their code. I can’t help you with them.)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Polygons finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!