如何用日期标记X轴? [英] How to label x-axis with dates?

查看:110
本文介绍了如何用日期标记X轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法弄清楚如何在Matlab中绘制非数字顺序的日期.我需要的日期是从1月22日到2月1日,但是当我将其放在Matlab中时,它会从1月1日开始按数字顺序排列.所以我想知道如何获取它,以便它按我的列表顺序排列.

I cant seem to figure out how to plot dates in Matlab that aren't in numerical order. The dates I need are from January 22nd to Feburary 1st, but when I put it in Matlab it goes in numerical order starting from the 1st. So I was wondering how to get it so that it goes in order of the list I have.

这是我编写的代码:

date = [22 23 24 25 26 27 28 29 30 31 1 2 3 4];
in_bed = [3 8 26 76 225 298 258 233 189 128 68 29 14 4];
convalescent = [0 0 0 0 9 17 105 162 176 166 150 85 47 20];
plot(date,in_bed,'*',date,convalescent,'*')
xlabel('Janurary -- Feburary')
ylabel('Number of Cases')
legend('Confined to bed','Convalescent')
title('Influenza Outbreak')

推荐答案

您是否考虑过使用实际的

Have you thought about working with the actual date format? This way your time arguments stay in chronological order.

先做一些准备工作

days = [22 23 24 25 26 27 28 29 30 31 1 2 3 4];
months = [1 1 1 1 1 1 1 1 1 1 2 2 2 2];
year = repmat(2015,numel(days),1);

in_bed = [3 8 26 76 225 298 258 233 189 128 68 29 14 4];
convalescent = [0 0 0 0 9 17 105 162 176 166 150 85 47 20];

创建 datevec (日期向量)

dateVector = [year(:) months(:) days(:)];

并使用 datenum :

date = datenum(dateVector); 

绘制并修复x刻度

plot(date,in_bed,'*',date,convalescent,'*')
set(gca,'XTick',date)  %// if you leave this step, labeling is dynamic!

使用 datetick :

datetick('x','dd-mm','keepticks')

和voilà:

这篇关于如何用日期标记X轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆