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
jonas 2018년 10월 8일

0 개 추천

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,:));
jonas
jonas 2018년 10월 9일
편집: jonas 2018년 10월 9일
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.
jonas
jonas 2018년 10월 9일
편집: jonas 2018년 10월 9일
I thought you were trying to plot a number of points? Could you perhaps upload the data?
I'm sorry h is 12x1
jonas
jonas 2018년 10월 9일
편집: jonas 2018년 10월 9일
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)
jonas
jonas 2018년 10월 9일
편집: jonas 2018년 10월 9일
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.
jonas
jonas 2018년 10월 9일
편집: jonas 2018년 10월 9일
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
jonas 2018년 10월 9일
Cheers!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

제품

릴리스

R2018b

질문:

2018년 10월 8일

댓글:

2018년 10월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by