matlab - 如何在 Matlab 中最好地设置日期轴
问题描述
您能帮我在 Matlab 中设置日期轴或指出正确的帖子吗?
我的问题如下:我有一些要绘制的数字格式的价格和日期,例如:
Prices = repmat([10; 5; 3; 4; 11; 12; 5; 2],10,1);
Dates = [726834:726834+8*10-1]';
如果我这样绘制它们:
plot(Dates,Prices)
dateaxis('x',17)
我得到了我不想要的 x 轴值,因为它们看起来不规则(我猜它们遵循某些规则但它们看起来不太好)。我怎样才能最好地将它们设置为,例如,总是每月的第一天,或者一月一号和七月一号,等等?我知道我可能可以使用 set(gca, 'xtick', ?? ??); 但我缺乏一些关于我如何做到这一点的概述,并且 Matlab 帮助对我没有帮助。
解决方案
此代码用每个月的第一天标记该图。要获取每年的一月或七月,只需选择月份数组的某些元素。该策略是使用 eomdate 获取每个月的最后一天并加 1。图 1 给出了每个月的第一天,图 2 给出了您在数组months_to_display 中选择的月份。
Prices = repmat([10; 5; 3; 4; 11; 12; 5; 2],10,1);
Dates = [726834:726834+8*10-1]';
firstDate = strsplit(datestr(Dates(1)-1, 'dd,mm,yyyy'),',');
lastDate = strsplit(datestr(Dates(end), 'dd,mm,yyyy'),',');
months = mod(str2double(firstDate{2}):str2double(lastDate{2})+12*(str2double(lastDate{3})-str2double(firstDate{3})),12);
months(months == 0) = 12;
years = zeros(1,length(months));
currYear = str2double(firstDate{3});
for i = 1:length(months)
years(i) = currYear;
if (months(i) == 12)
currYear = currYear + 1;
end
end
dayCount = eomdate(years,months);
firstDates = dayCount+1;
figure(1)
plot(Dates, Prices)
xticks(firstDates);
xticklabels(datestr(firstDates));
months_to_display = [1 7];
months_to_display = months_to_display - 1;
months_to_display(months_to_display == 0) = 12;
months_to_collect = ismember(months, months_to_display);
months = months(months_to_collect);
years = years(months_to_collect);
dayCount = eomdate(years,months);
firstDates = dayCount+1;
figure(2)
plot(Dates, Prices)
xticks(firstDates);
xticklabels(datestr(firstDates));
推荐阅读
- python - 制作一个简单的扑克游戏
- c++ - 如何实现模板函数的容器和迭代器的重载?
- php - Wrong version of the zip extension displayed in php.ini vs pecl install
- python - Flask 对象没有属性“validate_on_submit”
- c++ - Serial port open returning EAGAIN
- java - Convert char hex array to byte array java
- ruby - How to properly mock itnernal services in RSpec?
- html - 灵活的居中 SVG 旋转
- c++ - Inheriting a default template value
- asp.net-core - 在 onPost 方法中使用 IQueryable