在MATLAB图上缩放日期/时间标签时遇到问题 [英] Having issues scaling date/time labels on MATLAB plot
问题描述
我正在尝试解决在MATLAB中绘制的轴上的棘手问题.我想显示温度预报的日期和时间(因此绘制的日期标签本质上是预报有效"的小时数,以3小时为增量).值的范围大约为5天,但是我当前的图表仅显示了图表上的前五个预测时间(而不是整个范围,但只有几个点),如下所示:
I'm trying to fix a tricky issue with the axis on a plot I'm making in MATLAB. I want to display the dates and times of a temperature forecast (so the date labels plotted are essentially the hour the forecast is "valid" for, in 3-hour increments). The range of values is approximately 5 days, however my current plot is only showing the first five forecast times on the plot (instead the whole range, but only with a few points), as illustrated below:
我当前正在通过创建包含日期标签的字符串矢量显示这些标签(由DateVectors矩阵生成).这样做的代码是:
I'm currently displaying these labels by creating a vector of strings containing the date labels (as generated from a matrix of DateVectors). The code for doing this is:
% Format all the dates into strings that can be displayed on the graph
for rid = 1:numRows
rowdate = formattedDates(rid,:); % DateVector for this forecast step
fcstDateStrs{rid} = datestr(rowdate, 'dd-mmm-yyyy HHZ');
end
然后,我将这些日期作为set(gca,'XtickLabel', {}, 'YtickLabel', fcstDateStrs)
行的y轴.有没有一种方法可以在该轴上缩放标签,以显示所有正确缩放的日期标签(因此第一个标签类似于"03-Oct-2012 06Z",而最后一个标签类似于"08-Oct- 2012 06Z)?
Then, I place these dates as the y-axis with the line set(gca,'XtickLabel', {}, 'YtickLabel', fcstDateStrs)
. Is there a way I can scale the labels on that axis to show all of the date labels properly scaled (so that the first one was like "03-Oct-2012 06Z" and the last one would be something like "08-Oct-2012 06Z")?
推荐答案
您将不得不弄乱轴的ytick
属性:
You'll have to mess with the ytick
property of the axes:
ticks = get(gca, 'ytick')
将为您提供X
轴上当前分配的刻度线的向量.您可以使用这些设置所需的刻度数:
will give you a vector of currently assigned tickmarks on the X
-axis. You can use these to set the amount of ticks you want:
newTicks = linspace(ticks(1), ticks(end), numel(fcstDateStrs));
set(gca,...
'Yticks' , newTicks,...
'YtickLabels', fcstDateStrs);
请注意,由于您现在已经手动设置了刻度和刻度标签,因此重新缩放绘图会有些尴尬-仅当您确定该绘图不会再有太大变化时才执行此步骤.
Note that since you've now set the ticks and tick labels manually, re-scaling the plot will be somewhat awkward -- this step is only to be executed once you're fairly sure the plot won't change much anymore.
这篇关于在MATLAB图上缩放日期/时间标签时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!