Problems with plotting to a pdf

hFig = gcf();
set (hFig, 'PaperType', 'a4');
set (hFig, 'PaperOrientation', 'landscape');

plotyy(time, pos, time, posPID);
xlabel('Time (sec)');
titletext = ["Position and PWM versus time ", dattim];
legStr = {'Position', 'PWM'};
legend (legStr);
print("Position.pdf", "-landscape", "-color", "-dpdf")

Produces a graphic window that looks like:

but the pdf file has a small image in the centre of the page that is truncated top/bottom and left/right

What am I doing wrong (or not doing). I expected the graph to be scaled to fit the page.

PS how to label the right hand Y-axis?
Thanks, David

I had this problem, too. I don’t have the code on hand right now. But check out the figure properties like “paperposition”, “paperunits”, “papersize” and the like.

It helps if you can provide a self-contained example that demonstrate the problem.
I tried replacing plotyy in you code with “demo plotyy 1” and I do not see the problem.

Also, note that “time” is a built-in function in octave. By defining your own variable with the same
name you shadowed this function which may lead to some problems.

If you can dig out your solution that would help a lot.


As previously requested, it would help if you could provide a self contained example. A few other advises/requests:

  • Avoid shadowing builtin functions, time
  • Avoid working with data that have a large offset as compared to their relative variance (like your pos variable), because the opengl renderer uses single precision floting points and sometimes behaves weird with such data,
  • When debugging, make sure you don’t use non default values for some graphics properties, i.e., get (0, "default") should return an empty struct,
  • Attach the resulting PDF file rather than a screenshot, so that we can inspect it.

Anyway, if I replace your variables time, pos, posPID, and titletext with fake data, your code works for me. The output PDF files has the correct paper type (A4) and orientation with the full plot centered on the page (see Position.pdf (5.6 KB)).

Hmmm Windows or Linux? Could that be relevant?

My full code is test.m attached
test.m (1.6 KB)
Data file attached
Servos20220109_143313.csv (31.1 KB)
Output pdf attached
Position.pdf (17.6 KB)

Could it be relevant the that graphics window with the displayed plot is a) square and b) small

Also FWIW your pdf has a SMALL plot centred on the page. I want the plot to FILL the printable area like this:
Servos20220109_140948.pdf (24.0 KB)

test is also a builtin function :slight_smile:.

Yes, if I run your example on a very small figure (e.g. 250x250 pixels) I can reproduce the issue in printout as well as onscreen. Can you file a bug report here?

What is resolution of your computer screen? Do you use some hi-DPI scaling?

Is this a bug or should I do something to set the size of the figure from the pagesize? It was a complete surprise to me that the graph wasn’t scaled to fit the pagesize.

My screen resolution in 3480*2160 with a zoom factor of 125%.

The fact that the labels are cropped is a bug but the fact that the plot size is the same as onscreen is not (see doc ("figure paperpositionmode")). If you want to change the size of the plot in printout, see doc ("figure paperposition"). If you want the plot to fill the entire page then you can use the -fillpage option for the print command, see doc print.

If you do “test subplot” (make sure the “test” is not the name of your scripts!) do
you get any failures?

Should we apply DPI scale to the default figure size?

So I created the figure visible off, and called print with -fillpage which created a PDF file that is mostly right.

There is however a small problem with the 1st plot of the two on the page - the line at the top has been clipped
Servos20220109_140948.pdf (23.9 KB)

PS how do I open that pdf file from my script rather than having to open it using e.g. File Explorer?
PPS Given I set papertype, how do I extract the values for papersize and use that to set the size of the displayed figure (assuming I set it visible of course)

I think you need to move the call to hold on, at line 36, after the first plot at line 37. This will let the plot function format the axes with box on among others.

open (filename)

In principle you would do that

units = get (gcf, "units");
set (gcf, "units", get (gcf, "paperunits"), "position", [10 10 get(gcf, "papersize")], "units", units);

but this won’t work unless your screen is large enough to hold the entire page: the figure size will be bounded by the screen size.

While the screen IS large enough to hold the entire page I was thinking to extract the x/y values for paper size and set the figure size to (say) half that to get a reasonable view of the graphs. I’m a little confused by the rather complex set command at the end there - looks almost reminiscent of APL!

To do that, should it read:

units = get (gcf, "units");
set (gcf, "units", get (gcf, "paperunits"), "position", [10 10 get(gcf, "papersize")/2], "units", units);

Or would that be too easy :slight_smile:


Well that actually did work to size the figure window, but it didn’t scale the plot vertically to fit the window until I manually resized the window by a pixel or two.

Is there another command I need to issue to avoid that?

Aha! I worked it out: Create the figure as not visible, create plots, set size and then set visible!!

The papersize is in interpreted in paperunits (inches by default) while the figure position is interpreted in units (pixels by default). So the command first changes units to match paperunits, then pass the paperposition as the width and height elements for the position, and finally restores the original units. The save/restore step are not necessary, just cleaner.

Sorry for the delay. First of all, I noticed that ‘help print’ in the command window lists more options than the function reference at Function Reference: print. I’m not sure why. Anyway, there is an option ‘fillpage’ which might do what you want. Although I don’t know if that already helps with the cropping of the axes.