Filter löschen
Filter löschen

Why does exportgraphics not fully work, when using yyaxis?

5 Ansichten (letzte 30 Tage)
Sim
Sim am 1 Nov. 2023
Bearbeitet: Sim am 1 Nov. 2023
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 !

Antworten (1)

Voss
Voss am 1 Nov. 2023
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 Kommentar
Sim
Sim am 1 Nov. 2023
Bearbeitet: Sim am 1 Nov. 2023
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 :-)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by