在 Matlab 中绘制数据的问题(在 x 轴上设置月份和年份)

Problem plotting data in Matlab (setting month and year in the x-axis)

我在 MATLAB 中有以下 table 结构:

Year  Month  datapoint
1990   1        5
1990   2        7
.
.
.
1995   12       3

我想用 y 轴上的数据点和 x 轴上的 1990_1、1990_2... 之类的东西来绘制它。

我该怎么做?

您可以通过使用 get 函数获取该对象的句柄来格式化 XAxis 的外观,然后直接修改属性。

% Create example table
t = table();
t.Year = repelem(1990,72,1);
t.Month = [1:72].';
t.datapoint = [5:76].';

plot(t.datapoint)

% Get x axis
xaxis = get(gca,'XAxis');

% Format tick labels
xaxis.TickLabels = compose('%d_%d',t.Year,t.Month);

% Format interpreter
xaxis.TickLabelInterpreter = 'none';

% Limit number of ticks
xaxis.TickValues = 1:numel(t.datapoint);


根据您的评论,仅查看每 12 个标签:

indx = 1:72;
indx(12:12:72) = 0;
indx(indx > 1) = 1;
xaxis.TickLabels(find(indx)) = {''}