Filter löschen
Filter löschen

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

33 Ansichten (letzte 30 Tage)
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 Kommentar
Dyuman Joshi
Dyuman Joshi am 14 Feb. 2024
"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)

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Hassaan
Hassaan am 14 Feb. 2024
Bearbeitet: Hassaan am 14 Feb. 2024
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.

Kategorien

Mehr zu Graphics Object Properties finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by