Line colour and style
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi,
I need to set the colours of my legend and line from yellow and black to red and blue; They should be red and blue alrady as the code uses 'r' and 'b', but it comes up yellow and blue. The left leg needs to be red and right blue for legend and graph. The graph lines also need to be solid for walking and dashed for running, can anyone help?
Full code and data attached, screenshot is just the example I'm getting.
Thanks.
%% Load in data
%clear workspace
clearvars
%Prompt user for filename
[fname, pname] = uigetfile('*.csv');
%Create fully-formed filename as a string
filename = fullfile(pname, fname);
%Check that file exists
assert(exist(filename,'file')==2, '%s does not exist.', filename);
%Read in the data, skipping the first row - save as a varaible "data"
data = csvread(filename);
%% Define vectors
t=0:2:100; % create time series from 0 to 100 in increments of 2 to represent 0-100
% of the gait cycle
%Associate data from the csv file to variable names.
RHipAngles_S_Walk = data(:,1);
LHipAngles_S_Walk = data(:,2);
RKneeAngles_S_Walk = data(:,3);
LKneeAngles_S_Walk = data(:,4);
RAnkleAngles_S_Walk = data(:,5);
LAnkleAngles_S_Walk = data(:,6);
RHipAngles_S_Run = data(:,7);
LHipAngles_S_Run = data(:,8);
RKneeAngles_S_Run = data(:,9);
LKneeAngles_S_Run = data(:,10);
RAnkleAngles_S_Run = data(:,11);
LAnkleAngles_S_Run = data(:,12);
% input the percentage of the gait cycle foot off occurred for left and right
strides.
FootOff_Left_Walk = 55;
FootOff_Right_Walk = 56;
FootOff_Left_Run = 33;
FootOff_Right_Run = 32;
%% Find Maximum and minimum values
% set the maximum and minimum, rounding to the closest 10 for the hip, knee
% and ankle. ceil() rounds up, floor() rounds down
%Find Maximums
HipMax_S_Walk = ceil(max(max([LHipAngles_S_Walk RHipAngles_S_Walk]))/10)*10;
KneeMax_S_Walk = ceil(max(max([LKneeAngles_S_Walk RKneeAngles_S_Walk]))/10)*10;
AnkleMax_S_Walk = ceil(max(max([LAnkleAngles_S_Walk RAnkleAngles_S_Walk]))/10)*10;
HipMax_S_Run = ceil(max(max([LHipAngles_S_Run RHipAngles_S_Run]))/10)*10;
KneeMax_S_Run = ceil(max(max([LKneeAngles_S_Run RKneeAngles_S_Run]))/10)*10;
AnkleMax_S_Run = ceil(max(max([LAnkleAngles_S_Run RAnkleAngles_S_Run]))/10)*10;
%Find Minimums
HipMin_S_Walk = floor(min(min([LHipAngles_S_Walk RHipAngles_S_Walk]))/10)*10;
KneeMin_S_Walk = floor(min(min([LKneeAngles_S_Walk RKneeAngles_S_Walk]))/10)*10;
AnkleMin_S_Walk = floor(min(min([LAnkleAngles_S_Walk RAnkleAngles_S_Walk]))/10)*10;
HipMin_S_Run = floor(min(min([LHipAngles_S_Run RHipAngles_S_Run]))/10)*10;
KneeMin_S_Run = floor(min(min([LKneeAngles_S_Run RKneeAngles_S_Run]))/10)*10;
AnkleMin_S_Run = floor(min(min([LAnkleAngles_S_Run RAnkleAngles_S_Run]))/10)*10;
%% Plot data
%Plot the sagittal plane angles of the hip, knee and ankle
figure('units','centimeters','position',[5 5 16 10]) %create a figure 5cm from the
left and down from the bottom left of the screen, 16 cm wide x 10 cm tall
SplotRows = 2; %How many rows of subplots
SplotCols = 4; %number of columns of subplots
% Hip Flexion Walking
subplot(SplotRows,SplotCols,1)
plot(t, LHipAngles_S_Walk,'k-')
xlim([0 100])
xlabel('Stride (%)')
ylabel(['Hip Flexion (' char(176) ')'])
ylim([HipMin_S_Walk HipMax_S_Walk])
hold on
plot(t, RHipAngles_S_Walk,'y-')
plot([FootOff_Left_Walk FootOff_Left_Walk], [HipMin_S_Walk
HipMax_S_Walk],'r:');
plot([FootOff_Right_Walk FootOff_Right_Walk], [HipMin_S_Walk
HipMax_S_Walk],'b:');
plot([0 100], [0 0],'k-');
hold off
set(gca,'FontSize',8) % Set FontSize to 8
% Knee Flexion Walking
subplot(SplotRows,SplotCols,2)
plot(t, LKneeAngles_S_Walk,'k-')
xlim([0 100])
xlabel('Stride (%)')
ylabel(['Knee Flexion (' char(176) ')'])
ylim([KneeMin_S_Walk KneeMax_S_Walk])
hold on
plot([FootOff_Left_Walk FootOff_Left_Walk], [KneeMin_S_Walk
KneeMax_S_Walk],'k:');
plot([FootOff_Right_Walk FootOff_Right_Walk], [KneeMin_S_Walk
KneeMax_S_Walk],'b:');
plot([0 100], [0 0],'k-');
plot(t, RKneeAngles_S_Walk,'y-')
hold off
set(gca,'FontSize',8) % Set FontSize to 8
% Ankle Dorsiflexion Walking
subplot(SplotRows,SplotCols,3)
plot(t, LAnkleAngles_S_Walk,'k-')
xlim([0 100])
xlabel('Stride (%)')
ylabel(['Ankle Dorsiflexion (' char(176) ')'])
ylim([AnkleMin_S_Walk AnkleMax_S_Walk])
hold on
plot([FootOff_Left_Walk FootOff_Left_Walk], [AnkleMin_S_Walk
AnkleMax_S_Walk],'r:');
plot([FootOff_Right_Walk FootOff_Right_Walk], [AnkleMin_S_Walk
AnkleMax_S_Walk],'b:');
plot([0 100], [0 0],'k-');
plot(t, RAnkleAngles_S_Walk,'y-')
hold off
set(gca,'FontSize',8) % Set FontSize to 8
% Hip Flexion Running
subplot(SplotRows,SplotCols,5)
plot(t, LHipAngles_S_Run,'k-')
xlim([0 100])
xlabel('Stride (%)')
ylabel(['Hip Flexion (' char(176) ')'])
ylim([HipMin_S_Run HipMax_S_Run])
hold on
plot([FootOff_Left_Run FootOff_Left_Run], [HipMin_S_Run HipMax_S_Run],'r:');
plot([FootOff_Right_Run FootOff_Right_Run], [HipMin_S_Run HipMax_S_Run],'b:');
plot([0 100], [0 0],'k-');
plot(t, RHipAngles_S_Run,'y-')
hold off
set(gca,'FontSize',8) % Set FontSize to 8
% Knee Flexion Running
subplot(SplotRows,SplotCols,6)
plot(t, LKneeAngles_S_Run,'k-')
xlim([0 100])
xlabel('Stride (%)')
ylabel(['Knee Flexion (' char(176) ')'])
ylim([KneeMin_S_Run KneeMax_S_Run])
hold on
plot([FootOff_Left_Run FootOff_Left_Run], [KneeMin_S_Run KneeMax_S_Run],'r:');
plot([FootOff_Right_Run FootOff_Right_Run], [KneeMin_S_Run
KneeMax_S_Run],'b:');
plot([0 100], [0 0],'k-');
plot(t, RKneeAngles_S_Run,'y-')
hold off
set(gca,'FontSize',8) % Set FontSize to 8
% Ankle Flexion Running
subplot(SplotRows,SplotCols,7)
plot(t, LAnkleAngles_S_Run,'k-')
xlim([0 100])
xlabel('Stride (%)')
ylabel(['Ankle Dorsiflexion (' char(176) ')'])
ylim([AnkleMin_S_Run AnkleMax_S_Run])
hold on
plot(t, RAnkleAngles_S_Run,'y-')
plot([FootOff_Left_Run FootOff_Left_Run], [AnkleMin_S_Run
AnkleMax_S_Run],'r:');
plot([FootOff_Right_Run FootOff_Right_Run], [AnkleMin_S_Run
AnkleMax_S_Run],'b:');
plot([0 100], [0 0],'k-');
hold off
set(gca,'FontSize',8) % Set FontSize to 8
% add a legend using the final data for line colour/style
legend({'Left stride','Right stride','Left foot off', 'Right foot
off'},'units','centimeters','Position',[13 8 1 1])

1 comentario
Image Analyst
el 21 de Feb. de 2022
Make it easy for us to help you. I see you calling plot() to plot curves in red, blue, black, and yellow, but honestly I can't follow it unless I have the data. Read this
and then attach the vectors in a .mat file.
In the meantime, add 'LineWidth', 3 to your plot commands just in case it's an artifact of really thin lines being so close together on your screen.
Respuestas (1)
DGM
el 21 de Feb. de 2022
Editada: DGM
el 21 de Feb. de 2022
Without data, I'm just going to strip out everything that's irrelevant.
This is a routine demonstration of the fact that legend() can't guess what plot objects you're trying to label. I mean, it does try, but you should expect it to routinely fail, especially if you don't even specify the labels in the same order that the plot objects were created in.
- Write orderly, formatted code
- Keep track of the handles to the relevant plot objects
- Give legend() both the handles and the labels in the correct order
t = [0 100];
% Hip Flexion Running
h(1) = plot(t, [0 100],'k-'); hold on % Lhip angles
h(2) = plot(t, [0 80],'y-'); % Rhip angles
h(3) = plot(t, [0 60],'r:'); % LF off
h(4) = plot(t, [0 40],'b:'); % RF off
h(5) = plot(t, [0 20],'k-'); % ???
hold off
xlabel('Stride (%)')
ylabel(['Hip Flexion (' char(176) ')'])
title('Running');
% add a legend using the final data for line colour/style
lblstr = {'Left stride','Right stride','Left foot off', 'Right foot off'};
legend(h,lblstr)
0 comentarios
Ver también
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
