How to constrain the plot within the limits?

2 ビュー (過去 30 日間)
Andi
Andi 2023 年 2 月 16 日
コメント済み: Askic V 2023 年 2 月 16 日
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

採用された回答

Askic V
Askic V 2023 年 2 月 16 日
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 件のコメント
Askic V
Askic V 2023 年 2 月 16 日
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

サインインしてコメントする。

その他の回答 (1 件)

Askic V
Askic V 2023 年 2 月 16 日
編集済み: Askic V 2023 年 2 月 16 日
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 件のコメント
Askic V
Askic V 2023 年 2 月 16 日
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

サインインしてコメントする。

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by