Adding Legend to Bar Graph

I have a bar graph with a mix of colors and would like to create a legend but I can't figure out where to put it within my code.
The code I'm using is as follows:
b = bar(StepsS2.Time, StepsS2.Steps);
b.FaceColor = 'flat';
for i = 1:length(StepsS2.Time)
switch StepsS2.Action(i)
case "Jump"
b.CData(i,:) = [1 0 0];
case "Run"
b.CData(i,:) = [1 1 0];
case "Squat"
b.CData(i,:) = [0 1 1];
case "Cycle"
b.CData(i,:) = [0 0 1];
otherwise
b.CData(i,:) = [0 1 0];
end
end
I've tried a variety of solutions such as:
set(b, {'DisplayName'}, {'Jump', 'Run', 'Squat', 'Cycle', 'Other'}'), which gives the following error: Error using matlab.graphics.chart.primitive.Bar/set
Value cell array handle dimension must match handle vector length.
and also
legend(b, 'Jump', 'Run', 'Squat', Cycle', 'Other'), which only displays 'Jump'

Risposte (3)

Hi, here is an easy solution to your exercise:
Labelit={};
LEG = {"Jump", 'Run', 'Squat', 'Cycle', 'Other'};
for ii=1:5
b= bar(A(ii), B(ii)); hold on
b.FaceColor = 'flat';
Labelit{ii}=LEG{ii};
legend(Labelit{:});
end

3 Commenti

What would A and B be? I get an error saying that it's unrecognized.
A=StepsS2.Time; B=StepsS2.Steps
Or
...
b= bar(StepsS2.Time(ii), StepsS2.Steps(ii)); hold on
...
That worked, the accurate legend showed up on my graph but it messed up the rest of the bars when I put that code after the code I listed above. If I put the code listed above after the code you provided, I get this error: Warning: Error updating Bar.
CData must be an RGB triplet, a scalar, an M-by-1 vector the same length as X, or an M-by-3 matrix.
I didn't redefine b so this is exactly what I put in:
figure()
Labelit = {};
LEG = {'Jump', 'Run', 'Squat', 'Cycle', 'Other'};
for ii = 1:5
b = bar(StepsS2.Time(ii), StepsS2.Steps(ii)); hold on
b.FaceColor = 'flat';
Labelit{ii} = LEG{ii};
legend(Labelit{:});
end
for i = 1:length(Subject2StepsS2.Time)
switch Subject2StepsS2.ActivityType(i)
case "Jump"
b.CData(i,:) = [1 0 0];
case "Run"
b.CData(i,:) = [0 1 1];
case "Squat"
b.CData(i,:) = [0 1 0];
case "Cycle"
b.CData(i,:) = [0 0 1];
otherwise
b.CData(i,:) = [1 0 1];
end
end

Accedi per commentare.

If you are concerned of coloring all bars specifically, then you need to use this code:
figure()
Labelit = {};
CL = [1 0 0; 0 1 1; 0 1 0; 0 0 1; 1 0 1];
LEG = {'Jump', 'Run', 'Squat', 'Cycle', 'Other'};
for ii = 1:5
b = bar(StepsS2.Time(ii), StepsS2.Steps(ii)); hold on
b.FaceColor = 'flat';
b.CData = CL(ii,:);
Labelit{ii} = LEG{ii};
legend(Labelit{:});
end

1 Commento

It's still not working, the legend shows up just fine, but when I input the code above it produces one thick bar of 'Run' that isn't even part of my data. When I follow the code above with:
b = bar(StepsS2.Time, StepsS2.Steps);
b.FaceColor = 'flat';
for i = 1:length(StepsS2.Time)
switch StepsS2.ActivityType(i)
case "Jump"
b.CData(i,:) = [1 0 0];
case "Run"
b.CData(i,:) = [0 1 1];
case "Squat"
b.CData(i,:) = [0 1 0];
case "Cycle"
b.CData(i,:) = [0 0 1];
otherwise
b.CData(i,:) = [1 0 1];
end
end
I still get that weird chunk of 'Run' that isn't part of my data, in addition to my actual data. There is no data for 'Run' before the blue steps of Cycle.

Accedi per commentare.

Why you keep using this useless (removed) part of your code:
for i = 1:length(StepsS2.Time)
switch StepsS2.ActivityType(i)
case "Jump"
b.CData(i,:) = [1 0 0];
case "Run"
b.CData(i,:) = [0 1 1];
case "Squat"
b.CData(i,:) = [0 1 0];
case "Cycle"
b.CData(i,:) = [0 0 1];
otherwise
b.CData(i,:) = [1 0 1];
end
end
This is the complete code:
figure()
Labelit = {};
CL = [1 0 0; 0 1 1; 0 1 0; 0 0 1; 1 0 1];
LEG = {'Jump', 'Run', 'Squat', 'Cycle', 'Other'};
for ii = 1:5
b = bar(StepsS2.Time(ii), StepsS2.Steps(ii)); hold on
b.FaceColor = 'flat';
b.CData = CL(ii,:);
Labelit{ii} = LEG{ii};
legend(Labelit{:});
end

1 Commento

That code doesn't work, it's not pulling the data from StepsS2.ActivityType which gives each data point of time and steps a value of either jump, run, squat, cycle, or other. If I use solely the code you provided none of my data is actually there, just this random bar and the legend:

Accedi per commentare.

Categorie

Prodotti

Release

R2020a

Richiesto:

il 7 Lug 2021

Commentato:

il 8 Lug 2021

Community Treasure Hunt

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

Start Hunting!

Translated by