英文:
plot in matlab with similar xlabels spaces
问题
以下是翻译好的部分:
我在Matlab中有一个图形,其中xlabel是2的幂次方值,其中n是整数,ylabel包括任何矢量值,我需要绘制这些值并保持x矢量等间距。我现在所做的是正确的,但当x值较小时,xlabel中会显示小间隙,而当x值较大时,这些间隙会变大。
我所做的是:
M = [2, 4, 8, 16, 32, 64, 128, 256];
s1 = [104 136 168 200 232 264 296 328];
figure(1)
semilogy(M, s1, 'ko-', 'LineWidth', 1); hold on; %
grid;
axis ([0 256 0 600])
xticks(M);
英文:
I have a figure in matlab, where xlabel is 2 power n values, where n is an integer and ylable includes any vector values, I need to plot those values and keep x vector to be equal spaced. What I do now is correct but small spaces in xlabel are shown when x values are small while that becomes larger with higher values of x values.
That is what I did:
M = [2, 4, 8, 16, 32, 64, 128, 256];
s1 = [104 136 168 200 232 264 296 328];
figure(1)
semilogy(M, s1, 'ko-', 'LineWidth', 1); hold on; %
grid;
axis ([0 256 0 600])
xticks(M);
答案1
得分: 2
一种实现均匀间隔的 x 轴刻度的方法是使用一个线性向量 (0,1,2,3,等等...)
,与您的数据具有相同的长度。您将您的 y
数据绘制在这个 xa
(X Apparent,X 显式)向量上,然后只需修改 xticklabels
以显示对应每个点的正确的2的幂。
因此,对于您的代码:
max_xa = numel(M) - 1; % "x" 点的数量(-1,因为我们将从0开始)
xa = 0:max_xa; % 生成 x-显式向量
figure
% 将您的 "sl" 值绘制在 "xa" 上
semilogy(xa, s1, 'ko-', 'LineWidth', 1); grid on ;
axis([0 max_xa 0 600]) % 调整限制
xticklabels(M); % 调整 X 轴刻度标签
将生成以下图像:
注意:我们将 X 显式向量从 0
开始,而不是 1
,因为否则 Matlab 会自动插入一个刻度在值 0
处,而我们的 xticklabels
将全部偏移一个位置。
英文:
One way to have evenly spaced xticks is to use a linear vector (0,1,2,3,etc...)
the same length as your data. You plot your y
data versus this xa
(X Apparent) vector, then just modify the xticklabels
to display the proper power of 2 corresponding to each point.
So for your code:
max_xa = numel(M)-1 ; % number of "x" points (-1 because we'll start at 0)
xa = 0:max_xa ; % generate the x-apparent vector
figure
% Plot your "sl" values versus "xa"
semilogy(xa, s1, 'ko-', 'LineWidth', 1); grid on ;
axis ([0 max_xa 0 600]) % adust limits
xticklabels(M); % adjust X tick labels
will generate the following figure:
Note: We started the X apparent vector at 0
instead of 1
because otherwise Matlab would automatically insert a xtick at the value 0
and our xticklabels
would all be offset by one position.
答案2
得分: 0
你的问题将保持不变,除非你删除一些xticklabels或更好地使用loglog来绘制,以保留抛物线数据的特性。
在你的代码中使用以下行:
loglog(M, s1, 'ko-', 'LineWidth', 1); hold on;
你将得到这个结果 1。
英文:
Your issues will remain unless you drop some xticklabels or better use loglog to plot which retains the feature of parabolic data.
Use following lines in your code:
loglog(M, s1, 'ko-', 'LineWidth', 1); hold on; %
You will get this result 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论