필터 지우기
필터 지우기

Axis exponent missing when using exportgraphics to export figure in pdf vector format (or image)

조회 수: 109 (최근 30일)
Hi,
When I use exportgraphics to export a figure in pdf vector graphics format (or even image formats such as .png, .tiff), any axis exponents from the figure are missing in the output export file. For my application, I need to export the figure in vector format.
Example code to generate figure:
fig1 = figure(1);
plot(rand(10,1)*10^-5)
exportgraphics(fig1,'testMissingAxisExponent01.pdf','ContentType','vector');
Example figure exported by using 'Save As' in MATLAB figure GUI (which properly shows the axis exponent):
Example figure generated from exportgraphics (with the missing axis exponent):
I first discovered this issue when working with plots in nested tiles using tiledlayouts and tried increasing the padding with no success. However, this issue seems to be related to exportgraphics.
Any solutions or workarounds? Thank you!
  댓글 수: 2
Matt Latourette
Matt Latourette 2024년 6월 12일
I was using the export button from the axes toolbar instead of calling exportgraphics programmatically, but I encounter the same problem with the exponent for the y-axis being cut off when I try to save a plot. I'm using MATLAB R2024a.
After I found this question on the MATLAB Answers web page, I took a look at the documentation for the exportgraphics command. The "Padding" option seemed promising as a potential solution, but when I tried that I got an error message stating, "Illegal option 'padding' given." What I tried, specifically, was to click on the plot axes to select my plot. Then, in the command window:
ax = gca;
exportgraphics(ax, "figurepadding.png", "Padding", "figure")

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2024년 6월 3일
편집: Adam Danz 2024년 6월 14일
This issue has been fixed in MATLAB R2024a Update 4.
=================================================
Thanks for reporting this issue. Follow progress on this bug by following the bug report below
The recommended work-around is to call the print function. For example, to export the current figure to a PNG file named myplot.png, use this command:
print("myplot.png","-dpng")
To copy the current figure to the clipboard as pixels, use this command:
print("-clipboard","-dbitmap")
  댓글 수: 7
Raymond Yeung
Raymond Yeung 2024년 6월 19일
I just checked again and the R2024a Update 4 is now available. I was able to install the new update.
I can confirm that the issues with exportgraphics and copygraphics functions excluding secondary axes labels (including axes exponents) have been resolved.
Thanks to the community for your input and the staff for fixing the issue!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Pablo Nicolas
Pablo Nicolas 2024년 3월 27일
이동: Fangjun Jiang 2024년 3월 27일
I encountered the same problem and I was looking for an answer or possible solution but unsucessfully. What I did that finally solved the issue was to install an older Matlab version (Matlab 2023a). Hopefully, this was helphul for you!
  댓글 수: 2
Raymond Yeung
Raymond Yeung 2024년 3월 27일
이동: Fangjun Jiang 2024년 3월 27일
Thanks Pablo! I tried exporting my plots using "exportgraphics" again in older versions of MATLAB (R2023a, R2023b) which indeed does work. I put in a bug report for this issue in MATLAB R2024a.
Raymond Yeung
Raymond Yeung 2024년 3월 27일
From user discussions (Pablo Nicolas) and interactions with MathWorks Support, this is an issue that appeared with MATLAB R2024a. I have already put in a bug report.
Current workarounds include using an older version of MATLAB (tested MATLAB R2023a and R023b with success). Alternatively, you could use "saveas" instead of "exportgraphics". However, if you want to export in vector format, you would have to change the figure renderer to 'Painters' which will apparently be removed in future releases.
fig1.Renderer = 'painters';
saveas(fig1,'testPlotSaveAs.pdf')
If you still want to use "exportgraphics" in MATLAB R2024a, another workaround is to use the "text" function to superimpose the exponent onto the plot, but this workaround is not ideal since it is tricky to ensure you get the correct position on the plot.
fig1 = figure(1);
plot(rand(10,1)*10^-5);
ylimits = ylim;
exponent = floor(log10(ylimits(2)));
% Create a string that represents the exponent part
exponentStr = ['x10^{' num2str(floor(log10(ylimits(2)))) '}'];
% Add text to the plot to display the exponent
text(0, 1.03, exponentStr, 'Units', 'normalized', 'FontSize', 10);
% Manually adjust y-axis tick labels to not include the exponent part
% This step is primarily so that the non-exported figure does not have
% overlapping exponent text
yticks = get(gca, 'YTick');
newYTickLabels = arrayfun(@(y) num2str(y/10^exponent), yticks, 'UniformOutput', false);
set(gca, 'YTickLabel', newYTickLabels);
exportgraphics(fig1,'addTextToPlot.pdf','ContentType','vector');

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by