How can I plot a legend only for data points?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Hello I try to plot a legend for different data points and get this error:
Unable to perform assignment because the left and right sides have a different number of elements.
Error in Robertson86 (line 57) h(i)=plot(xdata(in),ydata(in),'o','Color',cdata2(i,:)); But it does work with line plots and fills.
for i = 1:12
% plot a polygon
Poly = Robertson_zones_1986{i};
plot(Poly(:,1),Poly(:,2),'k','LineWidth',1.5)
fill(Poly(:,1),Poly(:,2),cdata(i,:))
text(RobNumPos86(i,1),RobNumPos86(i,2),num2str(i),'Color',[0.3 0.3 0.3])
% check if data is inside this particular polygon
in= inpolygon(xdata,ydata,Poly(:,1),Poly(:,2));
h(i)=plot(xdata(in),ydata(in),'o','Color',cdata2(i,:));
b(i)=numel(xdata(in));
end
legend(h,'1.Data type','2.Data type',....)
채택된 답변
jonas
2018년 10월 8일
Your plot outputs multiple line handles. If that is not what you want, then you need to adjust the dimensions of xdata and ydata. If you want to plot multiple line objects in every iteration, then I suggest you store those values in a cell array:
h{i}=plot(xdata(in),ydata(in),'o','Color',cdata2(i,:));
댓글 수: 11
Unable to perform assignment because brace indexing is not supported for variables of this type.
Error in Robertson86 (line 57) h{i}=plot(xdata(in),ydata(in),'o','Color',cdata2(i,:));
Well you probably forgot to clear h. Can you copy the size of h using your old code and show me?
The size of h is 1x9 Patch. And inside it is 1x1 Patch. Check photo attached.
I thought you were trying to plot a number of points? Could you perhaps upload the data?
I'm sorry h is 12x1
If h is 12x1, then the code should not return an error? Again, could you perhaps upload the data so I can have a look?
Also, why is h a double? Are you using an old version of MATLAB (2018b is indicated in the question)
The problem is that you have no points inside of some of the polygons. Therefore, no line handle is produced and the script returns an error when it tries to assign an empty array to h(i). Try something like this
figure;hold on
for i = 1:12
% plot a polygon
Poly = Robertson_zones_1986{i};
plot(Poly(:,1),Poly(:,2),'k','LineWidth',1.5)
m=fill(Poly(:,1),Poly(:,2),cdata(i,:))
text(RobNumPos86(i,1),RobNumPos86(i,2),num2str(i),'Color',[0.3 0.3 0.3])
% check if data is inside this particular polygon
in= inpolygon(xdata,ydata,Poly(:,1),Poly(:,2));
if sum(in)>0;
h(i)=scatter3(xdata(in),ydata(in),ones(size(ydata(in))),[],cdata(i,:),'filled','markeredgecolor','r','DisplayName',sprintf('Data type %g',i));
b(i)=numel(xdata(in));
else
h(i)=NaN
end
end
legend(h(~isnan(h)))

I dont know how you want to deal with the fact that some data types are absent. Do you want to indicate this in the legend somehow? Also, note that I changed the 'cdata2(i,:)' that was inside of the plot to 'cdata(i,:)' as I did not find a variable called cdata2.
Thanks for the answer. I will try to implement your solution and get back to you. cdata2 is created in a code (line 34) as a random RGB colour vector for different zones where data is plotted. My goal is to show a legend of data points that appeared in any of the zones cause this code is going to be used for different data inputs so the data might appear in any of those 12 zones. As I understood from your solution my loop was problematic cause some of the data does not appear in the zones.
Exactly. An interesting observation I made was that neither of
plot([],[])
scatter([],[])
returns a handle. Therefore, if you try to assign them to h(i), they will both return an error. However, if you write like this
plot([],[],'color','r')
scatter([],[],'markerfacecolor','r')
then the second one (scatter) actually returns a handle. I don't know why this is the case. But if you want to show all datatypes, even those not present in the plot, then you can simply remove the if-condition, such as
figure;hold on
for i = 1:12
% plot a polygon
Poly = Robertson_zones_1986{i};
plot(Poly(:,1),Poly(:,2),'k','LineWidth',1.5)
m=fill(Poly(:,1),Poly(:,2),cdata(i,:))
text(RobNumPos86(i,1),RobNumPos86(i,2),num2str(i),'Color',[0.3 0.3 0.3])
% check if data is inside this particular polygon
in= inpolygon(xdata,ydata,Poly(:,1),Poly(:,2));
h(i)=scatter3(xdata(in),ydata(in),ones(size(ydata(in))),[],cdata(i,:),'filled','markeredgecolor','r','DisplayName',sprintf('Data type %g',i));
b(i)=numel(xdata(in));
end
legend(h)

Yes, your solution does work as I wished. Thanks again for your help!
jonas
2018년 10월 9일
Cheers!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Legend에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
