How to set the 'DisplayName' properties for a bar chart with multiple sets of bars directly from the call to 'bar', not using 'set' afterwards

조회 수: 38 (최근 30일)
This questions is similar to the ones at the links below (which were never answered satisfactorily).
https://uk.mathworks.com/matlabcentral/answers/476496-how-could-i-set-the-legend-for-a-particular-bar-in-a-bar-chart
Why can I use the 'set' command to set the bar chart 'DisplayName' properties for a bar chart with multiple groups of bars, but attempting to set the exact same DisplayName when calling bar throws an error?
To use the solution from the mathworks link:
% This will produce 4 groups of 3 different colored bars
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
h = bar(y); % h will have 3 handles, one for each color of bars
% set 3 display names for the 3 handles
set(h, {'DisplayName'}, {'Jan','Feb','Mar'}')
% Legend will show names for each color
legend()
This works fine. But attempting to set the 'DisplayName' from 'bar' directly throws an error.
% This will produce 4 groups of 3 different colored bars
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
h = bar(y, {'DisplayName'}, {'Jan','Feb','Mar'}'); % h will have 3 handles, one for each color of bars
Error using matlab.graphics.chart.primitive.Bar
Value cell array handle dimension must match handle vector length.

Error in bar (line 223)
h(i) = matlab.graphics.chart.primitive.Bar('Parent',hPar,...
legend()
This strikes me as poor design.
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2024년 2월 14일
"but attempting to set the exact same DisplayName when calling bar throws an error?"
Because the expected input to the property DisplayName is a character vector or a string scalar, whereas you have provided a cell array of character vectors.
When using the bar() call, DisplayName is expected to be used for when the output is a single bar object.
In case of multiple bar objects as the output, you will have to use other approachs, like using set() (as you did above) or legend() -
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
h = bar(y)
h =
1×3 Bar array: Bar Bar Bar
arr = {'Jan','Feb','Mar'};
legend(arr)

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

답변 (1개)

Hassaan
Hassaan 2024년 2월 14일
편집: Hassaan 2024년 2월 14일
As per my understanding and the working code you shared. It's essential to understand that the syntax you attempted to use directly within the bar function call for setting DisplayName properties is not supported. The correct way to set the DisplayName for each group of bars for the purpose of having a legend is to first create the bar chart and then separately set the DisplayName for each group using the handles returned by the bar function.
% Data for the bar chart
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
% Create the bar chart
h = bar(y); % h will have 3 handles, one for each color of bars
% Set 3 display names for the 3 handles, corresponding to each group of bars
set(h(1), 'DisplayName', 'Jan');
set(h(2), 'DisplayName', 'Feb');
set(h(3), 'DisplayName', 'Mar');
% Create a legend to show names for each color
legend();
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by