在MATLAB图上设置特定数量的刻度线 [英] Setting a specific number of tick marks on MATLAB plot

查看:838
本文介绍了在MATLAB图上设置特定数量的刻度线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何在我在MATLAB中创建的绘图的x轴上设置自定义数量的刻度线,但是我不确定这是否可行.我看到了这个问题,似乎在问同样的事情,但是自那以来,这个答案没有帮助

I am trying to figure out how to set a custom number of tick marks on the x-axis of a plot I am creating in MATLAB, but I am not sure if this is possible. I saw this question that seems to be asking the same thing, but that answer wasn't helpful since

set(gca,'XTick',tickVector);

设置刻度线的位置,而不是刻度线的数量. 我的代码如下.

sets the location of the ticks, not the number of ticks. My code is as follows.

rangeBegin = 100000;
rangeEnd = 200000;
numberOfXTicks = 5;

plot(data(:, 1));
xAxisVals = linspace(rangeBegin, rangeEnd, numberOfXTicks);
%set(gca,'XTick',rangeBegin:rangeEnd); % Doesn't work as expected
set(gca,'XTickLabel',xAxisVals);

因此,在此示例中,我只是在寻找一种方法来强制MATLAB创建在x轴上具有5个刻度的图,以匹配我设置的5个XTickLabel.

So in this example, I am just looking for a way to force MATLAB to create the plot with 5 ticks on the x-axis in order to match the 5 XTickLabels that I have set.

数据是大约为<3000x1>的双精度数组.

data is an array of doubles that is roughly <3000x1>.

我还应该补充一点,我希望我的x轴值来自单独的数组.上面显示的数据数组对应于一个时间数组(未显示...我的糟糕),并且数据数组中的每个值在该时间数组中都有一个对应的值.由于我要从数据数组中选择一个范围,因此我想选择相应的时间值并将其用作x标签.但是很明显,我不想在我的x轴上放置3000个时间标签. 希望这会更清楚.

I should also add that I want my x-axis values to be from a separate array. The data array shown above corresponds to a time array (not shown...my bad), and each value in the data array has a corresponding value in the time array. Since I am selecting a range from the data array, I want to select the corresponding time values and use those as the x labels. But obviously I do not want 3000 time labels on my x-axis. Hopefully this is more clear.

推荐答案

numberOfXTicks = 5;

h = plot(data(:, 1));
xData = get(h,'XData');
set(gca,'Xtick',linspace(xData(1),xData(end),numberOfXTicks))

这篇关于在MATLAB图上设置特定数量的刻度线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆