COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Y axis plot labeling? Want auto scaling, but want to format...

• Follow

Hi there,

I have a series of graphs that are created in batches (up to 400) by a Matlab .m script. The y-axis values can range from less than +/- 2000 to over +/- 5000000. The Matlab auto labeling works well for small numbers (> +/-10,000) but any higher and it starts to plot the y-axis labels as (for example) -2.5, -1.25, 0, 1.25, 2.5 with a x10^4 label at the top of the plot. I need to avoid this labeling, and need real integers plotted along the y-axis. Is there any way I can over-ride the auto labeling that Matlab puts out?

I also want the top-most YLabel and bottom-most YLabel to have the string "nm/sec" append to the label.

Here is an example of what I currently get (A), and what I want (B):

A. What I get from Matlab auto labeling.

x 10^5
5.0 -|
|
|
2.5 -|
|
|
0 -|
|
|
2.5 -|
|
|
5.0 -|

B. This is what I want:

500000 nm/sec -|
|
|
250000 -|
|
|
0 -|
|
|
250000 -|
|
|
500000 nm/sec -|

I have tried to do this by creating a loop that assigns values to two cells - one for the values and one for the tick labels. I then determine the max range from all the values, and choose a number of ticks (5 in this case), and assign values to the cells within the for loop. I then use these cells to manually force the Y-axis values:

my_y_range = my_y_max - my_y_min;

my_y_ticks = 5;
my_y_tickinterval = my_y_range / my_y_ticks;

value_cell = cell(1,my_y_ticks);
label_cell = cell(1,my_y_ticks);

for i=1:my_y_ticks,
value            = my_y_min + (my_y_tickinterval * i);
value_cell{i} = value;
% If we are on the first and last tick value
if i == 0 || i == my_y_ticks
label_cell{i}  = [ value ' nm/sec' ];
else
label_cell{i}  = value;
end
end

% Later in script, get the current axes and set the Y ticks
set( gca, 'YTick', cell2mat(value_cell), 'YTickLabel', label_cell ) ;

However, this does not replicate the nice Matlab auto-scaling.

So I guess I want to be able to pass the YTickLabel plot property some sort of function that will get the output I want. Does anyone have any suggestions?

- Rob

 0

Rob Newman wrote:

> I have a series of graphs that are created in batches (up to 400) by a
> Matlab .m script. The y-axis values can range from less than +/- 2000 to
> over +/- 5000000. The Matlab auto labeling works well for small numbers
> (> +/-10,000) but any higher and it starts to plot the y-axis labels as
> (for example) -2.5, -1.25, 0, 1.25, 2.5 with a x10^4 label at the top of
> the plot. I need to avoid this labeling, and need real integers plotted
> along the y-axis. Is there any way I can over-ride the auto labeling
> that Matlab puts out?

plot normally, then get() the YTick property of the axis: the returned values
will be the exact numeric positions of the ticks. Use sprintf() or num2str()
to format the values however you like, and set() the YTickLabel property to
the result.

For example,

set(gca, 'YTickLabel', num2str(get(gca, 'YTick')));

The numeric exponent like x 10^4 that you see only appears when the
yticklabelmode is automatic, so it when you set the yticklabel, that numeric
exponent will be removed.

 0

Walter Roberson <roberson@hushmail.com> wrote in message <hn8rnf$bsi$1@canopus.cc.umanitoba.ca>...

>
> plot normally, then get() the YTick property of the axis: the returned values
> will be the exact numeric positions of the ticks. Use sprintf() or num2str()
> to format the values however you like, and set() the YTickLabel property to
> the result.
>
> For example,
>
> set(gca, 'YTickLabel', num2str(get(gca, 'YTick')));
>
> The numeric exponent like x 10^4 that you see only appears when the
> yticklabelmode is automatic, so it when you set the yticklabel, that numeric
> exponent will be removed.

Hi Walter,

Thanks for the response. That makes sense. However, the code you list above appears to add all the tick mark values at every mark on the y-axis.

x 10^5
5.0 -|
|
|
2.5 -|
|
|
0 -|
|
|
- 2.5 -|
|
|
- 5.0 -|

I now get:

-500000 -250000 0 250000 500000 -|
|
|
-500000 -250000 0 250000 500000 -|
|
|
-500000 -250000 0 250000 500000 -|
|
|
-500000 -250000 0 250000 500000 -|
|
|
-500000 -250000 0 250000 500000 -|

Any ideas what could be causing that?

Regards.

 0

Rob Newman wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message

>> set(gca, 'YTickLabel', num2str(get(gca, 'YTick')));

> Thanks for the response. That makes sense. However, the code you list
> above appears to add all the tick mark values at every mark on the y-axis.

Sorry, minor correction:

set(gca, 'YTickLabel', num2str(get(gca, 'YTick').'));

Notice the addition of the .' after the get() call; it transforms the row
vector into a column vector so that num2str() will produce a column of
individual labels instead of a combined single row of labels.

 0

"Rob Newman" <rlnewman@ucsd.edu> wrote in message
>
>         x 10^5
>   5.0 -|
>          |
>          |
>    2.5 -|
>          |
>          |
>      0 -|
>          |
>          |
> - 2.5 -|
>          |
>          |
> - 5.0 -|
>
> I now get:
>
> -500000 -250000 0 250000 500000 -|
>                                                     |
>                                                     |
> -500000 -250000 0 250000 500000 -|
>                                                     |
>                                                     |
> -500000 -250000 0 250000 500000 -|
>                                                     |
>                                                     |
> -500000 -250000 0 250000 500000 -|
>                                                     |
>                                                     |
> -500000 -250000 0 250000 500000 -|

If I print out the values of:

num2str(get(gca,'YTick'))

I get the following output from Matlab:

ans =
-500000    -250000     0   250000  500000

ans =
-500000    -250000     0   250000  500000

ans =
-500000    -250000     0   250000  500000

ans =
-500000    -250000     0   250000  500000

ans =
-500000    -250000     0   250000  500000

So it looks like it gets the complete YTick matrix for each label value. Does that seem like correct behavior? FWIW, I am running Matlab Version 7.3.0.298 (R2006b)

Thanks.

 0

Walter Roberson <roberson@hushmail.com> wrote in message <hn97g6$en$1@canopus.cc.umanitoba.ca>...
>
> Sorry, minor correction:
>
> set(gca, 'YTickLabel', num2str(get(gca, 'YTick').'));
>
> Notice the addition of the .' after the get() call; it transforms the row
> vector into a column vector so that num2str() will produce a column of
> individual labels instead of a combined single row of labels.

Great! That worked! Thanks so much. So, in regards to my other question, if I want to append the string 'nm/sec' to the very first value (-500000 to -500000 nm/sec) and also to the very last value (500000 to 500000 nm/sec) on the Y-axis, can I just get the first and last values of that column vector, such as:

mytitles = num2str(get(gca, 'YTick').)
title_length = length(mytitles)

mytitles(1) = [ mytitles(1) 'nm/sec' ]

mytitles(title_length) = [ mytitles(title_length) 'nm/sec' ]

% Then do the row to column vector transform
my_new_column_titles = mytitles'

% Now set the YTickLabels
set(gca, 'YTickLabel', my_new_column_titles )

Am I way off base here?
(Apologies for the pseudo code - I am more used to coding in Python and Perl)

Regards,
- Rob

 0

Rob Newman wrote:

> Great! That worked! Thanks so much. So, in regards to my other question,
> if I want to append the string 'nm/sec' to the very first value (-500000
> to -500000 nm/sec) and also to the very last value (500000 to 500000
> nm/sec) on the Y-axis, can I just get the first and last values of that
> column vector, such as:
>
> mytitles = num2str(get(gca, 'YTick').)
> title_length = length(mytitles)
>
> mytitles(1) = [ mytitles(1) 'nm/sec' ]
> mytitles(title_length) = [ mytitles(title_length) 'nm/sec' ]
>
> % Then do the row to column vector transform
> my_new_column_titles = mytitles'
>
> % Now set the YTickLabels
> set(gca, 'YTickLabel', my_new_column_titles )
>
> Am I way off base here?

Yah, that won't work for a number of reasons.

mytitles = cellstr(num2str(get(gca,'YTick').'));
mytitles{1} = [mytitles{1} ' nm/sec'];
mytitles{end} = [mytitles{end} ' nm/sec'];
set(gca, 'YTickLabel', mytitles);

However, the numbers then won't line up unless you have

set(gca,'YAxisLocation', 'right')

To get the tick label numbers to line up on the left,

set(gca, 'YTickLabel', char(mytitles), 'FontName', 'Courier')


 0

Walter Roberson <roberson@hushmail.com> wrote in message

> Yah, that won't work for a number of reasons.
>
> mytitles = cellstr(num2str(get(gca,'YTick').'));
> mytitles{1} = [mytitles{1} ' nm/sec'];
> mytitles{end} = [mytitles{end} ' nm/sec'];
> set(gca, 'YTickLabel', mytitles);
>
> However, the numbers then won't line up unless you have
>
> set(gca,'YAxisLocation', 'right')
>
>
> To get the tick label numbers to line up on the left,
>
> set(gca, 'YTickLabel', char(mytitles), 'FontName', 'Courier')
>

Hi Walter,

Thanks so much for all the help. All the above worked, and I have learned more about Matlab cells and vector structures than I knew before. Much obliged!!!!

I think I am good for now. All the best,
- Rob

 0

"Rob Newman" <rlnewman@ucsd.edu> wrote in message <hn8psc$i1h$1@fred.mathworks.com>...
> Hi there,
> ...
> I also want the top-most YLabel and bottom-most YLabel to have the string "nm/sec" append to the label.

I have written some routines to do arbitrary labelling of the axis ticks. Normal tick labels don't allow for the use of special symbols, such as the latex symbol '\pi'. To get around this restriction, I have a routine (SetXTickLabel) which erases the existing labels, then places a centered tex string in place of each label. The new text string can include latex symbols.

Another routine (SymbolLabesl) will generate text labels with "units" appended and allows for fractions in the label. For instance "0.75" and a symbol of "\pi" will give a label "3 \pi / 4". The text strings generated can be used as input to SetXTickLabel.

The routines are included in the private folder of a package "PlotFilter" on Matlab Central.

There are some other routines in there that might be of interest. GenTicks generates tick values, which in many cases are more aestetically pleasing than the ones generated by Matlab. There is also a routine for generating minor tick (MidTick)s.

Run the TestPlotFilter routine in the PlotFilter distribution to see examples of the axis labelling generated by using the routines above.

 0

8 Replies
374 Views

Similiar Articles:

7/24/2012 7:28:24 AM