different label color in plot legend

3 Ansichten (letzte 30 Tage)
DANIEL KONG LEN HAO
DANIEL KONG LEN HAO am 5 Okt. 2021
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 Kommentare
DGM
DGM am 5 Okt. 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 am 8 Okt. 2021
Alright, thank you managed to solve it using this method!

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Anshika Chaurasia
Anshika Chaurasia am 8 Okt. 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.

Kategorien

Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by