我正在使用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')
如果我没有清楚地解释这个问题,请随时询问。
答案 0 :(得分:1)
问题似乎是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');