最近看到有人问下面的图应该怎么画。最简单的方法是使用Origin,Origin也可以很容易实现上述的功能,绘图教程:http://www.cappchem.com/Article2014/1108.html。
但是,目前笔者在Matlab中尚未找到现成的相关的绘图方法供我们使用,如果看到这里的某位看官知道Matlab简单的方法,希望不吝赐教,在评论区留言给笔者。这里只是介绍一种通过Matlab代码的方式如何实现。 首先,我们以一个简单的实例开始,假设我们需要画的是下面的一个三维图,和一般的三维散点图不同的是YOZ平面的散点使用折线连接在了一起。
data=textread('F:/时段.txt');
data=data(:,1:end-1);
car_num = size(data,1);
day_num = size(data,2);
hour_num = 3;
for i=1:car_num%车序号
%每辆车一条曲线
count = 1;
u = [];
for j=1:day_num%天序号
u(count,1) = i;
u(count,2) = j;
u(count,3) = data(i,j);
count = count + 1;
end
%绘制每辆车的曲线
x=u(:,1);%车
y=u(:,2);%天
z=u(:,3);%时段
plot3(x,y,z,'bo',x,y,z);
grid on;
hold on;
axis([0, car_num+1,0,day_num,0,hour_num]);% 坐标轴的显示范围
set(gca, 'XTick', 0:car_num+1); % 车
set(gca, 'YTick', 0:day_num); % 天
set(gca, 'ZTick', 0:hour_num); % 时段
end
xlabel('车');
ylabel('天');
zlabel('时段');
title('折线图');
332000013130000313000010000222
101200001310000221000011100003
133100000100001213000031200002
200001233000032300002320000212
031300000320000021200000331300
003232000013320000303000021320
222000021220000021220000210200
100001112000032210000313300001
200003313000022000003320000212
000201300000311300001210000313
300300003123000022010000213000
322300002220000111000033200001
000022320000133000012200003023
302000023320000123000022110000
000033310000210000031100001023
302000032210000122100002333000
001200002320000033000001000033
103100002110000112000012210000
001120000320100003113000000331
120000133200003231000003231000
212300001210000031100003112000
001110000120300001033000033330
000021110000333000001332000023
001213000012320000102200001121
000023330000113200001233000031
000000020000000000000020000000
300000000000000000000000010010
010100000000000000012000000000
000003012000013000003000000301
001000000003000000100002000022
200300000300100000200000200000
010000030000000010000130300001
003200002000000300001030000320
000101000000202000000001000002
000010020000330000000000001000
000300001001000001202000030030
311000030000200000000000010000
000000020100002000000000100200
000220000331000012310000021300
020130000000120000213000000003
001000010000000011030000023020
000000021000000000021000000101
300001032000000023000031010000
003001000000030010000122300000
003120000103000001000020310000
200200000000222000000011020000
010000011300000303000002320000
000003030000000010200000230200
303000010000330000110000110100
000100000000200002030000200002
000100002000002032000000223000
000211000002000020000031210000
001212000000113300003000010020
000300001300000011000020000103
000100000300200003000012020000
其中,数据共55行,每一行数据代表一辆车的信息,该行中的第i列代表该车的第i天的状态,例如第一行,第一列中的3代表,第一辆车在第一天的第三个时段出行。
有了上面的基础,是不是就可以绘制类似于下面的图了?其实和下图相比,上图只要YOZ平面上有足够多的的点,同时,X值的个数再少一点,就可以达到下述的效果。
如果你还想添加一个网格,那么可以这样,以下内容参考自Matlab官网提供的示例。
[Y,Z] = meshgrid(1:0.5:10,1:20);
X = zeros(size(X));
surf(X,Y,Z)