필터 지우기
필터 지우기

Tweak legend entries without creating a new legend

조회 수: 64 (최근 30일)
Chad Greene
Chad Greene 2014년 7월 11일
답변: Rodrigo Bernal 2021년 7월 13일
I have created a plot with a legend and now I would like to edit legend entries without recreating the whole legend. For example, for five legend entries I would like to reorder them like
newOrder = [2 3 5 4 1];
I can reorder the entries by first getting current legend handles and strings like this:
[~,~,plot_h,text_strings] = legend;
And then I can reorder the entries like this:
legend(plot_h(newOrder),text_strings{newOrder})
However, this doesn't only reorder the legend; it seems to delete the legend I'd already created, with its perfect placement and color scheme, etc, and creates a new legend with entries in the correct new order. How can I edit these entries without screwing up the placement, colors, boxoff, etc that I've already set?

채택된 답변

Chad Greene
Chad Greene 2014년 7월 14일
I did not quite figure out how to edit an existing legend, but I did work up this function that does what I needed it to do.
  댓글 수: 1
Ben11
Ben11 2014년 7월 14일
Nice! Sorry I kind of forgot to update this post.

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

추가 답변 (2개)

Rodrigo Bernal
Rodrigo Bernal 2021년 7월 13일
Edit a specific legend entry of an existing legend:
figu=gca;
figu.Legend.String(6)={'Edit the 6th legend'};

Ben11
Ben11 2014년 7월 11일
편집: Ben11 2014년 7월 11일
Maybe assign a handle to your legend in the first place, eg
hLegend = legend(...);
and then change the String property with the new order:
set(hLegend,'String',newOrder);
This should only update the current legend without replacing it.
  댓글 수: 2
Chad Greene
Chad Greene 2014년 7월 11일
That's the first solution I tried, but there are a couple of issues:
1. It'd have to be set(h,'string',text_strings(newOrder))
and
2. That changes the order of the strings without changing the order of the markers beside them. I want to keep the associations between strings and their data.
Chad Greene
Chad Greene 2014년 7월 11일
Is there a legend property corresponding to 'String' for 'Data'? Then it would be a simple issue of
set(h,'String',text_strings(newOrder),'Data',plot_h(newOrder))

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

카테고리

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