y-Label cut off by exportgraphics()

The description of the function exportgraphics says, that it produces a tightly cropped image. In my case it produces a tightly cropped PDF which is unfortunately a little bit too tight at the y-label. I tried rearranging the figure within the window, but exportgraphics does not change its output (which is the intended functionality). Unfortunately, in my case, this leads to a cut image.
The code:
set(groot,'defaultAxesFontName','PT Sans')
p = figure('Position', [455,503,879,400]);
ylabel('Label Example: i C', 'FontWeight', 'bold')
set(gca, 'FontSize', 14)
exportgraphics(p,'label_example.pdf','BackgroundColor','none','ContentType','vector');
produces a slightly cut off y-label as you can see in this picture:
The large C as well as the small i are cut off at the top. The font 'PT Sans' is only needed to produce the exact image, but the problem is also happening with other fonts. At the other borders of my figure exportgraphics adds a small white space, as I would expect. This only happens for the y-label.
If you need further information, please comment and I will try to give it to you as soon as possible. Thanks in advance for the help.
Best regards
Stefan

9 Comments

It adds a white box around the plot to force exportgraphics to keep the margins. However this does not explain the behaviour of exportgraphics, so I keep this question open.
i cannot reproduce your problem, my pdf is fine. I am running 2022a
try switching your Renderer
try plotting into a tiledlayout, that sometimes helped me to fix some gitches
Hi Jonas,
thanks for your reply. Unfortunately I don't have 2022a available, but I can reproduce the error using a different PC and also 2021b (on both machines). In the picture there is the example using Times New Roman as the font on the other PC. The C and i are still cut off.
This is the output for 'opengl info' on my laptop
>> opengl info
Version: '4.5.0 - Build 25.20.100.6472'
Vendor: 'Intel'
Renderer: 'Intel(R) UHD Graphics 620'
RendererDriverVersion: '25.20.100.6472'
RendererDriverReleaseDate: '10-Dec-2018'
MaxTextureSize: 16384
Visual: 'Visual 0x07, (RGBA 32 bits (8 8 8 8), Z depth 16 bits, Hardware acceleration, Double buffer, Antialias 8 samples)'
Software: 'false'
HardwareSupportLevel: 'full'
SupportsGraphicsSmoothing: 1
SupportsDepthPeelTransparency: 1
SupportsAlignVertexCenters: 1
Extensions: {225×1 cell}
MaxFrameBufferSize: 16384
And this is the one for the other PC
>> opengl info
Version: '1.1.0'
Vendor: 'Microsoft Corporation'
Renderer: 'GDI Generic'
MaxTextureSize: 1024
Visual: 'Visual 0x0e, (RGB 24 bits (8 8 8), Z depth 16 bits, Software, Single buffer, Antialias 0 samples)'
Software: 'true'
HardwareSupportLevel: 'none'
SupportsGraphicsSmoothing: 0
SupportsDepthPeelTransparency: 0
SupportsAlignVertexCenters: 0
Extensions: {3×1 cell}
MaxFrameBufferSize: 0
As far as I understand it you can not change the renderer itself, or is there anything I could or should change?
Thanks again for your help.
you can change your renderer using
set(gcf,'Renderer','opengl')
or
set(gcf,'Renderer','painters')
you can also try to update your graphics card driver
I tried both renderers and installed another graphics driver, but it did not change anything. However I tried a few more things and the font size seems to be the problem. With a font size of 14.1 the problem does not occur anymore. Did you also use a font size of 14 as in my example?
yes, I also used 14 as font size. I just copy pasted your code onto my machine. Did you try a tiledlayout for printing, sometimes this helped me:
set(groot,'defaultAxesFontName','PT Sans')
p = figure('Position', [455,503,879,400]);
tiledlayout(1,1); nexttile();
ylabel('Label Example: i C', 'FontWeight', 'bold')
set(gca, 'FontSize', 14)
exportgraphics(p,'label_example.pdf','BackgroundColor','none','ContentType','vector');
Hi Jonas,
the tiledlayout function did not change the output for me.
The .1 increase in font size however works and does not change my figures significantly. Thanks for your help and trying to reproduce my problem. I will keep the question open for a little bit and see if anyone is able to reproduce the output.
I ran into something like this with labels getting cut off, and I noticed the figure window would sort of blink or hesitate as functions like xlabel() and xlim() are applied. I think it was catching up on the plotting process, and exportgraphics() was getting called before it had settled to its final state. I added pause(2) between the last command that affected the figure and exportgraphics, and it's no longer cutting off my axis labels.
Ebgert
Ebgert on 6 Jul 2024
Moved: Voss on 6 Jul 2024
This is still a problem and absolutely should not stll require a convoluted workaround for such a basic functionality.

Sign in to comment.

 Accepted Answer

Aman
Aman on 14 Feb 2023
Hi,
I understand that you are trying to download the graphic content in a tightly cropped format, but in your case, it is cropping the “ylabel”. To rectify this, you can draw a suitable size rectangle around your content which will prevent cropping of your “ylabel” by the “exportgraphics” function. You can visit the below link for the reference.

1 Comment

Hi Aman,
thanks for the answer. I found that link myself (see my first comment). I fixed my figures using this workaround but wanted to keep the question open, to see, if there is any general setting or code mistake I made to produce the too tight cropping.
Do you have any idea, why exportgraphics does cut the ylabel in some cases, but not in others?

Sign in to comment.

More Answers (0)

Categories

Find more on Printing and Saving in Help Center and File Exchange

Products

Release

R2020b

Asked:

on 6 Feb 2023

Moved:

on 6 Jul 2024

Community Treasure Hunt

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

Start Hunting!