How to constrain the plot within the limits?

4 Ansichten (letzte 30 Tage)
Andi
Andi am 16 Feb. 2023
Kommentiert: Askic V am 16 Feb. 2023
Hi everyone,
I am facing an issue while saving the figure in higher resolution. Figure get out of range plus axis labels also getting bigger.
May someone help me how can i fix this.
Here is my code:
D=figure (4)
D.Position(3:4)=[550,400];
subplot(311);
yyaxis left
plot(t,sf2(:, 2),'-b');
ylabel('Vent Fluid (\circC)')
%title('Vent Fluid Temp');
%grid on
yyaxis right
plot(t,sf1(:,2),'r-');
ylabel('Tidal amplitude (m)')
%legend('Vent','Tidal');
% title('Tidal Height');
grid on
text(4.8,2257.5,'(a)')
%xlabel('Time (days)')
%legend('Tidal','Vent');
subplot(312);
yyaxis left
yy1 = smooth(t,xxx,0.1,'loess');
yy2 = smooth(t,yyy,0.1,'loess');
plot(t,yy1,'-r', 'LineWidth',1)
ylabel('Normalized (\circC)')
yyaxis right
plot(t,yy2,'-b', 'LineWidth',1);
ylabel('Normalized (m)')
xlim([0.5 4.5]);
text(4.3,0.8,'(b)')
%xlabel('Time (days)');
%legend('Vent','Tidal');
grid on
subplot(313);
%set(gcf,'position',[x_f,y_f,width,height])
yyaxis left
plot(t,xxx,'-r', 'LineWidth',1);
ylabel('Normalized (\circC)')
yyaxis right
grid on
plot(t,y3,'-b', 'LineWidth',1);
ylabel('Normalized (m)')
xlim([0.5 4.5]);
xlabel('Time (days)');
text(4.3,0.8,'(c)')
exportgraphics(D,'barchart.png','Resolution',300)
Here is what i get
and here is what i am looking for

Akzeptierte Antwort

Askic V
Askic V am 16 Feb. 2023
Is this helpflul to you?
t =linspace(0, 5);
subplot(311);
y1 = sin(2*pi*t);
y2 = sin(2*pi*t)+0.2*randn(size(t));
myplot(t, y1, y2,[0.5, 4.5], [-1.5, 1.5]);
subplot(312);
y1 = sin(3*pi*t);
y2 = sin(3*pi*t)+0.2*randn(size(t));
myplot(t, y1, y2,[0.5, 4.5], [-1.5, 1.5]);
subplot(313)
y1 = sin(3*pi*t);
y2 = sin(3*pi*t)+0.2*randn(size(t));
myplot(t, y1, y2,[0.5, 4.5], [-1.5, 1.5]);
function myplot(t, y1, y2, x_lim, y_lim)
yyaxis left
plot(t,y1,'-b');
xlim([x_lim(1), x_lim(2)]); ylim([y_lim(1), y_lim(2)]);
set(gca,'fontsize',8) % set up font size
ylabel('Vent Fluid (\circC)')
%title('Vent Fluid Temp');
grid on
yyaxis right
plot(t,y2,'r-');
xlim([x_lim(1), x_lim(2)]); ylim([y_lim(1), y_lim(2)]);
ylabel('Tidal amplitude (m)')
end
  2 Kommentare
Andi
Andi am 16 Feb. 2023
Bearbeitet: Andi am 16 Feb. 2023
subplot(311);
y1 = sf2(:, 2);
y2 = sf1(:, 2);
myplot(t,y1, y2,[0, 5], [-1, 1]);
grid on
subplot(312);
y1 = xxx
y2 = yyy
myplot(t, y1, y2,[0.5, 4.5], [-1, 1]);
subplot(313)
y1 = xxx;
y2 = y3;
exportgraphics(gcf,'aaa.png','Resolution',300)
myplot(t, y1, y2,[0.5, 4.5], [-1, 1]);
function myplot(t, y1, y2, x_lim, y_lim)
yyaxis left
plot(t,y1,'-b');
xlim([x_lim(1), x_lim(2)]); ylim([y_lim(1), y_lim(2)]);
ylabel('Vent Fluid (\circC)', 'FontSize',8)
%title('Vent Fluid Temp');
grid on
yyaxis right
plot(t,y2,'r-');
xlim([x_lim(1), x_lim(2)]); ylim([y_lim(1), y_lim(2)]);
ylabel('Tidal (m)', 'FontSize',8)
xlabel('Time (days)', 'FontSize',8)
end
Askic V
Askic V am 16 Feb. 2023
Just to be sure, please execute the following lines before actually executing the code in the script:
clear % clear workspace
clc % clear command windows
close all % close all open figures

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Askic V
Askic V am 16 Feb. 2023
Bearbeitet: Askic V am 16 Feb. 2023
The complete script with modifed myplot function is the following:
clear
clc
close all
t =linspace(0, 5);
subplot(311);
y1 = sin(2*pi*t);
y2 = sin(2*pi*t)+0.2*randn(size(t));
% set up desired limits for x and y
myplot(t, y1, y2,[0.5, 4.5], [-1.5, 1.5],...
'subplot1',{'Vent Fluid (\circC)','Tidal amplitude (m)'},7);
subplot(312);
y1 = sin(3*pi*t);
y2 = sin(3*pi*t)+0.2*randn(size(t));
% set up desired limits for x and y
myplot(t, y1, y2,[0.5, 4.5], [-1.5, 1.5],...
'subplot2',{'Vent Fluid (\circC)','Tidal amplitude (m)'},7);
subplot(313)
y1 = sin(4*pi*t);
y2 = sin(4*pi*t)+0.2*randn(size(t));
% set up desired limits for x and y
myplot(t, y1, y2,[0.5, 4.5], [-1.5, 1.5],...
'subplot3',{'Vent Fluid (\circC)','Tidal amplitude (m)'},7);
exportgraphics(gcf,'aaa.png','Resolution',300)
function myplot(t, y1, y2, x_lim, y_lim, x_label, y_label, font_size)
yyaxis left
plot(t,y1,'-b');
xlim([x_lim(1), x_lim(2)]); ylim([y_lim(1), y_lim(2)]);
set(gca,'fontsize',font_size) % set up font size
ylabel(y_label{1})
grid on
yyaxis right
plot(t,y2,'r-');
xlim([x_lim(1), x_lim(2)]); ylim([y_lim(1), y_lim(2)]);
ylabel(y_label{2})
xlabel(x_label);
end
This script will produce aaa.png file that is attached.
  4 Kommentare
Askic V
Askic V am 16 Feb. 2023
This is really strange, because, the script I posted is executed online by clicking on the green play button. So the latest version of the Matlab (2022b) is executing the code. You do get correct figure, but only saved .png file is not correct?
Askic V
Askic V am 16 Feb. 2023
Regarding your question about different limits for left and right axis, please have a look at the following variant:
%%
clear
clc
close all
t =linspace(0, 5);
ax1 = subplot(311);
y1 = sin(2*pi*t);
y2 = sin(2*pi*t)+0.2*randn(size(t));
myplot(t, y1, y2,...
'subplot1',{'Vent Fluid (\circC)','Tidal amplitude (m)'},7);
% set up desired limits for x and y
xlim([0.5, 4.5]);
y_lim_left = [-1.5, 1.5];
y_lim_right = [-2, 2];
ax1.YAxis(1).Limits = y_lim_left;
ax1.YAxis(2).Limits = y_lim_right;
ax2 = subplot(312);
y1 = sin(3*pi*t);
y2 = sin(3*pi*t)+0.2*randn(size(t));
myplot(t, y1, y2,...
'subplot2',{'Vent Fluid (\circC)','Tidal amplitude (m)'},7);
% set up desired limits for x and y
xlim([0.5, 4.5]);
y_lim_left = [-1.5, 1.5];
y_lim_right = [-2, 2];
ax2.YAxis(1).Limits = y_lim_left;
ax2.YAxis(2).Limits = y_lim_right;
ax3 = subplot(313)
ax3 =
Axes with properties: XLim: [0 1] YLim: [0 1] XScale: 'linear' YScale: 'linear' GridLineStyle: '-' Position: [0.1300 0.1100 0.7750 0.2157] Units: 'normalized' Show all properties
y1 = sin(4*pi*t);
y2 = sin(4*pi*t)+0.2*randn(size(t));
myplot(t, y1, y2,...
'subplot3',{'Vent Fluid (\circC)','Tidal amplitude (m)'},7);
% set up desired limits for x and y
xlim([0.5, 4.5]);
y_lim_left = [-1.5, 1.5];
y_lim_right = [-2, 2];
ax3.YAxis(1).Limits = y_lim_left;
ax3.YAxis(2).Limits = y_lim_right;
exportgraphics(gcf,'aaa.png','Resolution',300)
function myplot(t, y1, y2, x_label, y_label, font_size)
yyaxis left
plot(t,y1,'-b');
set(gca,'fontsize',font_size) % set up font size
ylabel(y_label{1})
grid on
yyaxis right
plot(t,y2,'r-');
ylabel(y_label{2})
xlabel(x_label);
end

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by