different label color in plot legend

42 次查看(过去 30 天)
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 个评论
DGM
DGM 2021-10-5
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
Alright, thank you managed to solve it using this method!

请先登录,再进行评论。

回答(1 个)

Anshika Chaurasia
Anshika Chaurasia 2021-10-8
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.

类别

Help CenterFile Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息

标签

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by