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 件のコメント
Rytis Stockus
2018 年 10 月 9 日
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?
Rytis Stockus
2018 年 10 月 9 日
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?
Rytis Stockus
2018 年 10 月 9 日
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.
Rytis Stockus
2018 年 10 月 9 日
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)

Rytis Stockus
2018 年 10 月 9 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
