简体   繁体   English

GNU Octave Matlab: Plot 刻度标签

[英]GNU Octave Matlab: Plot tick labeling

I am making a frequency plot and I would like some help on tick labeling.我正在制作一个频率 plot 并且我想要一些关于刻度标记的帮助。 Here is what I have:这是我所拥有的: 频率图

semilogx([200,1000,5000], [0,6,0]);
xlim([20 20000]);
sc = [20:10:100,125:25:175];
scale = [sc,sc*10,sc*100, 20000];
xticks(scale);
xticklabels(scale);
set(gca,'XMinorTick','Off')
grid on;
set (gca, "xminorgrid", "off")
xlabel('frequency (Hz)');
ylabel('dB');
  1. How can I make all numbers from 1000 and upwards appear as 1K, 2K, 5K and so on?如何使 1000 及以上的所有数字显示为 1K、2K、5K 等?
  2. How could I make the lines on 50,100,200,500,1K,2K,5K,10K appear thicker/more black?如何使 50,100,200,500,1K,2K,5K,10K 上的线条看起来更粗/更黑?

In MATLAB在 MATLAB

* I unfortunately could not yet find how to bold the specific lines *不幸的是,我还没有找到如何加粗特定行

Adding the following code allows the ticks to converted to the new names/format suggested in part 1. For part 2 the best I could find out right now is bolding the specific numbers, unfortunately not the specific ticks/lines.添加以下代码可以将刻度转换为第 1 部分中建议的新名称/格式。对于第 2 部分,我现在能找到的最好的方法是将特定数字加粗,不幸的是,不是特定的刻度/行。 Here \bf indicates which labels are to be bolded.这里\bf指示哪些标签要加粗。 All the names will correspond to the positions set originally by your axis vector scale .所有名称都将对应于最初由您的轴矢量scale设置的位置。 The last line in the code below indicates the replacement of the current axis, gca .下面代码中的最后一行表示替换当前轴gca

定制轴

semilogx([200,1000,5000],[0,6,0]);
sc = [20:10:100,125:25:175];
scale = [sc,sc*10,sc*100, 20000];

Current_Axis = gca;
Current_Axis.XMinorTick = 'off';
xlabel('frequency (Hz)'); ylabel('dB');
xlim([20 20000]);
grid on;

X_Scale_Names = {'\bf20'; '30'; '40'; '\bf50'; '60';
'70';'80';'90';'\bf100';'125';'150';'175';'\bf200';'300';'400';
'500';'600';'700';'800';'900';'\bf1K';'1.25K';'1.5K';'1.75K';
'\bf2K';'3K';'4K';'\bf5K';'6K';'7K';'8K';'9K';'\bf10K';'12.5K';'15K';
'17.5K';'20K'};

To Adjust More Grid and Axis Properties:要调整更多网格和轴属性:

Current_Axis = gca;
set(Current_Axis,'xtick',scale,'xticklabel',X_Scale_Names);
Current_Axis.LineWidth = 1;
Current_Axis.GridColor = 'k';
Current_Axis.GridAlpha = 0.5;

Ran using MATLAB R2019b使用 MATLAB R2019b 运行

Octave approach (probably works on matlab too though)八度法(可能也适用于 matlab)

I wouldn't rely on latex trickery to do this to be honest.老实说,我不会依靠 latex 诡计来做到这一点。
Here is the way I usually do stuff like this.这是我通常做这样的事情的方式。
Effectively, because the axis labels object is considered a single object, and you cannot split it into parts, the trick is to overlay an invisible, bare-minimum axes object defining only the labels you want, and treat those as you'd like (eg adjust its fontweight, fontsize, xcolor, etc etc).实际上,由于轴标签 object 被认为是单个 object,并且您不能将其拆分为多个部分,因此诀窍是覆盖一个不可见的、最小的轴 object'd,只定义您想要的标签(例如调整其字体粗细、字体大小、xcolor 等)。

H = semilogx([200,1000,5000], [0,6,0]);
A = gca();
B = axes();

subscale = [20:10:100,125:25:175];
scale    = [subscale,subscale * 10,subscale * 100, 20000];

ScaleTextLabels = {};
for i = 1 : length( scale )
    if scale(i) >= 1000, ScaleTextLabels{i} = sprintf("%dk", scale(i) / 1000 );
    else,                ScaleTextLabels{i} = num2str( scale(i) );
    end
end

SpecialTickLabels   = { '50', '100', '200', '500', '1k', '2k', '5k', '10k'};
ScaleIndices        = 1 : length( ScaleTextLabels );
SpecialIndices      = nthargout( 2, @ismember, SpecialTickLabels, ScaleTextLabels );
NormalIndices       = setdiff( ScaleIndices, SpecialIndices );

set( A, 'xgrid', 'on', 'xlabel', 'frequency (Hz)', 'xlim', [20 20000]      , 'xminorgrid', 'off', 'xminortick', 'off', 'xticklabel', ScaleTextLabels(NormalIndices),  'xtick', scale(NormalIndices) , 'ylabel', 'dB', 'gridlinestyle', ':', 'gridcolor', 'k', 'gridalpha', 0.5 );
set( B, 'xgrid', 'on', 'xlabel', ''              , 'xlim', get( A, 'xlim' ), 'xminorgrid', 'off', 'xminortick', 'off', 'xticklabel', ScaleTextLabels(SpecialIndices), 'xtick', scale(SpecialIndices), 'ylabel', ''  , 'color', 'none', 'fontsize', 12, 'fontweight', 'bold', 'position', get( A, 'position'), 'xcolor', [0,0,0], 'xscale', 'log', 'ylim', get( A, 'ylim'), 'ytick', [], 'gridlinestyle', '--', 'gridcolor', 'k', 'gridalpha', 0.8 );

This "layers of transparent axes objects" technique is very useful to keep in mind in general, it allows great flexibility when designing complex graphs.一般来说,这种“透明轴对象层”技术非常有用,它在设计复杂图形时具有很大的灵活性。 :) :)

I did it like this:我是这样做的:

semilogx([200,1000,5000], [0,6,0]);
xlim([20 20000]);
sc = [20:5:35,40:10:100,125:25:175];
scale = [sc,sc*10,sc*100, 20000];
xticks(scale);
xticklabels(scale);
set(gca,'XMinorTick','Off')
grid on;
set(gca,'gridlinestyle',':');
set(gca,'gridalpha',0.6);
set (gca, "xminorgrid", "off");
xg = [50,100,200,500,1000,2000,5000,10000]; #highlight grids
xx = reshape([xg;xg;NaN(1,length(xg))],1,length(xg)*3);
yy = repmat([ylim() NaN],1,length(xg));
line(xx,yy,'Color',[0.65,0.65,0.65]);
xlabel('frequency (Hz)');
ylabel('dB');
X_Scale_Names = {'\fontsize{11}\bf20'; '25'; '30';'35';'40'; '\fontsize{11}\bf50'; '60';
'70';'80';'90';'\fontsize{11}\bf100';'125';'150';'175';'\fontsize{11}\bf200';'250';'300';'350';'400';
'\fontsize{11}\bf500';'600';'700';'800';'900';'\fontsize{11}\bf1K';'1.25K';'1.5K';'1.75K';
'\fontsize{11}\bf2K';'2.5K';'3K';'3.5K';'4K';'\fontsize{11}\bf5K';'6K';'7K';'8K';'9K';'\fontsize{11}\bf10K';'12.5K';'15K';
'17.5K';'\fontsize{11}\bf20K'};
set(gca,'xtick',scale,'xticklabel',X_Scale_Names);

But I don't think this is the best/fastest/easiest way to do it...但我不认为这是最好/最快/最简单的方法......

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

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