Matlab中如何改变xlabel默认位置,方法是什么
Admin 2022-06-29 群英技术资讯 1272 次浏览
这篇文章给大家分享的是Matlab中如何改变xlabel默认位置,方法是什么。小编觉得挺实用的,因此分享给大家做个参考,文中的介绍得很详细,而要易于理解和学习,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。xlabel(‘time',‘FontSize',12);
如果没有设置位置,默认是在中间
xlabel(‘time',‘position',[900,1870],‘FontSize',12);
此时‘time'在你设置的位置
pos=axis;%取得当前坐标轴的范围,即[xmin xmax ymin ymax]
xlabel(‘time',‘FontSize',12, ‘Position',[pos(2) pos(3)])
x=0:pi/50:2*pi;
y=sin(x);
plot(x,y);
pos=axis;%取得当前坐标轴的范围,即[xmin xmax ymin ymax]
xlabel('x轴','position',[pos(2) 1.15*pos(3)]);%设置x轴标签的文本在图的右下方,1.15这个值根据自己的需要可以调整
形成的图

补充:Matlab作图实例――xlabel,ylabel,title,text,plot,patch,datetime等的应用
所用数据如下:

代码如下:
clear
clc
format compact
format shortG
T = readtable('repayment_schedule.xlsx','ReadVariableNames',true)
T.time=datetime(datestr(T.time,'yyyy.mm.dd'),'InputFormat','yyyy.MM.dd',...
'format','yyyy.MM.dd')
p=plot(T.time,T.m_per_month,T.time,T.m_residue)
p(1).Marker='o'
p(2).Marker='*'
box off
%让y轴不用科学计数法显示
h=gca
y_val=h.YTick
y_str=string(y_val) %等价于y_str=num2str(y_val')
h.YTickLabel=y_str
%横轴日期显示设置
h.XTick=T.time
xtickangle(45) %让x轴的标签逆时针旋转45度
%画垂直虚线
hold on
p1=plot([datetime(2018,11,20) datetime(2018,11,20)],...
[0 30830],'Color',[0.6 0.6 0.6],'LineStyle','--')
p2=plot([datetime(2018,12,20) datetime(2018,12,20)],...
[0 26434],'Color',[0.6 0.6 0.6],'LineStyle','--')
p3=plot([datetime(2019,01,20) datetime(2019,01,20)],...
[0 22038],'Color',[0.6 0.6 0.6],'LineStyle','--')
p4=plot([datetime(2019,02,20) datetime(2019,02,20)],...
[0 17641],'Color',[0.6 0.6 0.6],'LineStyle','--')
p5=plot([datetime(2019,03,20) datetime(2019,03,20)],...
[0 13245],'Color',[0.6 0.6 0.6],'LineStyle','--')
p6=plot([datetime(2019,04,20) datetime(2019,04,20)],...
[0 8849],'Color',[0.6 0.6 0.6],'LineStyle','--')
p7=plot([datetime(2019,05,20) datetime(2019,05,20)],...
[0 4452.8],'Color',[0.6 0.6 0.6],'LineStyle','--')
hold off
%标注每个点
str1=string(T.m_per_month)
str2=string(T.m_residue)
text(T.time,T.m_per_month-1200,str1,'Color',[0 0.447 0.741],...
'HorizontalAlignment','center')
text(datetime(datenum(T.time)+2,'ConvertFrom','datenum'),...
T.m_residue+1100,str2,...
'Color',[0.85 0.325 0.098],...
'HorizontalAlignment','left')
%图例
legend([p(1) p(2)],{'每月还款金额','每月还款后剩余总本息'},...
'Location','northeast','NumColumns',1)
%各个标题
xlabel('还款时间')
ylabel('还款金额')
title({'GGG还款计划';'2018.12.20-2019.06.20'})
print('GGG还款计划','-dpdf')
%将数据再写入excel
% writetable(T,'test.xlsx','WriteVariableNames',true)
做出的图如下:

相应代码为:
%填充并画网格
clear
clc
v1 = [0 0; 4 0; 4 4;0 4];
f1 = [1 2 3 4];
figure
patch('Faces',f1,'Vertices',v1,...
'EdgeColor',[0.75 0.75 0.75],'FaceColor',[0.75 0.75 0.75]);
g=gca
g.XTick=[0:4]
g.YTick=[0:4]
g.XLim=[0 4.5]
g.YLim=[0 4.5]
grid on
g.Layer = 'top';
g.GridColor=[1 1 1]
g.GridLineStyle='--'
g.GridAlpha = 1
axis square
%挖洞
v2 = [1 1;2 1;2 2;1 2];
f2 = [1 2 3 4];
patch('Faces',f2,'Vertices',v2,...
'EdgeColor',[0.75 0.75 0.75],'FaceColor',[1 1 1]);
%画函数图
hold on
f1=@(t) 4*t-4
f2=@(t) 0.25*t+1
f1p=fplot(f1,[1 2],'k','LineWidth',1,'DisplayName','X的策略')
f2p=fplot(f2,[0 4],'--k','LineWidth',1,'DisplayName','Y的策略')
xlabel('X的策略')
ylabel('Y的策略')
legend([f1p f2p],{},'NumColumns',2,'FontSize',10)
%导出为PDF
% saveas(gcf,'qiyan.pdf')
print('qiyan','-dpdf')
做出的图如下

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要为大家介绍了python 赋值语句和基本输入输出,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
这篇文章给大家分享的是Python matplotlib绘图的一些使用技巧,对大家高效的、美观的显示图表操作有一定的帮助,有需要的朋友可以参考了解看看,那么接下来就跟随小编学习一下吧。
今天给大家分享的实例是,用python画城市地图且轮播展示的效果,本文有以5个省份的城市为例进行演示,实现代码如下,对大家学习Python有一定的帮助,有需要的朋友不妨了解看看。
python集合怎样理解?集合是python中很基础的一个内容,这篇文章主要给大家分享的是集合的基本信息和集合的基本操作,有这方面学习需要的朋友可以参考。
本文给大家分享的是python中排序的方法,简单介绍了冒泡排序、选择排序、插入排序、快速排序、希尔排序、归并排序。有这方面学习需要的朋友可以参考参考。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008