Clear Filters
Clear Filters

different label color in plot legend

24 views (last 30 days)
There is a bug in MATLAB where after I have plotted my figure, the display within the legend of the plot for tidal volume displays a black line instead of a red circle that was plotted for it. Any way to solve this solution?
% Plotting the actual volume against time plot
figure(3)
hold on
plot(time,V,'b-') % Plot volume figure
plot(time,yzero,'k-') % Plot zero line
plot(time(pos),tv,'ro') % Plot tidal volume
% Labelling the figure
xlim([time(1) time(end)])
xlabel('Time (s)')
ylabel('Volume (L)')
title('Volume against time plot')
legend('Actual Volume plot','Zero Line','Tidal Volume')
hold off
  3 Comments
DGM
DGM on 5 Oct 2021
I see you're using R2018b. Your code works as-is in R2015b and R2019b for me, so idk what's up there.
Working off of Mathieu's comment, it's often helpful to explicitly specify the objects to which the legend should refer. This is generally the way I recommend. There are other ways, but iirc, this would be the most compatible with older versions.
% dummy data
time = 0:0.1:2.5;
V = 0.7*sin(1.3*time);
[tv,pos] = max(V);
yzero = zeros(size(V));
% Plotting the actual volume against time plot
%figure(3)
hold on
% store the plot object handles
h(1) = plot(time,V,'b-'); % Plot volume figure
h(2) = plot(time,yzero,'k-'); % Plot zero line
h(3) = plot(time(pos),tv,'ro'); % Plot tidal volume
% Labelling the figure
% xlim([time(1) time(end)]) % not very useful (?)
xlabel('Time (s)')
ylabel('Volume (L)')
title('Volume against time plot')
% give the handles vector to legend()
legend(h,{'Actual Volume plot','Zero Line','Tidal Volume'})
hold off
DANIEL KONG LEN HAO
DANIEL KONG LEN HAO on 8 Oct 2021
Alright, thank you managed to solve it using this method!

Sign in to comment.

Answers (1)

Anshika Chaurasia
Anshika Chaurasia on 8 Oct 2021
Hi,
I ran the above code in R2018b and it's working fine.
clc
clearvars
close all
% dummy data
time = 0:0.1:2.5;
V = 0.7*sin(1.3*time);
[tv,pos] = max(V);
yzero = zeros(size(V));
% Plotting the actual volume against time plot
figure(3)
hold on
plot(time,V,'b-') % Plot volume figure
plot(time,yzero,'k-') % Plot zero line
plot(time(pos),tv,'ro') % Plot tidal volume
% Labelling the figure
xlim([time(1) time(end)])
xlabel('Time (s)')
ylabel('Volume (L)')
title('Volume against time plot')
legend('Actual Volume plot','Zero Line','Tidal Volume')
hold off
If you are still facing the issue then share the script with us to reproduce the issue at our end.

Tags

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!