Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Modify-ing tick labels

Subject: Modify-ing tick labels

From: Sierd

Date: 12 Jan, 2010 13:10:07

Message: 1 of 4

Recently I am working with topographic data.
I have the desire to sometimes get rid of the 10^x on the axis ticks. Just dividing the data by 10^x does not always do the job for me so I wrote some simple code which extracts the axis ticks from a figure and divides the axis labels by in this case 10^3 and re-assigns the axis labels. Run this code for an example

--------------
% a random surface is created
[x,y,z] = peaks(30);
x = x*1e4;
y = y*1e3 ;

figure(1)
pcolor(x,y,z);

%%% Lets assume the x and y units are meters and I want them to be in km with %%% two decimals


figure(2)
pcolor(x,y,z);


% from here the ticks are re-written
ticks = get(gca,'YTick');

% generate tick strings
for i = 1: length(ticks)
    proper_tick{i} = num2str(ticks(i)/1000,'%0.2f');
end

set(gca,'yticklabel',proper_tick)

ticks = get(gca,'xTick');

% generate tick strings
for i = 1: length(ticks)
    proper_tick{i} = num2str(ticks(i)/1000,'%0.2f');
end

set(gca,'xticklabel',proper_tick)

% eof
-------------

Notice the difference on the x axis between figure 1 and 2. The 10^4 should not be there in figure 2. I expect this is a bug of some sort because when I do the same operation in a figure created by scatter and I re-label the axes the 10^4 disappears like you would expect.

Does somebody have an idea ?

Subject: Modify-ing tick labels

From: dpb

Date: 12 Jan, 2010 14:39:23

Message: 2 of 4

Sierd wrote:
...

> %%% Lets assume the x and y units are meters and I want them to be in km
> with %%% two decimals
>
...

Why not just plot the x/1000 vs y/1000 then?

--

Subject: Modify-ing tick labels

From: Kelly Kearney

Date: 12 Jan, 2010 15:13:24

Message: 3 of 4

"Sierd " <Sierd.deVries@tudelft.nl> wrote in message <hihsbf$qph$1@fred.mathworks.com>...
> Recently I am working with topographic data.
> I have the desire to sometimes get rid of the 10^x on the axis ticks. Just dividing the data by 10^x does not always do the job for me so I wrote some simple code which extracts the axis ticks from a figure and divides the axis labels by in this case 10^3 and re-assigns the axis labels. Run this code for an example

> Notice the difference on the x axis between figure 1 and 2. The 10^4 should not be there in figure 2. I expect this is a bug of some sort because when I do the same operation in a figure created by scatter and I re-label the axes the 10^4 disappears like you would expect.
>
> Does somebody have an idea ?

I often see the same behavior, where the factor at the end of the axis refuses to disappear even when the ticks are relabeled (I use datetick-ed axes all the time, which is when I notice it). It seems to be a sporadic problem, though, and I've never been able to figure out what conditions cause it to arise.

I wrote a function, tick2text.m, to deal with reformatting of tick labels. It's probably a bit of overkill for this situation, but it works:

[x,y,z] = peaks(30);
x = x*1e4;
y = y*1e3 ;
pcolor(x,y,z);
tick2text(gca, 'axis', 'x', 'xformat', @(x) num2str(x/1000, '%.2f'))

The function is on the FEX:

http://www.mathworks.com/matlabcentral/fileexchange/16003-tick2text-create-easy-to-customize-tick-labels

Subject: Modify-ing tick labels

From: Sebastien

Date: 28 Sep, 2011 22:16:26

Message: 4 of 4

"Kelly Kearney" wrote in message <hii3ik$gk8$1@fred.mathworks.com>...
> "Sierd " <Sierd.deVries@tudelft.nl> wrote in message <hihsbf$qph$1@fred.mathworks.com>...
> > Recently I am working with topographic data.
> > I have the desire to sometimes get rid of the 10^x on the axis ticks. Just dividing the data by 10^x does not always do the job for me so I wrote some simple code which extracts the axis ticks from a figure and divides the axis labels by in this case 10^3 and re-assigns the axis labels. Run this code for an example
>
> > Notice the difference on the x axis between figure 1 and 2. The 10^4 should not be there in figure 2. I expect this is a bug of some sort because when I do the same operation in a figure created by scatter and I re-label the axes the 10^4 disappears like you would expect.
> >
> > Does somebody have an idea ?
>
> I often see the same behavior, where the factor at the end of the axis refuses to disappear even when the ticks are relabeled (I use datetick-ed axes all the time, which is when I notice it). It seems to be a sporadic problem, though, and I've never been able to figure out what conditions cause it to arise.
>
> I wrote a function, tick2text.m, to deal with reformatting of tick labels. It's probably a bit of overkill for this situation, but it works:
>
> [x,y,z] = peaks(30);
> x = x*1e4;
> y = y*1e3 ;
> pcolor(x,y,z);
> tick2text(gca, 'axis', 'x', 'xformat', @(x) num2str(x/1000, '%.2f'))
>
> The function is on the FEX:
>
> http://www.mathworks.com/matlabcentral/fileexchange/16003-tick2text-create-easy-to-customize-tick-labels

I have the same issue with date ticks.
set(gcf,'renderer','painters') solves the problem, but then I loose transparency of patch objects. Same with Zbuffer renderer.

SB

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us