Legend is not displayed in my plot
Show older comments
I tried to plot a data where there are two x-axis. Therefore I created plot, and two x-axis and then when I try to display the legend, it is not shown.
Problem I found
- Legend is stacked behind the axes and therefore not visible
Things I tried
- Getting legend to the top using Uistack command
- Bringing the axes layers to the bottom using the following command
set(ax1, 'Layer', 'bottom'); % set axes to be behind the legend
set(ax2, 'Layer', 'bottom'); % set axes to be behind the legend
This also didn't work.
Below is my following code. I will attach the input data along with this file.
If my understanding is correct, the legend is stacked on the back and we need to bring them forward. I need help with this.
%% Plot Raw data with two x-axis
load("matlab.mat")
lim_1 = 600; % Enter the limits here for the plot
lim_2 = 900; % Enter the limits here for the plot
wv_tik = lim_1:50:lim_2; % Modify this if needed
E_tik = round(1240./wv_tik,2);
figure;
for i = 1:length(Data)
plot(Data(i).data(:,1), Data(i).data(:,2), LineWidth=1.5);
ax1 = gca; % get current axis
ax1.XColor = 'k'; % set bottom x-axis color to black
ax1.YColor = 'k'; % set left y-axis color to blue
ax1.XLim = [lim_1 lim_2];
ax1.XMinorTick = 'on';
hold on
% Add a second x-axis on top
ax2 = copyobj(ax1,gcf);
ax2.XColor = 'k'; % set top x-axis color to red
ax2.XTick = wv_tik;
ax2.XLim = [lim_1 lim_2];
ax2.XTickLabel = E_tik; % remove tick labels for top x-axis
ax2.XAxisLocation = 'top'; % set location of top x-axis
ax2.YTick = []; % remove tick marks for top x-axis
ax2.XMinorTick = 'on';
% Set the Visible property of the axes object to 'on'
% set(ax1, 'Visible', 'on');
% set(ax2, 'Visible', 'on');
end
hold off
xlabel('Wavelength ($\mu$m)', 'Interpreter', 'latex', 'FontSize', 14);
ylabel('PL ', 'Interpreter', 'latex', 'FontSize', 14);
set(gca,'FontSize',14); % set font size for the axis labels and tick marks
legend('Juni','Interpreter', 'latex', 'FontSize', 14, 'Location', 'eastoutside');
set(ax1, 'Layer', 'bottom'); % set axes to be behind the legend
set(ax2, 'Layer', 'bottom'); % set axes to be behind the legend
Accepted Answer
More Answers (1)
%% Plot Raw data with two x-axis
load("matlab.mat")
lim_1 = 600; % Enter the limits here for the plot
lim_2 = 900; % Enter the limits here for the plot
wv_tik = lim_1:50:lim_2; % Modify this if needed
E_tik = round(1240./wv_tik,2);
figure
plot(Data.data(:,1), Data.data(:,2), 'LineWidth',1.5)
ax1 = gca;
ax1.XColor = 'k'; % set bottom x-axis color to black
ax1.YColor = 'b'; % set left y-axis color to blue
ax1.XLim = [lim_1 lim_2];
ax1.XMinorTick = 'on';
xlabel(ax1,'Wavelength ($\mu$m)', 'Interpreter', 'latex', 'FontSize', 14);
ylabel(ax1,'PL', 'Interpreter', 'latex', 'FontSize', 14);
legend(ax1,'Juni','Interpreter', 'latex', 'FontSize', 14)%, 'Location', 'eastoutside')
%Create a new axes at same position rather than copying
%and modify it accordingly
ax2=axes('Position',ax1.Position);
ax2.XAxisLocation = 'top'; % set location of top x-axis
ax2.XColor = 'r'; % set top x-axis color to red
ax2.XLim = [lim_1 lim_2];
ax2.XTick = wv_tik;
ax2.XTickLabel = E_tik;
ax2.XMinorTick='on';
ax2.YAxisLocation = 'right'; % set location of top y-axis
ax2.YColor = 'y';
ax2.YTick = [];
ax2.Color = 'none';
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
