如何在每行上添加标签,并在"loglog"图的右侧添加第二个y轴? [英] How to add labels on each line and add 2nd y-axes to the right side of the 'loglog' plot?

查看:137
本文介绍了如何在每行上添加标签,并在"loglog"图的右侧添加第二个y轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我正在尝试对道路评估实施ISO分类.请参阅论文使用车辆加速度测量值来估算道路不平整度.但是我遇到了一些有关绘制ISO分类图的问题.代码如下所示:

Recently I am trying to implement the ISO classification for the roadway assessment. Please refer to the paper The use of vehicle acceleration measurements to estimate road roughness. But I bumped into some questions about plotting the ISO classification graph. The code is shown as follows:

%% generate ISO Clasification Curve (m^2/(rad/m))
AngSpaFre = 10^(-3):0.01:10^2;                                  % spatial frequency (rad/m)
ParamTable = [0              1*10^(-6)       2*(10^(-6));       % ISO classification parameters
              2*(10^(-6))    4*(10^(-6))     8*(10^(-6));       % (lower bound, geometric average, upper bound)
              8*(10^(-6))    16*(10^(-6))    32*(10^(-6));
              32*(10^(-6))   64*(10^(-6))    128*(10^(-6));
              128*(10^(-6))  256*(10^(-6))   512*(10^(-6));
              512*(10^(-6))  1024*(10^(-6))  2048*(10^(-6));
              2048*(10^(-6)) 4096*(10^(-6))  8192*(10^(-6));
              8192*(10^(-6)) 16384*(10^(-6)) 32768*(10^(-6))];
class_num = 8;
len = length(AngSpaFre);
iso_table = zeros(class_num, len);
for i = 1:class_num
  geo_mean = ParamTable(i, 2);
  geo_upbound = ParamTable(i, 3);
  for j = 1:len
    if(AngSpaFre(j) <= 1)
        iso_table(i,j) = geo_mean*(AngSpaFre(j)^(-2));
    else
        iso_table(i,j) = geo_mean*(AngSpaFre(j)^(-1.5));
    end
  end
end

figure,
ht = loglog(AngSpaFre, iso_table(1,:), AngSpaFre, iso_table(2,:), AngSpaFre, iso_table(3,:), AngSpaFre, iso_table(4,:), AngSpaFre, iso_table(5,:), AngSpaFre, iso_table(6,:), AngSpaFre, iso_table(7,:), AngSpaFre, iso_table(8,:));
hY = get(gca,'ylim');
hold on;
loglog([1 1], hY);
xlabel('Spatial Frequency \Omega (rad/m)');
ylabel('PSD (m^2/(rad/m))'); 
title('ISO Classification (log-log scale)');

当前结果如下图所示:

And the current result is illustrated as the figure bellow:

然后在我的实现中出现两个问题,希望任何人都可以给我一些解决方案或简单的示例,以使问题更清楚.

Then 2 questions comes in my implementation and hope any one could give me some solutions or simple examples to make the problems clear.

Q1:由于分类曲线应能识别道路的质量水平.因此,我想在每行下方添加标签以指示级别,如下图所示.我如何在Matlab中实现这一点?

Q1: Since the classification curve should recognize the quality-level of a road. So I'd like to add labels below each line to indicate the level like the graph below. How can I realize this in matlab?

Q2:另外,我想在"loglog"图的右侧添加另一个y轴,而没有对数刻度值,如下图所示.如何实现呢?我在官方网站上提到过很多例子,但大多数都集中在情节"/忠诚"的例子上. :(

Q2: Also, I'd like to add another y-axis on the right-side of the 'loglog' plot without log-scale values like the figure below. How to achieve this? I've refer to many examples in official website but most focus on the 'plot'/'plotyy' examples. :(

推荐答案

答案第一部分

将文本放置在某处的常用方法是 注释 功能.要使其基本功能正确无误,所以要与 latex解释器一起使用,因为它允许您用'\vspace{gapwidth}'指定每行与很多 更多选项 :

Answer Part I

The common way to put text somewhere is the annotation function. It's quite hard do get it right with its basic functions, so rather use it with the latex interpreter as it allows you to specify the gap between every line with '\vspace{gapwidth}' and a lot more options:

strings = {'H','G','F','E','D','C','B','A'};
verticalspace = '\vspace{4pt}';
str = cellfun(@(x) [x verticalspace],strings,'uni',0);


annotation('textbox', [0.4,0.6,0.1,0.1],...
           'String', str,...
           'LineStyle','none','Interpreter','latex');

这肯定很奇怪,您应该使用固定的图形宽度.

It's definitely fiddly and you should use a fixed figure width.

一种替代方法是使用 自定义数据提示 ,并根据需要对其进行调整.优点是文本框的位置是相对于您的数据而不是数字.

An alternative is to use custom datatips and tweak them as desired. Advantage is that the position of the textbox is relative to your data and not figure.

我的一般建议:完全不要使用Matlab进行此类调整,这是a ...中的痛苦.将图形另存为pdf(set(gcf,'renderer','painters')),并使用任何矢量图形程序或直接使用LaTeX/Tikz进行后期处理.

My general recommendation: don't use Matlab at all for doing such tweaking, it's pain in the a... . Save your figure as pdf (set(gcf,'renderer','painters')) and do the post processing with any vector graphics program or directly with LaTeX/Tikz.

要获取第二个轴,请使用:

To get a second axes, use:

ax1 = gca;
ax2 = axes('Position',ax1.Position,'Color','none');
ax2.YAxisLocation = 'right';
ax2.YLim = [0, 120];
ax2.YTick = 0:10:120;
ax2.XTick = [];

再次,在此之前指定确切的图形尺寸!之后调整大小只会带来麻烦.

And again, specify the exact figure dimensions before! Resizing afterwards just makes troubles.

在您当前的代码下面:

%%
f = gcf;
f.Units = 'pixels';
f.Position = ([100,100,1000,800]);

%%
strings = {'H','G','F','E','D','C','B','A'};
verticalspace = '\vspace{7.6pt}';
str = cellfun(@(x) [x verticalspace],strings,'uni',0);

annotation('textbox', [0.4,0.61,0.1,0.1],...
           'String', str,...
           'LineStyle','none','Interpreter','latex');

%%
ax1 = gca;
ax2 = axes('Position',ax1.Position,'Color','none');
ax2.YAxisLocation = 'right';
ax2.YLim = [0, 120];
ax2.YTick = 0:10:120;
ax2.XTick = [];
linkaxes([ax1,ax2],'x')

愿意提供(不调整大小):

would give (not resized):

这篇关于如何在每行上添加标签,并在"loglog"图的右侧添加第二个y轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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