简体   繁体   English

MATLAB:绘制双底x轴

[英]MATLAB: Plotting double bottom x-axis

I'm trying to plot double bottom x-axis according to the figure after the jump: 我试图在跳转后根据图形绘制双底x轴:

在此输入图像描述

It is a semilogx-plot in hours with additional specific ticks representing minutes, months and years. 这是一个半小时的semilogx图,其中包含代表分钟,月和年的额外特定刻度。 How do I create these additional ticks? 如何创建这些额外的刻度?

You can use Victor May's approach to overlay a secondary axes, but this needs more tweaking to actually work. 您可以使用Victor May的方法来覆盖辅助轴,但这需要更多调整才能实际工作。

First, let's recreate the basic plot: 首先,让我们重新创建基本情节:

clear, close
time = [1/60, 1, 24, 24*7, 24*30, 24*30*6, 24*365, 24*365*10];
r = [110, 90, 80, 75, 70, 65, 63, 60];

% plot the data
semilogx(time, r, 'o-')

% adjust ticks and format of primary axes
xlim([0.005 1e6])
ylim([0 140])
tick = 10 .^ (-2 : 6);
set(gca, 'XTick', tick)
set(gca, 'XTickLabel', arrayfun(@num2str, tick, 'UniformOutput', false))
set(gca, 'XMinorTick', 'off')
set(gca, 'TickDir', 'out')

在此输入图像描述

Overlaying a secondary axes only works properly if it has the same position, size, axis limits, and scale type as the primary, and if its background is transparent (otherwise the data become hidden): 如果辅助轴具有与主要轴相同的位置,大小,轴限制和比例类型, 并且其背景是透明的(否则数据将被隐藏),则仅覆盖辅助轴才能正常工作:

% put matching secondary axes on top with transparent background
pos = get(gca, 'Position');
axes('Position', pos)
set(gca, 'Color', 'none')
xlim([0.005 1e6])
ylim([0 140])
set(gca, 'XScale', 'log')
set(gca, 'XMinorTick', 'off')
set(gca, 'TickDir', 'out')

Giving it the proper ticks and tick labels 给它正确的刻度和刻度标签

% adjust ticks
set(gca, 'YTick', [])
set(gca, 'XTick', time)
label = {'1 min', '1 hour', '24 hours', '1 week', '1 month', '6 months', '1 year', '10 years'};
set(gca, 'XTickLabel', label)

results in 结果是

在此输入图像描述

– not really what we want. - 不是我们想要的。

With a trick we can let the ticks and tick labels of the secondary axes go inside... 通过一个技巧,我们可以让辅助轴的刻度和刻度标签进入......

% tinker with it
set(gca, 'XAxisLocation', 'top')
pos(4) = eps * pos(4);
set(gca, 'Position', pos)

在此输入图像描述

...but that's still not really what we want. ......但那仍然不是我们想要的。


A different strategy: Let's not overlay axes, but put the additional ticks in ourselves! 一个不同的策略:让我们不要叠加轴,而是将额外的刻度放在我们自己身上!

label = {'1 min', '1 hour', '24 hours', '1 week', '1 month', '6 months', '1 year', '10 years'};
line([time', time'], [0 2], 'Color', 'k')
text(time, 4 * ones(size(time)), label, 'Rotation', 90, 'VerticalAlignment', 'middle')

The result 结果

在此输入图像描述

is still not perfect, but useable. 仍然不完美,但可用。

You can do this by adding another axes object to the plot and then setting the properties of its tick marks. 您可以通过向绘图添加另一个轴对象,然后设置其刻度线的属性来完成此操作。 Something like this: 像这样的东西:

close all
plot(1:10, 1:10)
set(gca,'XTick',[1:2:10])
haxes1 = gca;
set(haxes1, 'TickDir', 'out')
haxes1_pos = get(haxes1,'Position'); % store position of first axes
haxes2 = axes('Position',haxes1_pos);
set(gca, 'Color', 'none')
set(haxes2, 'YTick', [])
set(haxes2, 'XTickMode', 'manual');
set(haxes2, 'XTick', [ 0.1 0.8])
set(haxes2, 'TickDir', 'in')

In addition to the code in this example, you will have to edit the tick labels and axis range of the additional axes object. 除了此示例中的代码之外,您还必须编辑附加轴对象的刻度标签和轴范围。 See here for reference on the XLim and XTickLabel properties which do that. 请参阅此处以获取有关XLim和XTickLabel属性的参考。

MATLAB doesn't currently support tick labels inside the axes. MATLAB目前不支持轴内的刻度标签。 A 3rd party function that handles this can be found here . 可以在此处找到处理此问题的第三方功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM