Matlab plot .eps file position legend is not same as in matlab plot

3 views (last 30 days)
Hello,
I want to save my plot as a .eps file. But when I open then the .eps file the legend is always inside the plot and not
in the right upper corner as in the real plot. See the picutres. How can I change that also in the .eps file the legend
is in the right upper corner?
Matlab plot:
.eps file:
Code:
M1 = readmatrix('1111');
M2 = readmatrix('2222');
M3 = readmatrix('3333');
M4 = readmatrix('4444');
M2(:,1) = M2(:,1) + M1(end,1) + 4;
M3(:,1) = M3(:,1) + M2(end,1) + 4;
M4(:,1) = M4(:,1) + M3(end,1) + 4;
M = [M1; M2; M3; M4];
% plot force data
subplot(2,1,1)
plot(M(:,1)/1000,M(:,2),'color',' [0, 0.4470, 0.7410]')
hold on
plot(M(:,1)/1000,M(:,3))
hold on
plot(M(:,1)/1000,M(:,4))
hold on
legend('Fx','Fy','Fz', 'Location', 'northeast')
grid on
h_xlabel = xlabel ({ '$t$\,/\,s'});
set(h_xlabel,'Interpreter','latex');
h_ylabel = ylabel ({'$F$\,/\,N'});
set(h_ylabel,'Interpreter',' latex');
% plot torque data
subplot(2,1,2)
plot(M(:,1)/1000,M(:,5),'color',' [0, 0.4470, 0.7410]')
hold on
plot(M(:,1)/1000,M(:,6))
plot(M(:,1)/1000,M(:,7))
legend('Mx','My','Mz', 'Location', 'northeast')
grid on
h_xlabel = xlabel ({ '$t$\,/\,s'});
set(h_xlabel,'Interpreter','latex');
h_ylabel = ylabel ({'$M$\,/\,Nmm'});
set(h_ylabel,'Interpreter',' latex');
grid on
h_xlabel = xlabel ({ '$t$\,/\,s'});
set(h_xlabel,'Interpreter','latex');
h_ylabel = ylabel ({'$s$\,/\,mm'});
set(h_ylabel,'Interpreter',' latex');
set(gcf,'units','centimeters');
set(gca,'FontSize',10,'FontName','Times');
print("title",'-depsc2');
  2 Comments
Jan
Jan on 5 May 2022
Edited: Jan on 5 May 2022
"But when I open then the .eps file the legend is always inside the plot and not in the right upper corner as in the real plot." - I cannot confirm this.The legend is at the same position on the screen and in the EPS. But the resolutions are different and this influences the vertical limits of the axes and the font size of the legende and the tick labels.
Try if this helps already:
print("title", '-depsc2', '-r0');
If not, try the modern exportgraphics .
Mark S
Mark S on 5 May 2022
Edited: Mark S on 5 May 2022
I have tried that but also then the legend is inside of the plot in the .eps file. I have used now another solution. I have put the legend in the southwest corner. So that works fine for me.

Sign in to comment.

Answers (1)

Sudarsanan A K
Sudarsanan A K on 3 Oct 2023
Hello Mark,
I understand that you are trying to add legends to the subplots where your concern is with the interfering legend box with the data lines within the plot. Also, you are particularly interested in positioning the legend in the upper-right corner of the figure and not to interfere it with the plots.
The issue that you are facing is due to insufficient size (or dimension) of the figure to contain the legend box with the specified font size of 10. One possible solution is to specify the figure size as follows before plotting the data.
figure('Position', [100 100 800 600]);
where, the array [100 100 800 600] specifies the position and size of the figure window. Each element of the array corresponds to a specific parameter:
  • The first element (100) specifies the horizontal position of the figure window from the left side of the screen.
  • The second element (100) specifies the vertical position of the figure window from the bottom of the screen.
  • The third element (800) specifies the width of the figure window.
  • The fourth element (600) specifies the height of the figure window.
For the given values, the figure window will be positioned 100 pixels from the left side of the screen, 100 pixels from the bottom of the screen, with a width of 800 pixels and a height of 600 pixels. Adjusting these values allows you to control the position and size of the figure window on your screen and this helps to avoid the cluttering with the legend box. The given parameters create an .eps file as given below.
I assume that you are also interested in positioning the legend box outside the plot, which is an alternative to avoid the cluttering in the plot. For this you can utilise 'Location' attribute of the 'legend' function as follows.
legend('Fx','Fy','Fz', 'Location', 'northeastoutside')
legend('Mx','My','Mz', 'Location', 'northeastoutside')
which will give you an .eps figure as shown in figure below.

Tags

Community Treasure Hunt

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

Start Hunting!