How to prevent export_fig plot background images to have a low resolution?

13 views (last 30 days)
Hello, I am trying to plot a figure in native resolution with "export_fig". I am a complete beginner, so some things may look odd.
I am using Matlab Online and added export_fig from the addon explorer.
My source image has a resolution of 3087x3999 pixels. With that image I cover an area of 3130 x 4054 cm. The size is off bit a little bit, but the scaling is equal.
In matlab I create a scatterplot with some data:
hold on
if size(messPositionenStrFarbe1,2) > 0
scatter(Table_Data.("Pos X")(messpunkteZeileTablePlotFarbe1)/100,Table_Data.("Pos Y")(messpunkteZeileTablePlotFarbe1)/100,50,farbeIstPos1,"x","LineWidth",0.7,"DisplayName",nameIstPos1);
% [...] draw some more lines to the same plot with scatter
The names for the legend are created with each scatter / drawing command.
After everything is drawn, I start formatting the plot:
% Plot_Titel und Plot_subtitel
p_title.FontSize = 14;
% Position Legend
legend_par = legend;
legend_par.FontSize = 8;
legend_par.Location = "northeast";
% Axis limits [x_min x_max y_min y_max]
I set axis("equal"); so that both have the same scale and 100 cm equal the same distance in both axis directions.
ax = gca; % gca get current axis
ax.XAxis.Limits = [0 x_max];
ax.YAxis.Limits = [0 y_max];
%add background image
backImage = imread("./Backgroundimage.png");
h = image(ax.XAxis.Limits,flip(ax.YAxis.Limits),backImage);
ax.Layer = "top";
I had to flip the Y-Axis for unknown reasons, else the image was upside down.
%Scale the axis and name them
ax.XLabel.FontSize = 11; % Standardwert 11
ax.XLabel.String = "Distance {\itX} in m";
ax.YLabel.FontSize = 11;
ax.YLabel.String = "Distance {\itY} in m";
% Add a subgrid to the plot
ax.XAxis.MinorTickValuesMode = "manual";
ax.XAxis.MinorTickValues = 0:1:x_max;
ax.XAxis.MinorTick = "on";
ax.YAxis.MinorTickValuesMode = "manual";
ax.YAxis.MinorTickValues = 0:1:y_max;
ax.YAxis.MinorTick = "on";
grid minor; % Only show Grid of Sub-Scale
Then I export the image with "export_fig":
if saveImage
export_fig(export_figname, '-png','-a1', '-native');
PNG image, a1 sets anti-aliasing to "none", which I hoped would improve image quality.
-native should output the image in full resolution.
During the printing I get the message:
Warning: print2array generating a 34.3M pixel image. This could be slow and might also cause memory problems.
But there is still ~5 GB of RAM left on my PC.
The exported image has a resolution of 3572 x 4817 pixels.
In the resulting image, the area of the original image still has a size of 3087x3999 pixels. But it is completely blurry. It looks like it was compressed down to the matlab native image output resolution and then scaled up again.
These two images show a section of the original image, cropped to show ~1500x500 pixels and the same area of the background image.
The orignal image (50% scale):
And this is what it looks like after it was exported and then cropped similarly, a bit larger to show the plot-scale. (1880x710 pixels)
Question 1:
When looking at the plot in Matlab itself, it looks as sharp as the original image. The problem arises when I export it.
Does anyone know what causes this and how I can fix it?
Question 2:
When exporting the image, it only shows the area of the plot that can be seen in the program with standard view of the plot. But when I zoom in on the plot, I notice that a few plotted points lie outside the area of the original image. So outside of the x_max=31.30; axis scale.
This is what it looks like in the matlab window, when zoomed into that area. The export_fig output image is cropped with the ending of the scale, so the points on the right are not shown.
Is there any way change the area that export_fig exports as an image?
  1 Comment
Daniel Neyers
Daniel Neyers on 16 Jan 2024
Regarding the second question, I ended up widening the image and calculated the new width for the resolution.

Sign in to comment.

Answers (1)

Yair Altman
Yair Altman on 16 Jan 2024
Edited: Yair Altman on 16 Jan 2024
Anti-aliasing is especially important for exported text characters, so by forcing export_fig to not anti-alias (using -a1) you are making the text output look jagged. Removing anti-aliasing is recommended only in rare cases, when image sharpness is more important than viewing quality. I suggest removing the -a1 option in your case.
Additionally, you might want to play around with various output resolutions (-m<value> or -r<value>) rather than -native.
For additional information on the various export_fig parameters, read the export_fig help section, and/or the README file.


Find more on Images in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!