【问题标题】:Matlab - Set xticks for a secondary axis not even with the first axisMatlab - 为辅助轴设置 xticks 甚至不是第一个轴
【发布时间】:2018-09-23 23:57:25
【问题描述】:

我有一个函数可以创建保持某个时间的累积概率(Y 轴上的值介于 0 和 1 之间,x 轴上的天数)。因为我知道距离,所以我考虑添加一个辅助 x 轴,其中包含与时间相关的平均速度。

this example 一样,添加辅助轴相对容易,一开始我设法将 xlim 设置为正确并反转它。

xlabel('Time (days)')
xlim([27 38])
a1Pos = get(gca,'Position');
b = axes('Position',[a1Pos(1) a1Pos(2)-.06 a1Pos(3) a1Pos(4)], 'YTick',[],'YTickLabel',[]);
xmaxb = round(dist/(xmaxa*24));
xminb = round(dist/(xmina*24));
set(b,'Units','normalized');
set(b,'Color','none');
set(b, 'XDir','reverse')
set(b,'xlim',[xminb xmaxb])
xlabel(b,'Average speed on the journey (knots)')

有两个问题,一是四舍五入影响太大,二是更重要的是xminb和xmaxb之间不是线性相关,所以我必须手动添加xticks?我试着这样做:

set(b,'XTick',[12 13 14 15 16 17])

因此,现在的辅助轴变成了空的,我不知道如何设置它们之间的不同间距(右侧 12-13 之间的距离应该大于左侧 16 和 17 之间的距离......)

【问题讨论】:

  • 我假设b 是辅助轴?包含一个数字并提供一个可验证的示例可能会有所帮助。
  • 是的,b是副轴,有什么建议吗?

标签: matlab


【解决方案1】:

我将使用与主轴相同的“XTick”和“Xlim”,并更改'XTickLabel' property。您可以为每个刻度线分配任何字符串(或数字)。

但在定义“XTickLabel”时要明确“XTick”,以便标签与轴上的特定位置相关联。缩放图形可以更改自动生成的刻度线的数量,这会导致您选择的标签出现在错误的位置。

【讨论】:

    【解决方案2】:

    我以为我发布了解决方案,有点像 Cris Luengo 所说的那样,因此(IMO)随着速度值的降低(随着时间的增加)而变得正确非常棘手:

    xmaxb = floor(dist/(xmaxa*24)); %xmaxa = max value of the first xlabel
    xminb = ceil(dist/(xmina*24)); %xmina = min value of the first xlabel
    bticks_lab = fliplr(xmaxb: 0.5:xminb); % spacing 0.5 for the second axis and flip it.
    bticks_loc = (dist./(bticks_lab * 24) - xmina) / (xmaxa-xmina); 
    % The new "b" axis got locations between 0 and 1, this took awhile to figure out!
    b = axes('Position',[a1Pos(1) a1Pos(2)-.06 a1Pos(3) a1Pos(4)], 'YTick',[],'YTickLabel',[]);
    set(b,'Units','normalized');
    set(b,'Color','none');
    set(b,'XTick',bticks_loc)
    set(b,'XTickLabel',{bticks_lab})
    

    希望它在未来对其他人有所帮助!

    【讨论】:

      猜你喜欢
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-29
      相关资源
      最近更新 更多