Why does exportgraphics not fully work, when using yyaxis?
4 views (last 30 days)
Show older comments
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';
Answers (1)
Voss
on 1 Nov 2023
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
See Also
Categories
Find more on Data Exploration 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!