MATLAB Answers

1

legend text is transparent in MATLAB R2018a

Asked by Dylan Catlett on 22 Apr 2019
Latest activity Commented on by Dylan Catlett on 23 Apr 2019
Accepted Answer by Jan
I recently updated Matlab from R2015b to R2018a and now I am unable to re-create the legends on some of my plots. The problem is that in the new plots, the legend text is transparent. I've tried modifying properties of the legend manually to no avail.
Here's the R2015b code and resulting plot.
counter = 1;
figure(); hold on;
for i = 1:7;
stIDX = idx_allSt(:,i);
for j = 1:12;
mIDX = idx_allM{j};
idx = ismember(stIDX,mIDX);
idx = stIDX(idx);
scatter(all_AFs_ds(idx,3),all_AFs_ds(idx,4),st_symbols{i},'MarkerEdgeColor',monthly_colors(j,:));
end
counter = counter + 1;
end
% for making a station legend:
for i = 1:7;
h(i) = plot(0,0,st_symbols{i},'Color','k','visible','off');
end
l = legend(h, 'St 1', 'St 2', 'St 3', 'St 4', 'St 5', 'St 6', 'St 7');
ax = gca; ax.XLabel.String = 'PC 1 [mixed upwelling community -->]'; ax.YLabel.String = 'PC 2 [diatom bloom -->]';
% for making a custom colormap by month (still working on it):
c = colorbar; colormap(COLORS); caxis([1 12]); c.Ticks = 1:12;
c.TickLabels = {'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'};
% plot zero-lines:
x = ax.XLim; x = x(1):x(length(x)); y = zeros(length(x),1); plot(x,y,'k');
x = ax.YLim; x = x(1):x(length(x)); y = zeros(length(x),1); plot(y,x,'k');
title('Pigments - Mode 1 vs. Mode 2');
Screen Shot 2019-04-22 at 3.25.08 PM.png
You can see the legend has the typical black font, and that in the second for-loop in the above code snippet, I was able to create the legend so that it only included the 7 entries I wanted and displayed them all in black.
When I do this in R2018a (with a modified code snippet to attempt to manually set the text to appear black and/or bold), I get the following:
counter = 1;
figure(); hold on;
for i = 1:7;
stIDX = idx_allSt(:,i);
for j = 1:12;
mIDX = idx_allM{j};
idx = ismember(stIDX,mIDX);
idx = stIDX(idx);
scatter(all_AFs_ds(idx,3),all_AFs_ds(idx,4),st_symbols{i},'MarkerEdgeColor',monthly_colors(j,:));
end
counter = counter + 1;
end
ax = gca; ax.XLabel.String = 'PC 1'; ax.YLabel.String = 'PC 2';
% for making a station legend:
for i = 1:7;
h(i) = plot(0,0,st_symbols{i},'Color','k','visible','off');
end
% for making a custom colormap by month (still working on it):
c = colorbar; colormap(COLORS); caxis([1 12]); c.Ticks = 1:12;
c.TickLabels = {'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'};
% plot zero-lines:
x = ax.XLim; x = x(1):x(length(x)); y = zeros(length(x),1); plot(x,y,'k');
x = ax.YLim; x = x(1):x(length(x)); y = zeros(length(x),1); plot(y,x,'k');
title('R2018a');
l = legend(h, 'St 1', 'St 2', 'St 3', 'St 4', 'St 5', 'St 6', 'St 7');
Screen Shot 2019-04-22 at 3.31.15 PM.png
The legend text is now slightly transparent. Why? I've tried adding the following lines but the transparency in the legend text remains. I can't seem to find any explanations or work-arounds for this in R2018a, so any help would be much-appreciated.
l.HandleVisibility = 'off'; % have also tried 'on'
l.TextColor = 'k'; l.Color = 'w'; l.EdgeColor = 'k';
l.FontSize = 14; l.FontWeight = 'bold';

  0 Comments

Sign in to comment.

1 Answer

Answer by Jan
on 23 Apr 2019
Edited by Jan
on 23 Apr 2019
 Accepted Answer

Instead of plotting invisible points, use this:
h(i) = plot(NaN, NaN, st_symbols{i}, 'Color', 'k');
By the way, I have no idea why you assume, that this code migth help:
l.HandleVisibility = 'off'; % have also tried 'on'
l.TextColor = 'k'; l.Color = 'w'; l.EdgeColor = 'k';
l.FontSize = 14; l.FontWeight = 'bold';
The HandleVisibility controls, if the handle is hidden or not. This does not influence the optical appearence.

  1 Comment

Thank you, Jan, and apologies for the duplicate question.

Sign in to comment.