How to put in color a certain part of the background in a plot with matlab?
256 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
afrya
am 16 Jan. 2014
Kommentiert: Jérôme
am 18 Aug. 2022
Hello,
I would like to draw in grey color a certain part of the background in a plot. I know how to change the background color of the whole plot.I used this code after the plot function:
HANDLE = gca
get( HANDLE );
set( HANDLE, 'Color', [0.7,0.7,0.7] )
Exemple: y=x^2
How can I do to draw the blue part in grey and to leave the other parts in white?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/155168/image.jpeg)
0 Kommentare
Akzeptierte Antwort
Mischa Kim
am 16 Jan. 2014
Bearbeitet: Mischa Kim
am 16 Jan. 2014
4 Kommentare
Claire Andreasen
am 17 Aug. 2020
if you're using a grid, how would you make the grid appear overtop the rectangle?
Jérôme
am 18 Aug. 2022
Below is an example comparing without and with that line. It also solves the issue that the rectangle appears above the axes black line.
figure
subplot(2,1,1)
rectangle(Position=[0,-0.5,0.8,1], FaceColor=[0.9 0.9 0.9], EdgeColor=[0.9 0.9 0.9])
hold on; grid on; grid minor
plot(-1:0.01:1, (-1:0.01:1).^2)
subplot(2,1,2)
rectangle(Position=[0,-0.5,0.8,1], FaceColor=[0.9 0.9 0.9], EdgeColor=[0.9 0.9 0.9])
hold on; grid on; grid minor
set(gca, "Layer", "top")
plot(-1:0.01:1, (-1:0.01:1).^2)
Weitere Antworten (1)
Jérôme
am 18 Aug. 2022
In case you are using a legend on your plot, what is plotted with fill will be listed in the legend, whereas what is plotted with rectangle will not be listed in the legend. So, the best one to use depends on what we want to do.
0 Kommentare
Siehe auch
Kategorien
Mehr zu 2-D and 3-D Plots 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!