Hiding Plot Elements makes those elements appearing in the legend and the legend itself a much lighter shade than it should be. How to fix this?

3 views (last 30 days)
I am in the process of redoing a figure for a publication that needs to be changed up a bit based on reviewer comments. I have to create a plot legend that covers four different subplots and needs to include elements not included in all four subplots that all have two y-axes. To create the legend, I created an extra row of subplots and merged them to make a dummy subplot. I plotted generic versions of all the plot elements that need to be included in the legend, created the legend, and then hid the plot elements and the subplot itself so that the only thing still visible is the legend which is then dragged into the desired position. All went to plan except for the fact that the the symbols/lines/text in the legend appear to be several shades lighter than they should be. Briefly troubleshooting, I noticed hiding everything is what results in the lighter shades. The first version of the code I wrote to create the figure involved creating two different figures instead of the "two different subplots" and then I just cut and paste the legend into the figure separately outside of MATLAB. It would ideal just to get the plot and legend generated properly by the same code. Code below and figure attached. The code is very long, but the last 21 lines of code are of most interest for this question. Thank you in advance for any and all help.
set(0,'defaultfigurecolor','w');
figure(1)
colormap jet
h1 = subplot(5,5,[1,2,6,7]);
yyaxis left
area(hour1,sal1,'FaceColor',[0.8300 0.8300 0.8300],'EdgeColor',[0.8300 0.8300 0.8300],'LineStyle','-');
ylim([5.6 26.4]);
set(gca,'FontSize',10);
set(gca,'ycolor','k');
set(gca,'ytick',[8 12 16 20 24]);
ytix = get(gca,'ytick');
ytixlbl = regexp(sprintf('%.0f\n',ytix), '\n', 'split');
set(gca,'yticklabel',ytixlbl(1:end-1));
ylabel('Salinity','FontSize',10,'FontWeight','bold');
text(7.0,26.6,'a','FontSize',18,'FontWeight','bold');
str1 = {'01 June 2016'};
text(14.8,25,str1,'FontSize',10,'FontWeight','bold');
yyaxis right
scatter(hour1,ph1,50,'o','filled','MarkerFaceColor',[0 0 0.8]);
hold on
plot(hour1,ph1,'Color',[0 0 0.8],'LineWidth',1,'LineStyle','-');
hold off
ylim([-10.8 82.8]);
set(gca,'FontSize',10);
set(gca,'ycolor','k');
set(gca,'ytick',[0 18 36 54 72]);
ytix = get(gca,'ytick');
ytixlbl = regexp(sprintf('%.0f\n',ytix), '\n', 'split');
set(gca,'yticklabel',ytixlbl(1:end-1));
ylabel('[H^{+}_{T}] (nmol kg^{-1})','FontSize',10,'FontWeight','bold');
xlim([8.5 19.5]);
set(gca,'xtick',[9,11,13,15,17,19]);
xlabel('Hour','FontSize',10,'FontWeight','bold');
set(gca,'FontSize',10);
box on
h2 = subplot(5,5,[4,5,9,10]);
yyaxis left
area(hour1,sal1,'FaceColor',[0.8300 0.8300 0.8300],'EdgeColor',[0.8300 0.8300 0.8300],'LineStyle','-');
ylim([5.6 26.4]);
set(gca,'FontSize',10);
set(gca,'ycolor','k');
set(gca,'ytick',[8 12 16 20 24]);
ytix = get(gca,'ytick');
ytixlbl = regexp(sprintf('%.0f\n',ytix), '\n', 'split');
set(gca,'yticklabel',ytixlbl(1:end-1));
ylabel('Salinity','FontSize',10,'FontWeight','bold');
text(7.0,26.6,'b','FontSize',18,'FontWeight','bold');
str1 = {'01 June 2016'};
text(14.8,25,str1,'FontSize',10,'FontWeight','bold');
yyaxis right
scatter(hour1,dic1,75,'s','filled','MarkerFaceColor',[0.5500 0.7100 0.0000]);
hold on
scatter(hour1,ta1,60,'>','filled','MarkerFaceColor',[0.9100 0.4100 0.1700]);
hold on
plot(hour1,dic1,'Color',[0.5500 0.7100 0.0000],'LineWidth',1,'LineStyle','-');
hold on
plot(hour1,ta1,'Color',[0.9100 0.4100 0.1700],'LineWidth',1,'LineStyle','-');
hold off
ylim([1440 1960]);
set(gca,'FontSize',10);
set(gca,'ycolor','k');
set(gca,'ytick',[1500 1600 1700 1800 1900]);
ytix = get(gca,'ytick');
ytixlbl = regexp(sprintf('%.0f\n',ytix), '\n', 'split');
set(gca,'yticklabel',ytixlbl(1:end-1));
ylabel('DIC or TA (\mumol kg^{-1})','FontSize',10,'FontWeight','bold');
xlim([8.5 19.5]);
set(gca,'xtick',[9,11,13,15,17,19]);
xlabel('Hour','FontSize',10,'FontWeight','bold');
set(gca,'FontSize',10);
box on
h3 = subplot(5,5,[11,12,16,17]);
yyaxis left
area(hour2,sal2,'FaceColor',[0.8300 0.8300 0.8300],'EdgeColor',[0.8300 0.8300 0.8300],'LineStyle','-');
ylim([18.8 29.2]);
set(gca,'FontSize',10);
set(gca,'ycolor','k');
set(gca,'ytick',[20 22 24 26 28]);
ytix = get(gca,'ytick');
ytixlbl = regexp(sprintf('%.0f\n',ytix), '\n', 'split');
set(gca,'yticklabel',ytixlbl(1:end-1));
ylabel('Salinity','FontSize',10,'FontWeight','bold');
text(6.05,29.3,'c','FontSize',18,'FontWeight','bold');
str1 = {'02 August 2016'};
text(14.93,28.5,str1,'FontSize',10,'FontWeight','bold');
yyaxis right
scatter(hour2,ph2,50,'o','filled','MarkerFaceColor',[0 0 0.8]);
hold on
plot(hour2,ph2,'Color',[0 0 0.8],'LineWidth',1,'LineStyle','-');
hold off
ylim([-2 102]);
set(gca,'FontSize',10);
set(gca,'ycolor','k');
set(gca,'ytick',[10 30 50 70 90]);
ytix = get(gca,'ytick');
ytixlbl = regexp(sprintf('%.0f\n',ytix), '\n', 'split');
set(gca,'yticklabel',ytixlbl(1:end-1));
ylabel('[H^{+}_{T}] (nmol kg^{-1})','FontSize',10,'FontWeight','bold');
xlim([7.75 20]);
set(gca,'xtick',[8 10 12 14 16 18 20]);
xlabel('Hour','FontSize',10,'FontWeight','bold');
set(gca,'FontSize',10);
box on
p3 = get(h3,'Position');
p3(2) = p3(2)-0.015;
set(h3,'pos',p3);
h4 = subplot(5,5,[14,15,19,20]);
yyaxis left
area(hour2,sal2,'FaceColor',[0.8300 0.8300 0.8300],'EdgeColor',[0.8300 0.8300 0.8300],'LineStyle','-');
ylim([18.8 29.2]);
set(gca,'FontSize',10);
set(gca,'ycolor','k');
set(gca,'ytick',[20 22 24 26 28]);
ytix = get(gca,'ytick');
ytixlbl = regexp(sprintf('%.0f\n',ytix), '\n', 'split');
set(gca,'yticklabel',ytixlbl(1:end-1));
ylabel('Salinity','FontSize',10,'FontWeight','bold');
text(6.05,29.3,'d','FontSize',18,'FontWeight','bold');
str1 = {'02 August 2016'};
text(14.93,28.5,str1,'FontSize',10,'FontWeight','bold');
yyaxis right
scatter(hour2,dic2,75,'s','filled','MarkerFaceColor',[0.5500 0.7100 0.0000]);
hold on
scatter(hour2,ta2,60,'>','filled','MarkerFaceColor',[0.9100 0.4100 0.1700]);
hold on
plot(hour2,dic2,'Color',[0.5500 0.7100 0.0000],'LineWidth',1,'LineStyle','-');
hold on
plot(hour2,ta2,'Color',[0.9100 0.4100 0.1700],'LineWidth',1,'LineStyle','-');
hold off
ylim([1740 2260]);
set(gca,'FontSize',10);
set(gca,'ycolor','k');
set(gca,'ytick',[1800 1900 2000 2100 2200]);
ytix = get(gca,'ytick');
ytixlbl = regexp(sprintf('%.0f\n',ytix), '\n', 'split');
set(gca,'yticklabel',ytixlbl(1:end-1));
ylabel('DIC or TA (\mumol kg^{-1})','FontSize',10,'FontWeight','bold');
xlim([7.75 20]);
set(gca,'xtick',[8 10 12 14 16 18 20]);
xlabel('Hour','FontSize',10,'FontWeight','bold');
set(gca,'FontSize',10);
box on
p4 = get(h4,'Position');
p4(2) = p4(2)-0.015;
set(h4,'pos',p4);
h5 = subplot(5,5,[21,22,23,24,25]);
set(0,'defaultfigurecolor','w');
colormap jet
element1 = area(hour1,sal1,'FaceColor',[0.8300 0.8300 0.8300],'EdgeColor',[0.8300 0.8300 0.8300],'LineStyle','-');
hold on
element2 = plot(hour1,ph1,'-o','MarkerEdgeColor',[0 0 0.8],'LineWidth',1,'MarkerFaceColor',[0 0 0.8],'MarkerSize',7,'Color',[0 0 0.8]);
hold on
element3 = plot(hour1,dic1,'-s','MarkerEdgeColor',[0.5500 0.7100 0.000],'LineWidth',1,'MarkerFaceColor',[0.5500 0.7100 0.000],'MarkerSize',10,'Color',[0.5500 0.7100 0.0000]);
hold on
element4 = plot(hour1,ta1,'->','MarkerEdgeColor',[0.9100 0.4100 0.1700],'LineWidth',1,'MarkerFaceColor',[0.9100 0.4100 0.1700],'MarkerSize',7,'Color',[0.9100 0.4100 0.1700]);
hold off
plotleg = legend('Salinity','[H^{+}_{T}]','DIC','TA','Location','bestoutside','Orientation','vertical');
legend('boxon');
set(plotleg,'FontSize',13);
box off
axis off
set(element1,'Visible','off');
set(element2,'Visible','off');
set(element3,'Visible','off');
set(element4,'Visible','off');
set(h5,'Visible','off');

Accepted Answer

dpb
dpb on 24 Feb 2020
Edited: dpb on 26 Feb 2020
OK, wasn't too hard to take yours and make an example...
figure(1)
colormap jet
hAx(1) = subplot(5,5,[1,2,6,7]);
yyaxis left
ylim([5.6 26.4]);
set(gca,'FontSize',10);
set(gca,'ycolor','k');
set(gca,'ytick',[8 12 16 20 24]);
ytix = get(gca,'ytick');
ytixlbl = regexp(sprintf('%.0f\n',ytix), '\n', 'split');
set(gca,'yticklabel',ytixlbl(1:end-1));
ylabel('Salinity','FontSize',10,'FontWeight','bold');
text(7.0,26.6,'a','FontSize',18,'FontWeight','bold');
str1 = {'01 June 2016'};
text(14.8,25,str1,'FontSize',10,'FontWeight','bold');
hour1=linspace(9,19,7); sal1=[22 21 15 9 12 22 23];
hAr(1)=area(hour1,sal1,'FaceColor',[0.8300 0.8300 0.8300],'EdgeColor',[0.8300 0.8300 0.8300],'LineStyle','-');
yyaxis right
ph1=[8 10 72 68 8 7 2];
hL(1,1)=plot(hour1,ph1,'o-','MarkerSize',7,'MarkerFaceColor',[0 0 0.8],'Color',[0 0 0.8],'LineWidth',1);
ylim([-10.8 82.8]);
set(gca,'FontSize',10);
set(gca,'ycolor','k');
set(gca,'ytick',[0 18 36 54 72]);
ytix = get(gca,'ytick');
ytixlbl = regexp(sprintf('%.0f\n',ytix), '\n', 'split');
set(gca,'yticklabel',ytixlbl(1:end-1));
ylabel('[H^{+}_{T}] (nmol kg^{-1})','FontSize',10,'FontWeight','bold');
xlim([8.5 19.5]);
set(gca,'xtick',[9,11,13,15,17,19]);
xlabel('Hour','FontSize',10,'FontWeight','bold');
set(gca,'FontSize',10);
box on
hold on
dum1=nan(size(hour1));
hL(2,1)=plot(hour1,dum1,'s-','Color',[0.5500 0.7100 0.0000],'LineWidth',1,'MarkerSize',7,'MarkerFaceColor',[0.5500 0.7100 0.0000]);
hL(3,1)=plot(hour1,dum1,'>-','Color',[0.9100 0.4100 0.1700],'LineWidth',1,'MarkerSize',6,'MarkerFaceColor',[0.9100 0.4100 0.1700]);
hLg=legend([hAr; hL],'Salinity','[H^{+}_{T}]','DIC','TA','Location','bestoutside','Orientation','vertical');
return
The above adds the dummy linestyles from which to create the legend onto the first suplot(). Just use the handles to the line/area objects desired to get the right linestyles.
NB: You don't need scatter, just use the same properties of the line--that reduces clutter by quite a bit.
Above produces:
The size/location will come back when you use figure size than just the default figure window size here on screen.
The key item is the NaN data to put the desired linestyles on the figure for legend. This is a most useful "trick" to know for many purposes with HG.
  3 Comments
S_G
S_G on 25 Feb 2020
Thank you for the help. Fixed the figure per your recommenations and resubmitted the manuscript this afternoon.

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!