Question

How to show more decimal places in histogram axis

I have precision long number array in Octave and I would like to show it in histogram. It basically works well but it only shows a few decimal places on the histogram axis. I need to see more numbers in order to evaluate numbers exactly.

I've tried to use the command format long, or different graphics toolkit but without success.

Image

 2  36  2
1 Jan 1970

Solution

 1

The displayed axis labels are strings formatted independently from the command window numerical display (which the format command controls). The way to change this is to get the xaxis values, create a char array with the format you actually want, then set that to a new set of xlabels. See below, modified from a matlab central answer for an older version of matlab:

>> hist (sin(1:40)) ## generate a sample histogram

which will generate: histogram of sin(1:40) with default labels

>> xt = get (gca, 'xtick') ## get numbers used to make current labels
xt =

  -1.0000  -0.5000        0   0.5000   1.0000

Now we can convert to a character array that will have the format you want:

xt_label = num2str (xt.') ## convert to column vector, then to character vectors
>> xt_label = num2str (xt.')
xt_label =

  -1
-0.5
   0
 0.5
   1

Get a format you want using formatting codes:

>> xt_newlabel = num2str (xt.', '%.3f')
xt_newlabel =

-1.000
-0.500
0.000
0.500
1.000

See https://docs.octave.org/latest/Output-Conversion-Syntax.html for a description of the format codes.

Once you have a format you want, display it using the set command.

>> set(gca, 'xticklabel', xt_newlabel)

which will cause the modified labels to be displayed: histogram of sin(1:40) with modified labels

2024-07-19
Nick J