【问题标题】:How to fix the x tick label in MATLAB?如何修复 MATLAB 中的 x 刻度标签?
【发布时间】:2015-11-09 15:42:16
【问题描述】:

我在 excel 中有一些数据,包含以下信息

Rx        SD      TP         Time
-64.27  1.66    20.99   15:36:14.58
-62     1.58    29.22   15:38:50.53
-62.33  1.55    29.23   15:41:27.53
-61.17  1.33    29.26   15:44:04.54
-60.15  1.05    29.22   15:46:41.52
-62.53  1.46    29.14   15:49:18.55
-64.1   1.48    25.07   15:51:55.50
-62.45  1.52    28.91   15:54:31.51
-65.14  1.41    21.86   15:57:08.46
-63.61  2.05    20.05   15:59:44.50



.
.
.
.
-59.56  0.6    29.1     17:15:20.57
-59.71  0.68    29.1    17:17:57.58
-59.44  0.57    29.14   17:20:34.59
-59.62  0.64    29.12   17:23:11.60
-59.58  0.64    29.15   17:25:48.51

我已使用以下方法从 excel 中提取信息:

[tp,t] = xlsread(data,xlsheet,xlrange)

其中 t 存储时间值。

我在matlab中分别提取了Rx、SD和TP的这些值:

tp_w= tp(:,3);
rx_pow= tp(:,1);
rx_err= tp(:,2);
t1=datenum(t);

对于我的图表,我需要在subplot (2,1,1) 中绘制 TP 与时间的关系,并在subplot (2,1,2) 中绘制带有 SD 的 Rx 与时间关系。 图:我用下面的方法绘制了下图。

a1=subplot(2,1,1)
plot(t1,tp_w,'-gd','Linewidth',1);
xlim([min(t1),max(t1)])
ylim([0,30])
set(gca,'yTick',0:1:30)
datetick('x',13) 
xlabel('Time);
ylabel('TP ');
grid on; 

对于有错误的 Rx,我正在使用 errobar 函数:

a2=subplot(2,1,2)
errorbar(t1,rx_pow,rx,'-rd','Linewidth',1);
xlim([min(t1),max(t1)])
ylim([-70,-53])
set(gca,'yTick',-70:0.5:-53)
datetick('x',13) 
xlabel('Time);
ylabel('Rx ');
grid on; 

linkaxes([a1,a2],'x')

hold on

Q1:我的数据从15:36:15.73开始,到17:25:48.51结束。如何使我的图表从15:36:00 开始并以17:25:48.51 结束,这样我在开始和结束时就没有间隙。

Q2:如何添加更多 x-tick 以便在 X 轴上有更多标签?例如。 16:45:00...17:00:00...17:15:00等等

编辑1:添加绘图功能后更新:

h = gca;
set(h, 'XTick', [(55200/86400):(90/86400):(63000/86400)]); 
datetick('x', 13', 'keeplimits', 'keepticks');

EDIT2:进行必要的更改 评论 XTick:

a1=subplot(2,1,1)
plot(t1,tp_w,'-gd','Linewidth',1);
xlim([min(t1),max(t1)])
% h = gca;
% set(h,'XTick',[(55100/86400):(900/86400):(62140/86400)]); 
ylim([0,30])
set(gca,'yTick',0:1:30)
datetick('x', 13,'keepticks','keeplimits');
xlabel('Time');
ylabel('TP ');
grid on; 

最后保留 datetick 并注释链接轴:

a1=subplot(2,1,1)
plot(t1,tp_w,'-gd','Linewidth',1);
xlim([min(t1),max(t1)])
h = gca;
set(h,'XTick',[(55100/86400):(900/86400):(62140/86400)]); 
ylim([0,30])
set(gca,'yTick',0:1:30)
datetick('x', 13,'keepticks','keeplimits');
xlabel('Time');
ylabel('TP ');
grid on; 

没有轴:

get(gca) 的结果(linkaxes off + datetick 保留在末尾):

get(gca)
    ActivePositionProperty = outerposition
    ALim = [0 1]
    ALimMode = auto
    AmbientLightColor = [1 1 1]
    Box = off
    CameraPosition = [0.5 0.5 9.16025]
    CameraPositionMode = auto
    CameraTarget = [0.5 0.5 0.5]
    CameraTargetMode = auto
    CameraUpVector = [0 1 0]
    CameraUpVectorMode = auto
    CameraViewAngle = [6.60861]
    CameraViewAngleMode = auto
    CLim = [0 1]
    CLimMode = auto
    Color = [1 1 1]
    CurrentPoint = [ (2 by 3) double array]
    ColorOrder = [ (7 by 3) double array]
    DataAspectRatio = [1 1 1]
    DataAspectRatioMode = auto
    DrawMode = normal
    FontAngle = normal
    FontName = Helvetica
    FontSize = [10]
    FontUnits = points
    FontWeight = normal
    GridLineStyle = :
    Layer = bottom
    LineStyleOrder = -
    LineWidth = [0.5]
    MinorGridLineStyle = :
    NextPlot = replace
    OuterPosition = [0 0 1 1]
    PlotBoxAspectRatio = [1 1 1]
    PlotBoxAspectRatioMode = auto
    Projection = orthographic
    Position = [0.13 0.11 0.775 0.815]
    TickLength = [0.01 0.025]
    TickDir = in
    TickDirMode = auto
    TightInset = [0.0392857 0.0404762 0.00892857 0.0190476]
    Title = [174.006]
    Units = normalized
    View = [0 90]
    XColor = [0 0 0]
    XDir = normal
    XGrid = off
    XLabel = [175.006]
    XAxisLocation = bottom
    XLim = [0 1]
    XLimMode = auto
    XMinorGrid = off
    XMinorTick = off
    XScale = linear
    XTick = [ (1 by 11) double array]
    XTickLabel = [ (11 by 3) char array]
    XTickLabelMode = auto
    XTickMode = auto
    YColor = [0 0 0]
    YDir = normal
    YGrid = off
    YLabel = [176.006]
    YAxisLocation = left
    YLim = [0 1]
    YLimMode = auto
    YMinorGrid = off
    YMinorTick = off
    YScale = linear
    YTick = [ (1 by 11) double array]
    YTickLabel = [ (11 by 3) char array]
    YTickLabelMode = auto
    YTickMode = auto
    ZColor = [0 0 0]
    ZDir = normal
    ZGrid = off
    ZLabel = [177.006]
    ZLim = [0 1]
    ZLimMode = auto
    ZMinorGrid = off
    ZMinorTick = off
    ZScale = linear
    ZTick = [0 0.5 1]
    ZTickLabel = 
    ZTickLabelMode = auto
    ZTickMode = auto

    BeingDeleted = off
    ButtonDownFcn = 
    Children = []
    Clipping = on
    CreateFcn = 
    DeleteFcn = 
    BusyAction = queue
    HandleVisibility = on
    HitTest = on
    Interruptible = on
    Parent = [1]
    Selected = off
    SelectionHighlight = on
    Tag = 
    Type = axes
    UIContextMenu = []
    UserData = []
    Visible = on

【问题讨论】:

    标签: matlab


    【解决方案1】:

    我认为您设置限制的做法是正确的。问题是 datetick 把它们弄乱了。

    首先,要更改刻度数,请使用坐标区的“XTick”属性。例如:

    h = gca;
    set(h, 'XTick', [(55800/86400):(900/86400):(63000/86400)]);  %% This should do 15-minute increments...change the "900" to the seconds value that you want for intervals
    

    棘手的是 datetick 会与您的限制和刻度线混淆,除非您明确告诉它不要这样做。

    datetick('x', 13', 'keeplimits', 'keepticks');
    

    【讨论】:

    • 按照您的建议添加以下功能后,图表从原点开始,但没有轴。我已经更新了图表。
    • 您是否将 datetick() 命令作为最后一个命令运行?我会在没有链接轴命令的情况下尝试它,看看是否有帮助。如果没有,请运行 get(gca) 并在此处发布结果。
    • 嗨,评论 XTick 并仅使用 datetick 从原点开始图表,Xtick 标签再次未修复。查看编辑部分中的图表。
    • 好吧,我有点惊讶。我几乎正在运行您的代码,但没有得到相同的结果。在我看来,您的 XTick 设置可能不正确。如果您碰巧将 XTick 设置为 xlim 之外的一组值,您将准确地看到您正在显示的内容......根本没有刻度。因此,我将一次单步执行您的代码,观察图形如何变化,并在每一步之后,在命令提示符处运行 get(gca, 'XTick') 以确保您的 xTicks 保持在绘图范围内。抱歉,因为无法重现您的问题,所以我想不到其他任何事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多