网站首页 > 知识剖析 正文
MATLAB的x轴和y轴的位置和方向默认是位于图形的底部和左则,并且标注的刻度是按照数值递增的。如果需要修改x和y轴的位置以及方向,可以通过命令修改。今天主要是以实例讲解MATLAB修改坐标轴的位置和方向。
1.修改x轴的位置和方向
程序
clc;%清除命令行窗口命令x
clear all;%清除工作区变量
close all;%关闭所有图形窗口
%产生画图数据
x =linspace(-pi,pi,20);
y1 = x;
y2 = cos(x).*x;
y3 = sin(x).*x;
%绘制图像
figure;
plot(x,y1,'b-s','markerface','b');
hold on;
plot(x,y2,'r-^','markerface','r');
plot(x,y3,'k-^','markerface','k');
hold off;
xlabel('x');
grid on;
ylabel('y');
legend('y = x','y = cos(x)*x','y = sin(x)*x^{2}');
ylim([-5 5]);
figure;
subplot(3,1,1);
plot(x,y1,'b-s','markerface','b');
hold on;
plot(x,y2,'r-^','markerface','r');
plot(x,y3,'k-^','markerface','k');
hold off;
xlabel('x');
grid on;
ylabel('y');
ylim([-5 5]);
set(gca,'XAxisLocation','bottom'); %将x轴的位置设置在底部(默认)。
subplot(3,1,2);
plot(x,y1,'b-s','markerface','b');
hold on;
plot(x,y2,'r-^','markerface','r');
plot(x,y3,'k-^','markerface','k');
hold off;
xlabel('x');
grid on;
ylabel('y');
set(gca,'XDir','normal'); %将x轴方向设置为普通(从左到右递增)。
ylim([-5 5]);
set(gca,'XAxisLocation','top'); %将x轴的位置设置在顶部。
subplot(3,1,3);
plot(x,y1,'b-s','markerface','b');
hold on;
plot(x,y2,'r-^','markerface','r');
plot(x,y3,'k-^','markerface','k');
hold off;
xlabel('x');
grid on;
ylabel('y');
ylim([-5 5]);
set(gca,'XAxisLocation','origin'); %将x轴的位置设置在y=0处。
set(gca,'XDir','reverse'); %将x轴方向设置为反向(从右到左递增)。
运行结果
默认的坐标位置
修改x坐标的位置和方向
2.修改y轴的位置和方向
程序
clc;%清除命令行窗口命令x
clear all;%清除工作区变量
close all;%关闭所有图形窗口
%产生画图数据
x =linspace(-pi,pi,20);
y1 = x;
y2 = cos(x).*x;
y3 = sin(x).*x;
%绘制图像
figure;
plot(x,y1,'b-s','markerface','b');
hold on;
plot(x,y2,'r-^','markerface','r');
plot(x,y3,'k-^','markerface','k');
hold off;
xlabel('x');
grid on;
ylabel('y');
legend('y = x','y = cos(x)*x','y = sin(x)*x^{2}');
ylim([-5 5]);
figure;
subplot(3,1,1);
plot(x,y1,'b-s','markerface','b');
hold on;
plot(x,y2,'r-^','markerface','r');
plot(x,y3,'k-^','markerface','k');
hold off;
xlabel('x');
grid on;
ylabel('y');
ylim([-5 5]);
set(gca,'YAxisLocation','left'); %将y轴的位置设置在左边(默认)。
subplot(3,1,2);
plot(x,y1,'b-s','markerface','b');
hold on;
plot(x,y2,'r-^','markerface','r');
plot(x,y3,'k-^','markerface','k');
hold off;
xlabel('x');
grid on;
ylabel('y');
set(gca,'YDir','normal'); %将y轴方向设置为普通(从下到上递增)。
ylim([-5 5]);
set(gca,'YAxisLocation','right');%将y轴的位置设置在右边。
subplot(3,1,3);
plot(x,y1,'b-s','markerface','b');
hold on;
plot(x,y2,'r-^','markerface','r');
plot(x,y3,'k-^','markerface','k');
hold off;
xlabel('x');
grid on;
ylabel('y');
ylim([-5 5]);
set(gca,'YAxisLocation','origin'); %将y轴的位置设置在x=0处。
set(gca,'YDir','reverse'); %将y轴方向设置为反向(从上到下递增)。
默认的坐标位置
修改y坐标的位置和方向
3.同时修改x轴和y轴的位置置于原点处
程序
clc;%清除命令行窗口命令x
clear all;%清除工作区变量
close all;%关闭所有图形窗口
%产生画图数据
x =linspace(-pi,pi,20);
y1 = x;
y2 = cos(x).*x;
y3 = sin(x).*x;
%绘制图像
figure;
plot(x,y1,'b-s','markerface','b');
hold on;
plot(x,y2,'r-^','markerface','r');
plot(x,y3,'k-^','markerface','k');
hold off;
xlabel('x');
grid on;
ylabel('y');
legend('y = x','y = cos(x)*x','y = sin(x)*x^{2}');
ylim([-5 5]);
set(gca,'XAxisLocation','origin','YAxisLocation','origin');%将x轴和y轴的位置设置在原点处。
运行结果
本文内容来源于网络,仅供参考学习,如内容、图片有任何版权问题,请联系处理,24小时内删除。
作 者 | 郭志龙
编 辑 | 郭志龙
校 对 | 郭志龙
- 上一篇: 第38节 location、history对象
- 下一篇: SQL优化-数据库自优化
猜你喜欢
- 2024-11-24 浏览器跨域问题以及常用解决方案
- 2024-11-24 14个前端小知识,我猜你每天都会遇到
- 2024-11-24 跨域问题的4种解决方案
- 2024-11-24 什么是跨域?跨域解决方法
- 2024-11-24 构建rancher自定义ui的前端镜像
- 2024-11-24 你应该知道的前端小知识,初学者,确定不点进来看看吗?
- 2024-11-24 15个前端小知识
- 2024-11-24 记一场纯JS赛——DiceCTF2021 Web题解
- 2024-11-24 如何基于 Elasticsearch 实现排序沉底或前置
- 2024-11-24 如何查找网站源IP地址
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)