简体   繁体   English

删除Matlab图的刻度标签中的科学记数法

[英]Removing scientific notation in the tick label of a Matlab plot

I have made a plot in Matlab, using: 我在Matlab中制作了一个情节,使用:

hold on
plot(t1,Dx1,'r')
xlabel('t (ps)')
ylabel('Deviation of coordinate from initial coordinate (Å)')
plot(t1,Dy1,'g')
plot(t1,Dz1,'b')
hold off

However, the tick labels on the y axis are generated in scientific notation: 但是,y轴上的刻度标签是用科学记数法生成的:

y轴上的科学记数法

Is there any way I can remove the scientific notation and just have the y labels range from -0.0025 to 0.0005? 有什么方法可以删除科学记数法,并且y标签的范围是-0.0025到0.0005吗? Thanks! 谢谢!

You could try to manually set the tick labels yourself using sprintf: 您可以尝试使用sprintf自己手动设置刻度标签:

yt = get(gca,'YTick');
set(gca,'YTickLabel', sprintf('%.4f|',yt))

I also fought with getting my plot axes to display in fixed notion instead of scientific notation. 我也努力让我的情节轴以固定的概念而不是科学的符号显示。 The most frustrating part for me was that the "x10^4" label would remain on the edge of the plot box even after I reassigned the tick labels manually to fixed notation. 对我来说最令人沮丧的部分是,即使我手动将刻度标签重新分配给固定符号,“x10 ^ 4”标签仍会保留在绘图框的边缘。 Finally, thanks to the post above I tracked the problem down the figure renderer. 最后,感谢上面的帖子,我在图形渲染器中追踪了问题。 I was using 'OpenGL'. 我正在使用'OpenGL'。 When I changed to 'zbuffer' the "x10^4" label would properly disappear when I manually reset the tick labels. 当我改为'zbuffer'时,当我手动重置刻度标签时,“x10 ^ 4”标签会正确消失。 Here's an example code that apples the format '###,###.0' to the y-axis labels, and even dynamically updates the y-labels when you zoom/pan etc, thanks to two helpful functions I found on the Matlab File Exchange. 这是一个示例代码,它将格式'###,###。0'应用于y轴标签,甚至在缩放/平移等时动态更新y标签,这要归功于我在上面找到的两个有用的功能。 Matlab文件交换。 The place to find the other two functions is included as comments below example function. 找到其他两个函数的位置包含在下面的示例函数的注释中。

function []=TickFixExample()

figure %this one works
myRenderer='zbuffer';
set(gcf,'Renderer', myRenderer); 
axesh = axes();
set(gca,'YLim',[20000 20100]);
title(myRenderer)
ticklabelformat(gca,'y','###,###.0');

figure %this one doesn’t work
myRenderer='OpenGL';
set(gcf,'Renderer', myRenderer); 
axesh = axes();
set(gca,'YLim',[20000 20100]);
title(myRenderer)
ticklabelformat(gca,'y','###,###.0');

function ticklabelformat(hAxes,axName,format) by Y. Altman, can be found at: http://www.mathworks.com/matlabcentral/fileexchange/36254-ticklabelformat-set-a-dynamic-format-of-axes-tick-labels or by googling 'ticklabelformat matlab' I modified it slightly by changing line 105 as follows: 函数ticklabelformat(hAxes,axName,format)由Y. Altman提供,可在以下网址找到: http//www.mathworks.com/matlabcentral/fileexchange/36254-ticklabelformat-set-a-dynamic-format-of-axes-tick - 标签或通过谷歌搜索'ticklabelformat matlab'我通过更改第105行稍微修改了它,如下所示:

 tickLabels = arrayfun(@(x)(FormatNumberScalarInputStrOutput`(x,format)),tickValues,'UniformOutput',false);`

in lieu of Altman's version: 代替奥特曼的版本:

tickLabels = arrayfun(@(x)(sprintf(format,x)),tickValues,'UniformOutput',false);

that change provides for the thousands comma separator functionality by function y = NumberFormatter( Numbers , FormatPattern ) by S. Lienhard, also on Matlab File Exchange. 该更改通过S.Lienhard的函数y = NumberFormatter(Numbers,FormatPattern)提供了数千个逗号分隔符功能,也是在Matlab文件交换上。 My modified version of Lienhard code is given in full below: 我修改后的Lienhard代码版本如下:

function y = FormatNumberScalarInputStrOutput(Number ,FormatPattern)

 % adapted 12-2012 by D. Bourgoyne from NUMBERFORMATTER by S. Lienhard
% 
%   The pound sign (#) denotes a digit, the comma is a placeholder for the
%   grouping separator, and the period is a placeholder for the decimal
%   separator.
%   The pattern specifies leading and trailing zeros, because the 0
%   character is used instead of the pound sign (#).
% 
%   Examples:
%   NumberFormatter(rand(5),'0.000')
%   NumberFormatter(rand(5)*100,'###,###.000') 
import java.text.*
v = DecimalFormat(FormatPattern);
y = char(v.format(Number));

Try adding this after creating the axes: 尝试在创建轴后添加此项:

ax = gca;
ax.YAxis.Exponent = 0;

Here is an example: 这是一个例子:

x = 0:0.1:10;
y = 1000*x.^2;

%Plot with default notation:

subplot(1,2,1)
plot(x,y)


%Plot without exponent:

subplot(1,2,2)
plot(x,y)
ax = gca
ax.YAxis.Exponent = 0;

You can use this code to control the format of tick labels of y axis. 您可以使用此代码来控制y轴的刻度标签的格式。 This code originates from ticks_format.m . 此代码源自ticks_format.m。

% Set the preferred tick format here. %在此处设置首选刻度格式。

y_formatstring = '%3.4f';

% Here's the code. %这是代码。

ytick = get(gca, 'ytick');
for i = 1:length(ytick)
    yticklabel{i} = sprintf(y_formatstring, ytick(i));
end
set(gca, 'yticklabel', yticklabel)

你必须写下面的内容:

set(gcf, 'renderer', 'zbuffer')

在较新版本的matlab(2016b)上,您可以使用函数ytickformat和xtickformat轻松更改标签的格式。

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

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