필터 지우기
필터 지우기

Why does exportgraphics not fully work, when using yyaxis?

조회 수: 3 (최근 30일)
Sim
Sim 2023년 11월 1일
편집: Sim 2023년 11월 1일
Why does exportgraphics not fully work, when using yyaxis?
This is my minimum working example:
clear all;clc;close all;
% create a figure composed of subplots
fig = figure('position',[100 100 600 1500],'Color',[1 1 1]);
for i = [1 3 5 7 9 11]
ax1 = subplot(6,2,i);
ax2 = subplot(6,2,i+1);
myfunction_new([ax1,ax2]);
end
% print PDF
path = ('/.../figure/');
extension = 'pdf';
baseFileName = sprintf('%s.%s','my_figure',extension);
fullFileName = fullfile(path, baseFileName);
exportgraphics(fig,fullFileName,'ContentType','vector');
% function to create my graphs (within the subplots)
function myfunction_new(ax)
if ~nargin || numel(ax) < 2
figure()
ax = gca();
figure()
ax(2) = gca();
end
% plots on the left side (i.e. on left side subplots)
hold(ax(1),'on');
yyaxis(ax(1),'right')
plot(ax(1),1:10,rand(10,1));
plot(ax(1),1:10,rand(10,1));
ax(1).YAxis(1).Color = 'k';
ax(1).YAxis(2).Color = 'b';
hold(ax(1),'off');
% plots on the right side (i.e. on right side subplots)
number_groups = length(unique(rand(10,1)));
color = turbo(number_groups);
markers = repelem('o',number_groups);
sz = repelem(12,number_groups);
g = gscatter(ax(2),rand(10,1),rand(10,1),rand(10,1),color,markers,sz);
for i = 1 : number_groups
g(i).MarkerFaceColor=g(i).Color;
end
axis(ax(2),'off')
legend('off')
end
Result on monitor:
Result on PDF:
Instead, if I comment (or remove) the following commands, the PDF is generated correctly, including the plots on the right side as well:
% yyaxis(ax(1),'right')
plot(ax(1),1:10,rand(10,1));
plot(ax(1),1:10,rand(10,1));
% ax(1).YAxis(1).Color = 'k';
% ax(1).YAxis(2).Color = 'b';
However, I would need yyaxis !
  댓글 수: 1
Sim
Sim 2023년 11월 1일
It might be a bug of exportgraphics, or an issue with the axes. I do not know..

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

답변 (1개)

Voss
Voss 2023년 11월 1일
Why does exportgraphics not fully work, when using yyaxis?
I don't know why, but plotyy (not recommended) seems to give the correct result in the pdf.
clear all;clc;close all;
% create a figure composed of subplots
fig = figure('position',[100 100 600 1200],'Color',[1 1 1]);
for i = [1 3 5 7 9 11]
ax1 = subplot(6,2,i);
ax2 = subplot(6,2,i+1);
myfunction_new([ax1,ax2]);
end
% print PDF
file_path = ('/.../figure/');
extension = 'pdf';
baseFileName = sprintf('%s.%s','my_figure',extension);
fullFileName = fullfile(file_path, baseFileName);
exportgraphics(fig,fullFileName,'ContentType','vector');
% function to create my graphs (within the subplots)
function myfunction_new(ax)
if ~nargin || numel(ax) < 2
figure()
ax = gca();
figure()
ax(2) = gca();
end
% plots on the left side (i.e. on left side subplots)
hold(ax(1),'on');
% yyaxis(ax(1),'right')
% plot(ax(1),1:10,rand(10,1));
% plot(ax(1),1:10,rand(10,1));
% ax(1).YAxis(1).Color = 'k';
% ax(1).YAxis(2).Color = 'b';
new_ax = plotyy(ax(1),1:10,rand(10,1),1:10,rand(10,1)); %#ok<PLOTYY>
new_ax(1).YAxis.Color = 'k';
new_ax(2).YAxis.Color = 'b';
hold(ax(1),'off');
% plots on the right side (i.e. on right side subplots)
number_groups = length(unique(rand(10,1)));
color = turbo(number_groups);
markers = repelem('o',number_groups);
sz = repelem(12,number_groups);
g = gscatter(ax(2),rand(10,1),rand(10,1),rand(10,1),color,markers,sz);
for i = 1 : number_groups
g(i).MarkerFaceColor=g(i).Color;
end
axis(ax(2),'off')
legend('off')
end
  댓글 수: 1
Sim
Sim 2023년 11월 1일
편집: Sim 2023년 11월 1일
thanks a lot! Yes, it works, even though I am having several troubles to convert all the pieces of codes, that were related to yyaxis, to plotyy.... Yes, I accept your nice answer, but if any workaround with yyaxis could be found, I would really appreciate it :-)

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

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by