Legend is not displayed in my plot

39 visualizaciones (últimos 30 días)
Master
Master el 31 de Mzo. de 2023
Respondida: Dyuman Joshi el 31 de Mzo. de 2023
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

Respuesta aceptada

Raghunathraju
Raghunathraju el 31 de Mzo. de 2023
Hi
As per my understanding you want to display legend in your plot, but you are unable to see it as it is behind the axes ax2.It is because of the differences in dimensions of ax1 and ax2 .
You can simply do the following change in the code
ax2.Position=ax1.Position;
I can demonstrate the same with some changes in your code.
%% 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);
end
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';
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');
ax2.Position=ax1.Position;
Now you can see your expected result. I hope this resolves the issue.
  3 comentarios
Image Analyst
Image Analyst el 31 de Mzo. de 2023
Try
title('Juni','Interpreter', 'latex', 'FontSize', 14);
or else use
text(x, y, 'Juni');
to place it wherever you want.
Raghunathraju
Raghunathraju el 31 de Mzo. de 2023
You can make ax2 as the parent of legend to get the legend on top of the axis ax2.
Try something like this
legend(ax2,'Juni','Interpreter', 'latex', 'FontSize', 14);
Fore more information refer to axes and legend

Iniciar sesión para comentar.

Más respuestas (1)

Dyuman Joshi
Dyuman Joshi el 31 de Mzo. de 2023
%% 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';

Categorías

Más información sobre Axes Appearance en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by