绘制时间序列时如何调整网格密度

How to adjust grid density when plotting a timeseries

绘制 timeseries 时是否可以调整网格密度?

比如以一个包含data here的csv文件开始,其时间点是自去年2月以来的每一天。

T=readtable(filename); % filename is the full file name of the csv file
ts=timeseries([T{:,2}],cellstr([T{:,1}]));
plot(ts,'.')
grid on

结果是 this,其中绘图正确且 4 个网格标签间隔正确。但同时,对于502个时间点,只有4个网格标签。

我应该如何调整横轴的网格密度?


我从 中学到了一种生成自定义网格点的方法。我很想学习任何不同的方法。也许有更简单的方法,因为要求从自定义网格点放宽到自定义网格密度?

MATLAB 网格线是根据 xticks and yticks 所在的位置绘制的。因此,对于您的情况,您可以更改 xticks 属性 来调整水平网格线密度。


您可以使用函数 xticks()yticks() 查看当前报价,它会给您一个当前报价位置的数组。例如,在用你的数据绘制散点图后,我可以这样做:

>> xticks()

ans = 

  1×8 datetime array

   2020-01-01   2020-04-01   2020-07-01   2020-10-01   2021-01-01   2021-04-01   2021-07-01   2021-10-01

在这种情况下,要设置一个新的 xticks 值,我需要为函数提供一个 datetime 类型的数组,因为 x 值是 datetime 类型.

所以如果我想每 30 天画一条网格线,你可以这样做:

% Generate an array of datetime from the first date in your data,
% then incrementing by 30 days until the last day in your data.
tick = your_data.date(1) + (0:30:length(your_data.date))
% Update xticks location
xticks(tick)