Adding Legend to Bar Graph

조회 수: 26 (최근 30일)
Gillian Murray
Gillian Murray 2021년 7월 7일
댓글: Gillian Murray 2021년 7월 8일
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'

답변 (3개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 7월 7일
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
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 7월 7일
A=StepsS2.Time; B=StepsS2.Steps
Or
...
b= bar(StepsS2.Time(ii), StepsS2.Steps(ii)); hold on
...
Gillian Murray
Gillian Murray 2021년 7월 7일
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

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


Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 7월 8일
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
Gillian Murray
Gillian Murray 2021년 7월 8일
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.

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


Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 7월 8일
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
Gillian Murray
Gillian Murray 2021년 7월 8일
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:

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

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by