Contour plot Legend | How to change symbol to straight line?

35 ビュー (過去 30 日間)
EM
EM 2016 年 5 月 16 日
コメント済み: EM 2016 年 5 月 18 日
Hi,
I am having trouble with the contour plot legend. I do not like the symbol used in the legend. I would like to remove the contour symbol and simply use a straight line for my legend symbol. Any ideas would be appreciated.
Here is the code I used to generate the plot.
figure();
box on
hold on
contour(R,P,regime1,[1,2],'b')
contour(R,P,regime2,[1,2],'r')
xlim([0,Rmax-2])
ylim([0,Pmax-2])
legend('series 1','series 2','Location','Best')
  2 件のコメント
Walter Roberson
Walter Roberson 2016 年 5 月 17 日
Which MATLAB version are you using? I suspect you are using R2014b or later?
EM
EM 2016 年 5 月 18 日
Hi Walter,
Yes I am using 2015b

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

採用された回答

Matt Cohen
Matt Cohen 2016 年 5 月 18 日
Hi EM,
I understand you are interested in modifying the legend in a contour plot to use straight lines instead of contour symbols.
Assuming you are using MATLAB R2014b or later, there are some possible workarounds that you can use for this. One way to create a legend containing the colors of the contours is to create invisible lines with the colors of the contour plots. You can either plot NaN values in the figure, which will not directly show up in the figure, or you can create a Line object and set its 'Visible' property to 'off'. The following example code shows how you can do both:
%%Create arrays
x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);
%%Plot NaN values
figure
hold on
contour(X,Y,Z,(-2:0.5:2),'Color','r');
hRed = plot(NaN, '-r');
contour(X,Y,Z*2,(-12:4:12)/3,'Color','b');
hBlue = plot(NaN, '-b');
legend([hRed hBlue], 'red', 'blue');
%%Lines with 'Visible' set to 'off'
figure
line(X(1:2),Y(1:2),'Color','r','Visible','off');
line(X(1:2),Y(1:2),'Color','b','Visible','off');
legend('red','blue');
hold on
contour(X,Y,Z,(-2:0.5:2),'Color','r');
contour(X,Y,Z*2,(-12:4:12)/3,'Color','b');
hold off
I hope this information proves to be helpful.
Matt
  1 件のコメント
EM
EM 2016 年 5 月 18 日
Hi Matt,
This works beautifully! Thanks!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by