legend line colors don't change

10 views (last 30 days)
Ethan Leonard
Ethan Leonard on 18 May 2022
Commented: Ethan Leonard on 19 May 2022
I've been trying to edit my plots for dark contrast, so with the help of matlab answers, I wrote some plotting functions.
Since matlab's default choice for plotting colors is usually a dark color, I flip the color contrast in each line.
The problem is that when I try to add a legend, the it keeps the old colors, even though I'm adding the legend afterwards.
Here's my code, complete with the application I made it for. Skim to the bottom to see the plotting functions (the beginning isn't relevant, it's just so we are looking at the same thing). (If you just want to cut to the chase, skip to the comments at the very end.)
Thanks for being awesome.
% THE APPLICATION: THIS IS NOT THE PART YOU NEED TO READ
clear
close all
k = 0.606;
A = 0.005145;
dx = 0.0025;
m = 0.237;
c = 4.187;
A = [0 1/(m*c);-k*A/dx 0];
B= [1;0];
C = [1 0];
D = 0;
[b,a]=ss2tf(A,B,C,D);
H=tf(b,a);
Hdisc = c2d(H,0.1,'foh');
blackfig() %<------------------------FIRST FUNCTION CALL
impulse(H,'c-',Hdisc,'g-.',1.5)
lightlabels('Continuous','Discrete') %<------------------------SECOND FUNCTION CALL, WITH LEGEND
% THE PLOTTING FUNCTIONS
function blackfig()
% creates a black figure window with white axes. credit to the matlab answers
% community
figure1=figure('Color',[0 0 0],'InvertHardcopy','off');
axes1 = axes('Parent',figure1,...
'YColor',[1 1 1],...
'XColor',[1 1 1],...
'Color',[0 0 0]);
hold(axes1,'all');
end
function lightlabels(varargin) %I only add input variables if there is a legend.
% This changes the colors of the axes, grid, labels, and title. Credit to
% the matlab answers community.
ax=gca;
ax.YColor = 'y';
ax.XColor = 'y';
ax.GridColor = 'w';
ax.GridAlpha = .75;
ax = findall(gcf(),'Type','axes');
set(get(ax(1),'XLabel'),'Color','y')
set(get(ax(1),'YLabel'),'Color','y')
set(get(ax(1),'Title'),'Color','y')
% -------------------------------------------------
% This part I wrote, so it's definitely the part that went wrong.
% I'm trying to find the lines that draw the plot, then invert them with
% abs(color-[1 1 1]). The resultant colors are fine with me (dark colors
% tend to become bright), but I can't seem to get the legend to catch up.
klines=findobj('Type','Line','-not','Tag','StemLines'); %collecting the line objects that are made by my plots
% colors = zeros(length(klines),3); %to put the colors in later, but I don't actually know how to pass this
% into the legend command later. It remains unused.
for i=1:length(klines) %for as many lines as are in the plot
color=get(klines(i),'Color'); %get the color for this line
% colors(i,:)=color; %fill the colors array, but this remains unused
set(klines(i),'Color',abs(color-[1 1 1])); %invert the color and update the graph. this part appears to work.
end
if(size(varargin)~=0) %if legend names were passed in
legend(varargin(1,:),'TextColor','y','EdgeColor','y') %put them all into a legend. the names get pass in,
% the text and edge colors can be updated as shown, but I couldn't figure out how to find and change the line
% colors.
end
grid on
end

Accepted Answer

DGM
DGM on 18 May 2022
Edited: DGM on 18 May 2022
You need to give the line object handles to legend() to be sure it's picking the right objects.
% THE APPLICATION: THIS IS NOT THE PART YOU NEED TO READ
clear
close all
k = 0.606;
A = 0.005145;
dx = 0.0025;
m = 0.237;
c = 4.187;
A = [0 1/(m*c);-k*A/dx 0];
B= [1;0];
C = [1 0];
D = 0;
[b,a]=ss2tf(A,B,C,D);
H=tf(b,a);
Hdisc = c2d(H,0.1,'foh');
blackfig() %<------------------------FIRST FUNCTION CALL
impulse(H,'c-',Hdisc,'g-.',1.5)
lightlabels('Continuous','Discrete') %<------------------------SECOND FUNCTION CALL, WITH LEGEND
% THE PLOTTING FUNCTIONS
function blackfig()
% creates a black figure window with white axes. credit to the matlab answers
% community
figure1 = figure('Color',[0 0 0],'InvertHardcopy','off');
axes1 = axes('Parent',figure1,...
'YColor',[1 1 1],...
'XColor',[1 1 1],...
'Color',[0 0 0]);
hold(axes1,'all');
end
function lightlabels(varargin) %I only add input variables if there is a legend.
% This changes the colors of the axes, grid, labels, and title. Credit to
% the matlab answers community.
ax=gca;
ax.YColor = 'y';
ax.XColor = 'y';
ax.GridColor = 'w';
ax.GridAlpha = .75;
ax = findall(gcf(),'Type','axes');
set(get(ax(1),'XLabel'),'Color','y')
set(get(ax(1),'YLabel'),'Color','y')
set(get(ax(1),'Title'),'Color','y')
% -------------------------------------------------
% This part I wrote, so it's definitely the part that went wrong.
% I'm trying to find the lines that draw the plot, then invert them with
% abs(color-[1 1 1]). The resultant colors are fine with me (dark colors
% tend to become bright), but I can't seem to get the legend to catch up.
klines = findobj('Type','Line','-not','Tag','StemLines'); %collecting the line objects that are made by my plots
for i = 1:length(klines) %for as many lines as are in the plot
klines(i).Color = 1-klines(i).Color; % invert
end
if(size(varargin)~=0) %if legend names were passed in
% need to get the handles for the line objects
% -- but only the ones of interest
legend(klines(2:3),varargin(1,:),'TextColor','y','EdgeColor','y')
end
grid on
end
Alternatively, you can just invert the color spec in your call to impulse() or impulseplot() and avoid the need to invert those particular objects later
impulse(H,'r-',Hdisc,'m-.',1.5)
  3 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!