How to plot and print two graphs in a same page?

I have been trying two put two graphs in a single make and make it PDF. But the border of the pages is not equal on all the sides and the grpahs are tend to shift. the positions of the graphs are dictated by the 'PaperPosition' command which is applied after the 2nd graph. I need both the grpah on the same page with equal border margin and grpah lengths (height and width). I have added the code for clarification. Is there any way that I can use to solve the problem. The code is given below:
p=1
x=rand(90,1)
subplot(2,1,1)
plot(x)
grid on;
grid minor;
str = sprintf('Random data for experiment');
xlabel({'X-axis',str});
ylabel('Y-axis');
t=0:0.01:4*pi
f=1
y=5*sin(2*pi*f*t)
subplot(2,1,2)
plot(t,y)
grid on;
grid minor
str = sprintf('Sinewave for experiment');
xlabel({'Time(s)',str});
ylabel('Voltage(v)');
x_width1=8.7;
y_width1=11;
set(gcf, 'PaperPosition', [-0.1 0.4 x_width1 y_width1]);

7 commentaires

This looks good. What is the problem with this?
p=1;
x=rand(90,1);
subplot(2,1,1)
plot(x)
grid on;
grid minor;
str = sprintf('Random data for experiment');
xlabel({'X-axis',str});
ylabel('Y-axis');
t=0:0.01:4*pi;
f=1;
y=5*sin(2*pi*f*t);
subplot(2,1,2)
plot(t,y)
grid on;
grid minor
str = sprintf('Sinewave for experiment');
xlabel({'Time(s)',str});
ylabel('Voltage(v)');
I am trying to make a PDF with both the plots on the same page. I have been able to make the pdf using the subplot but the size and border of the page is not kept uniform. I need to set them up in a "US letter" page with both the graphs on the same page and equal height and width. How can I do that? I have been trying to set them using the follwoing command.
x_width1=8.7;
y_width1=11;
set(gcf, 'PaperPosition', [-0.1 0.4 x_width1 y_width1]);
But as usual, the border is not uniform and both the plots gets affetced by the command.
What function are you using to print to PDF?
I have used 'saveas' for the PDF generation.
Nikhil Sapre
Nikhil Sapre le 20 Avr 2022
Modifié(e) : Nikhil Sapre le 20 Avr 2022
Can you try using the print function and pass in the figure handle to it?
p=1;
x=rand(90,1);
subplot(2,1,1)
plot(x)
grid on;
grid minor;
str = sprintf('Random data for experiment');
xlabel({'X-axis',str});
ylabel('Y-axis');
t=0:0.01:4*pi;
f=1;
y=5*sin(2*pi*f*t);
subplot(2,1,2)
plot(t,y)
grid on;
grid minor
str = sprintf('Sinewave for experiment');
xlabel({'Time(s)',str});
ylabel('Voltage(v)');
set(gcf, 'PaperPosition', [-0.1 0.4 x_width1 y_width1]);
print(gcf, '-dpdf', 'test');
+1 @Nikhil Sapre (copy to answers section)
If you're using R2020a or later you could use exportgraphics but the figure borders will be trimmed.
Solved the problem by myself. Had to set position and x_width and y_width properly. Used 'saveas' command to save the figures as PDF. Thank you all.

Connectez-vous pour commenter.

 Réponse acceptée

Richard Quist
Richard Quist le 30 Avr 2022

0 votes

If you're using R2020a or later, you might want to try the exportgraphics command, which produces tightly cropped output (eliminating excess white space) in most cases. For PDF and other vector output formats (like EPS), you can ensure this by specifying the ContentType parameter:
% In R2020a and later, use the exportgraphics command and specify the 'ContentType' as 'vector':
exportgraphics(fig, 'output.pdf', 'ContentType', 'vector');

Plus de réponses (0)

Catégories

En savoir plus sur Data Exploration dans Centre d'aide et File Exchange

Produits

Version

R2021a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by