Legend Button displays data1, data2, etc

20 次查看(过去 30 天)
Hi,
I've been searching for a while and I'm hoping someone can help me figure this out.
How can I make the legend button in the figure toolbar display the data by name instead of 'data1,data2,data3,etc'?
I've set the 'Tag' property for each lineseries to what I want displayed in the legend.
Is there a way to change the callback function for icons such as the legend button in the default toolbar?
Thanks so much,
Scott

回答(1 个)

KAE
KAE 2017-5-4
You need to include strings in the legend command.
% Plot some data
figure;
h1 = plot(rand(1,7), rand(1,7));
hold on;
h2 = plot(rand(1,7), rand(1,7));
h3 = plot(rand(1,7), rand(1,7));
% Label each line in the legend
legend([h1 h2 h3], 'First line', 'Second line', 'Third line');
  1 个评论
Steven Lord
Steven Lord 2017-5-5
That's one way. Another, as stated in the legend function documentation, is:
"legend('show') creates a legend for the current axes or chart returned by gca. The legend automatically updates when you add or delete graphics objects from the axes. For the labels, the legend uses the text from the DisplayName properties of the graphics objects. If the DisplayName property is empty, then the legend uses a label of the form 'dataN'."
So you could assign a value to the DisplayName property of the graphics object when you construct it (or afterwards) and legend will use that value.

请先登录,再进行评论。

类别

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