python matlabplot 如何使得上下无间距

阅读: 评论:0

python matlabplot 如何使得上下无间距

python matlabplot 如何使得上下无间距

1.基础绘图

和python类似

1.1 绘图:plot()

如果绘制多条曲线,需要使用hold on-hold off ,不然后面的曲线会覆盖掉前面的。

hold on

plot(cos(0:pi/20:2*pi),'o-k');

plot(sin(0:pi/20:2*pi),'X--b');

hold off%两个一起画;不使用hold on的话,默认会覆盖掉前面那张图片

1.2 样式

plot样式

plot(x,y,‘str’)

图例:legend()

title()和xlabel()ylabel()zlabel()

x=0:0.5:4*pi;

y=sin(x);

h=cos(x);

w=1./(1+exp(-x));

xlabel('t=0 tp 2pi');

ylabel('values of sin(t) and e^{-x}');

plot(x,y,'bd-',x,h,'gp:',x,w,'ro-',x,w,'c^-');%样式

title('values of sin(t) and e^{-x}');%标题

legend('sin(x)','cos(x)','sigmoid','Gauss function');%图例

text() and annotation()

数学公式使用latex

x = linspace(0,3);

y = x.^2.*sin(x);

plot(x,y);

line([2,2],[0,2^2*sin(2)]);

str = '$$ int_{0}^{2} x^2sin(x) dx $$';%表示积分函数

text(0.25,2.5,str,'Interpreter','latex'); %积分函数的位置

annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]); %箭头位置

小练习:

t=1:0.01:2;

f=t.*t;

g=sin(2*pi*t);

plot(t,f,'k',t,g,'ro');

xlabel('Time (ms)');

ylabel('f(t)');

title('Mini Assignment #1');

legend('t^2','sin(2pi t)','location','northwest');%northwest指定图例在左上角

2. 图像调整(Figure Adjustment

)

一张图片由很多对象组成;

一个对象又有很多属性。

2.1 修改图片对象的句柄(handle)

函数

|函数|解释 |

|–|--|

|gca|返回当前图像的坐标轴axes |

|gcf|返回图像的figure对象|

|allchild|找到具体对象的所有孩子|

|ancestor|找到图片对象的祖先|

|delete|删除具体对象|

|findall|找出所有的图片对象|

2.2获得或者修改图片属性

获得属性/对象:get()

修改属性:set()

两者都需要通过获取handle来进行实现。

%2020.09.19

x = linspace(0, 2*pi, 1000);

y = sin(x);

h = plot(x,y);

get(h) %获取h的信息

get(gca)%获取当前图像的axis对象的所有信息

%set(gca, 'XLim', [0, 2*pi]);%修改Xlim的取值

%set(gca, 'YLim', [-1.2, 1.2]);

xlim([0, 2*pi]);%修改Xlim的取值

ylim([-1.2, 1.2]);

2.3 设置字体和坐标轴的间距(tick)

set(gca, 'FontSize', 25);%设置字体大小

set(gca, 'XTick', 0:pi/2:2*pi);%Tick, YTick, ZTick - 刻度值; 由递增值组成的向量

set(gca, 'XTickLabel', 0:90:360);

2.4 曲线的格式

线宽和曲线样式

%set(h, 'LineStyle', '-.',...

% 'LineWidth', 7.0, 'Color', 'g');%设置line的格式

plot(x,y, '-.g',... 'LineWidth', 7.0)

%delete(h);%删掉曲线h

图标(maker)的face和edge

x=rand(20,1); %rand(3,4) 返回一个 3×4 的矩阵。

set(gca, 'FontSize', 18);

plot(x,'-md','LineWidth', 2, 'MarkerEdgeColor', 'k',...

'MarkerFaceColor', 'g', 'MarkerSize', 10);

xlim([1, 20]);

2.5 小练习

t=1:0.01:2;

f=t.*t;

g=sin(2*pi*t);

plot(t,f,'k',t,g,'ro','LineWidth', 2,'MarkerEdgeColor', 'r',...

'MarkerFaceColor', 'g');

xlabel('Time (ms)');

ylabel('f(t)');

title('Mini Assignment #1');

legend('t^2','sin(2pi t)','location','northwest');

set(gca,'FontSize',20,'FontWeight','bold','TitleFontWeight','bold','XTick', -1:1:4);

3. 组合图片

当存在复合图像时,gcf指的是现在的图片,即figure2.

x = -10:0.1:10;

y1 = x.^2 - 8;

y2 = exp(x);

figure, plot(x,y1);

figure, plot(x,y2);

3.1 设置图像的位置和大小

3.2 一个figure中显示几个plots

t = 0:0.1:2*pi;

x = 3*cos(t);

y = sin(t);

subplot(2, 2, 1);

plot(x, y);

axis normal %x和y轴自动调整

subplot(2, 2, 2);

plot(x, y);

axis square %x和y轴的总长度一样

subplot(2, 2, 3);

plot(x, y);

axis equal %x和y轴的间隔一样

subplot(2, 2, 4);

plot(x, y);

axis equal tight %Set the axis limits to the range of the data

axis off

axis on

4. 把figures保存成文件

如果想要保存成高解析度的文件,建议使用‘print’。

%Saving Figures into Files

saveas(gcf,'practice','pdf');

原文链接:

本文发布于:2024-01-31 19:55:41,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170670214330980.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:间距   python   matlabplot
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23