我正在使用Matlab绘制数据并尝试处理刻度线/刻度线标签。
我打算做什么
绘制带有动态刻度标签的图形,以便我可以找到图形上特定点的时间戳。因此,时间戳记(xTickLabel
)仅应来自数据源。
我取得了什么
现在我能:
绘制带有动态刻度标签的图形,但这些标签不够精细:这些标签每刻度一秒钟。
这是绘图的代码:
fig = figure;
x = M(:, 1);
% convert the x (time) value from unix time to matlab time
x = (x / 86400 / 1000) + datenum(1970,1,1);
y1 = M(1:end, 2);
y2 = M(1:end, 3);
y3 = M(1:end, 4);
plot(x, y1, x, y2, x, y3);
xlim([x(1), x(end)]);
set(gca, 'XTick', x);
grid on;
datetick('x','yyyy-mm-dd HH:MM:SS.FFF', 'keeplimits', 'keepticks');
xticklabel_rotate;
hleg1 = legend('x', 'y', 'z');
% zoomAdaptiveDateTicks('on');
该代码运行良好,并且可以在不启用
zoomAdaptiveDateTicks('on')
的情况下使用源时间戳记每个时间戳绘制图形,原始图形如下所示:如果启用了
zoomAdaptiveDateTicks('on')
,则该图形将具有动态刻度线标签,但是无论缩放比例如何,标签都为每刻度一秒钟。我的问题
如何使图形同时具有精细刻度和动态刻度标记?
我试图找出刻度标签的值,但是没有运气,还有其他方法吗?
谢谢,
供您参考,这是
zoomAdaptiveDateTicks
(link)的代码 function zoomAdaptiveDateTicks(varargin)
% ZOOMADAPTIVEDATETICKS - Make date ticks adapt to zooming
%
% zoomAdaptiveDateTicks('on')
% Turns on the automatic adaptation of date ticks
% to user zooming for the current figure window
%
% zoomAdaptiveDateTicks('off')
% Turns off the automatic adaptation of date ticks
% to user zooming for the current figure window
%
% zoomAdaptiveDateTicks('demo')
% Opens a demo figure window to play with
if (nargin>0)
switch varargin{1}
case 'demo'
% Create demo values
dates = floor(now) - linspace(1169,0,15000)';
values= randn(15000,1);
% Show data with date ticks
figure
plot(dates,values)
datetick('x')
zoomAdaptiveDateTicks('on')
case 'on'
% Define a post zoom callback
set(zoom(gcf),'ActionPostCallback', @adaptiveDateTicks);
case 'off'
% Delete the post zoom callback
set(zoom(gcf),'ActionPostCallback', '');
otherwise
figure(gcf)
end
end
function adaptiveDateTicks(figureHandle,eventObjectHandle)
% Resetting x axis to automatic tick mark generation
set(eventObjectHandle.Axes,'XTickMode','auto')
% get and set the tick length
% a = get(eventObjectHandle.Axes, 'XTick');
% minimum = min(a);
% maximum = max(a);
fprintf('minimum :%s, maximum:%s \n', num2str(minimum), num2str(maximum));
% set(eventObjectHandle.Axes, 'XTick'
%set(eventObjectHandle.Axes, 'TickLength', 0.5*(get(eventObjectHandle.Axes, 'TickLength')))
% using automaticallly generate date ticks
datetick(eventObjectHandle.Axes,'x','keeplimits')
请随时问我是否不清楚。
最佳答案
问题似乎出在xticklabel_rotate
上,因为它冻结了标签。
所以我想也许我应该在回调中做这样的事情:
set(gca,'XTickLabelMode','auto');
xticklabel_rotate;
但这没有用...然后我注意到
xticklabel_rotate.m
中的第35行:% Note : you can not RE-RUN xticklabel_rotate on the same graph.
您应该尝试使用rotateXLabels而不是
xticklabel_rotate
。根据ss1271的注释,以下代码能够实现所需的结果。所需的修改是将
zoomAdaptiveDateTicks('on')
和xticklabel_rotate
替换为rotateXLabels(gca, 90)
:close all force; clear variables; clc;
M = [(1:1000)',randn(1000,3)]; ...' //This is just to fix SO formatting
fig = figure(1337);
x = M(:, 1);
% convert the x (time) value from unix time to matlab time
x = (x / 86400 / 1000) + datenum(1970,1,1);
y1 = M(:, 2);
y2 = M(:, 3);
y3 = M(:, 4);
plot(x, y1, x, y2, x, y3);
xlim([x(1), x(end)]);
set(gca, 'XTick', x);
grid on;
datetick('x','yyyy-mm-dd HH:MM:SS.FFF','keepticks','keeplimits');
rotateXLabels(gca, 90);
hleg1 = legend('x', 'y', 'z');