Legend is not displayed in my plot
조회 수: 39 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Raghunathraju
2023년 3월 31일
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
Image Analyst
2023년 3월 31일
Try
title('Juni','Interpreter', 'latex', 'FontSize', 14);
or else use
text(x, y, 'Juni');
to place it wherever you want.
Raghunathraju
2023년 3월 31일
추가 답변 (1개)
Dyuman Joshi
2023년 3월 31일
%% 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';
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Axes Appearance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!