フィルターのクリア

Generating a Legend for Colormap

22 ビュー (過去 30 日間)
Camille Woicekowski
Camille Woicekowski 2020 年 7 月 24 日
I have several lines plotted on one graph that are color-coded by a colormap. I want to add a legend (not colorbar) that idenitifies each of the lines by their color. The colormap is working and the legend is generated, by the markers in the legend are all black. Here is what I have
cm = jet(size(x_mtx))
for i=1:size(x_mtx)
p3=plot(x_mtx(i,:), 'Color',cm(i,:));
hold on;
leg_label(i,:) = sprintf('%1.2e',rmse_mtx(i,1));
end
leg=legend(leg_label);
How can I color the legend with the color corresponding to the line plotted?

回答 (1 件)

KSSV
KSSV 2020 年 7 月 24 日
  1 件のコメント
Camille Woicekowski
Camille Woicekowski 2020 年 7 月 24 日
This seems to work, but it'd get tedious typing in the values for each legend handle when I have 15+. Can I use this method, but set the legend labels in the for loop? The info for the labels saved in a 22x1 matrix (rmse_mtx). I tried
clear p
for i=1:size(x_mtx)
p3=plot(x_mtx(i,:),'Color',cm(i,:));hold on
p(i)=p3(1);
legend(p,{sprintf('%1.2e', rmse_mtx(i,:))});
%label(kk,:) = sprintf('%1.2e',rmse_mtx(kk,1));
end
but this only gives me the first label.

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

カテゴリ

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