필터 지우기
필터 지우기

Setting DisplayName for a bar graph

조회 수: 29 (최근 30일)
the cyclist
the cyclist 2012년 1월 5일
답변: Thalia Nikolaidou 2017년 11월 10일
When using the plot() command, one can set the DisplayName property of the plotted lines, and display them in the legend, like so:
hp=plot(1:10,[1:10;2:11]','DisplayName',{'line 1','line 2'});
legend(hp)
This approach does not work for a bar graph:
hb=bar(1:10,[1:10;2:11]','DisplayName',{'line 1','line 2'}); % Error!
legend(hb)
I know I can "get" the DisplayName property of the bars, like so:
hb=bar(1:10,[1:10;2:11]');
get(hb,'DisplayName')
But is there a way to set the DisplayName property in a vectorized way?
I tried
set(hb,'DisplayName',{'bar 1','bar 2'})
but the syntax requires a string input there, and balks at the cell array.

채택된 답변

Patrick Kalita
Patrick Kalita 2012년 1월 5일
The help for set says:
set(H,pn,MxN_pv) sets n property values on each of m graphics objects, where m = length(H) and n is equal to the number of property names contained in the cell array pn. This allows you to set a given group of properties to different values on each object.
In this case the length of H is 2 and we're setting 1 property. Therefore the cell array of property names pn will be 1-by-1 and the cell array of property values pv will be 2-by-1:
hb=bar(1:10,[1:10;2:11]');
set( hb, {'DisplayName'}, {'foo'; 'bar'} );
legend show

추가 답변 (1개)

Thalia Nikolaidou
Thalia Nikolaidou 2017년 11월 10일
figure1 = figure; axes1 = axes('Parent',figure1); hold(axes1,'on'); bar1 = bar(data_here); set(axes1,'XTickLabel',data_cell_array_labels_here);
It needs to set the "axes" not the gcs or the DisplayName!!

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by