11
votes

I have made a plot in Matlab, using:

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:

Scientific Notation on y-axis

Is there any way I can remove the scientific notation and just have the y labels range from -0.0025 to 0.0005? Thanks!

6

6 Answers

11
votes

You could try to manually set the tick labels yourself using sprintf:

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

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;
4
votes

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. Finally, thanks to the post above I tracked the problem down the figure renderer. I was using 'OpenGL'. When I changed to 'zbuffer' the "x10^4" label would properly disappear when I manually reset the tick labels. 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. 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:

 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. My modified version of Lienhard code is given in full below:

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));
1
votes

You can use this code to control the format of tick labels of y axis. This code originates from 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)
1
votes

You have to write the following:

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

On newer versions of matlab (2016b) you can use the functions ytickformat and xtickformat to easily change the format of the labels.