How to avoid patch color being included in legend ?

42 ビュー (過去 30 日間)
Mahesh Babu Dhanekula
Mahesh Babu Dhanekula 2020 年 8 月 7 日
コメント済み: Image Analyst 2020 年 8 月 7 日
I would like to know how to remove the patcharea being represented in legend. I have attached the code and outputs when patch command is included and not used. When patch is used to highlight a portion of plot its color is getting indicated rather than the linestyle and line color in legend. Also the thickening of xaxis and yaxis in upper and right side borders disappears when more than one patch command has used. This can be seen from the figure named patch. Please let me know how to fix this issue. Due to the limitation on number of pictures can be attached i have added the picture when patch command is used.
Edit: Through the usage of handles legend issue got solved. But the boder issue persists. Have a look at the picture named patch1 in comments.
clc;
clear all;
close all;
x = linspace(-10,10,200);
y = sin(4*x);
y1 = sin(x);
figure;
patch([2 5 5 2], [[1 1]*(-1) [1 1]*(1)], 'y', 'FaceAlpha',0.3, 'EdgeColor','none');
hold on
patch([-5 -2 -2 -5], [[1 1]*(-1) [1 1]*(1)], 'y', 'FaceAlpha',0.3, 'EdgeColor','none');
hold on
plot(x,y,'r');
hold on
plot(x,y1,'g');
hold off
grid on;
legend({'sin(4*x)','sin(x)'});

採用された回答

Image Analyst
Image Analyst 2020 年 8 月 7 日
編集済み: Image Analyst 2020 年 8 月 7 日
Try this:
clc;
clear all;
close all;
x = linspace(-10,10,200);
y = sin(4*x);
y1 = sin(x);
figure;
hold on
patch([2 5 5 2], [[1 1]*(-1) [1 1]*(1)], 'y', 'FaceAlpha',0.3, 'EdgeColor','none');
patch([-5 -2 -2 -5], [[1 1]*(-1) [1 1]*(1)], 'y', 'FaceAlpha',0.3, 'EdgeColor','none');
h1 = plot(x,y,'r');
h2 = plot(x,y1,'g');
hold off
grid on;
legend([h1 h2],'sin(4*x)','sin(x)');
box on
Alternatively you can set the "Annotation.LegendInformation.IconDisplayStyle" property of the patch object to 'off':
period = 3;
x = linspace(-10,10,200);
y = sin(2 * pi * x / period);
y1 = sin(x);
figure;
hold on
p1 = patch([2 5 5 2], [[1 1]*(-1) [1 1]*(1)], 'y', 'FaceAlpha',0.3, 'EdgeColor','none');
p2 = patch([-5 -2 -2 -5], [[1 1]*(-1) [1 1]*(1)], 'y', 'FaceAlpha',0.3, 'EdgeColor','none');
% Tell legend not to include the patches.
p1.Annotation.LegendInformation.IconDisplayStyle = 'off';
p2.Annotation.LegendInformation.IconDisplayStyle = 'off';
h1 = plot(x,y,'r', 'LineWidth', 2);
h2 = plot(x,y1,'g', 'LineWidth', 2);
hold off
grid on;
legend('sin(4*x)','sin(x)');
% Turn on border:
box on
  4 件のコメント
Mahesh Babu Dhanekula
Mahesh Babu Dhanekula 2020 年 8 月 7 日
thanks for the method is there any keywords particularly related to high resolution image export
Image Analyst
Image Analyst 2020 年 8 月 7 日
Not that I know of, other than the obvious.
You can search for Waterloo - a third party package:
I guess it's an alternative to export_fig.
The Mathworks developers told us that exportgraphics() can do most, but not all (yet) that export_fig can do. Their goal is to bring that functionality in-house to relieve Yair Altman from the burden of maintaining export_fig.

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

その他の回答 (2 件)

KSSV
KSSV 2020 年 8 月 7 日
編集済み: KSSV 2020 年 8 月 7 日
clc;
clear all;
close all;
x = linspace(-10,10,200);
y = sin(4*x);
y1 = sin(x);
figure;
hold on
patch([2 5 5 2], [[1 1]*(-1) [1 1]*(1)], 'y', 'FaceAlpha',0.3, 'EdgeColor','none');
patch([-5 -2 -2 -5], [[1 1]*(-1) [1 1]*(1)], 'y', 'FaceAlpha',0.3, 'EdgeColor','none');
h1 = plot(x,y,'r');
h2 = plot(x,y1,'g');
hold off
grid on;
legend([h1 h2],'sin(4*x)','sin(x)');
  3 件のコメント
Mahesh Babu Dhanekula
Mahesh Babu Dhanekula 2020 年 8 月 7 日
But the borders of graph on top and right portions is not visible
Mahesh Babu Dhanekula
Mahesh Babu Dhanekula 2020 年 8 月 7 日
see the picture

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


Arthur Roué
Arthur Roué 2020 年 8 月 7 日
You need to explicit legend and object handle for each line / patch when calling legend function
hY = plot(x,y,'r');
hold on
hY1 = plot(x,y1,'g');
hold off
grid on;
legend([hY, hY1], {'sin(4*x)','sin(x)'});
  4 件のコメント
Arthur Roué
Arthur Roué 2020 年 8 月 7 日
Switch 'Box' property of your axes to 'on' in order to display all borders
x = linspace(-10,10,200);
y = sin(4*x);
y1 = sin(x);
figure;
hAxe = axes(gcf, 'NextPlot', 'add', 'Box', 'on');
patch([2 5 5 2], [[1 1]*(-1) [1 1]*(1)], 'y', 'FaceAlpha',0.3, 'EdgeColor','none');
patch([-5 -2 -2 -5], [[1 1]*(-1) [1 1]*(1)], 'y', 'FaceAlpha',0.3, 'EdgeColor','none');
hY = plot(x,y,'r');
hY1 = plot(x,y1,'g');
grid on;
legend([hY, hY1], {'sin(4*x)','sin(x)'});
Mahesh Babu Dhanekula
Mahesh Babu Dhanekula 2020 年 8 月 7 日
How can we export and save highest resolutions from matlab ?

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

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by