필터 지우기
필터 지우기

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

조회 수: 18 (최근 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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by