How to put in color a certain part of the background in a plot with matlab?

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?

 採用された回答

Mischa Kim
Mischa Kim 2014 年 1 月 16 日
編集済み: Mischa Kim 2014 年 1 月 16 日

0 投票

Try rectangle. First, draw the rectangle, then the line plots (using hold on, of course).

4 件のコメント

alagu mathi
alagu mathi 2015 年 4 月 21 日
編集済み: alagu mathi 2015 年 4 月 21 日
Hi Mischa Kim,
I have the same issue which afrya asked, I followed the above answer which you discussed. I got the rectangle it is in grey color. But I want to remove the outline of that. Can you help me how to remove the outline of the rectangle?
Thank you
Laurel
Laurel 2018 年 1 月 20 日
Use the 'EdgeColor' property: rectangle('Position', pos, 'EdgeColor',[.5 .5 .5], 'FaceColor', [.5 .5 .5])
Claire Andreasen
Claire Andreasen 2020 年 8 月 17 日
if you're using a grid, how would you make the grid appear overtop the rectangle?
@Claire Andreasen, you can use set(gca, "Layer", "top") after rectangle and before plot.
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)

サインインしてコメントする。

その他の回答 (1 件)

Jérôme
Jérôme 2022 年 8 月 18 日

0 投票

You can also use fill.
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.

カテゴリ

質問済み:

2014 年 1 月 16 日

コメント済み:

2022 年 8 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by