change the values of X axis in histogram to codes

3 Ansichten (letzte 30 Tage)
bero
bero am 7 Mär. 2016
Kommentiert: Adam am 7 Mär. 2016
Hi,
I have a sample data:
A=[11689
11688
10951
11695
11650
11650
11650
11650
11650
10951
10951
10951
10951
10951
11689
11689
11689
11689
11689
11689
11689
11689
11689
11689
11689
11650
11650
]
I made histogram, I need to change the X axis values to codes like [A B C D E]...
I tried:
x=[11689 11688 10951 11695 11650];
str={'A','B','C','D','E'};
histogram(A);
set(gca, 'XTickLabel',str, 'XTick',1:numel(str));
But nothing in X axis...

Antworten (1)

Adam
Adam am 7 Mär. 2016
Your tick values need to be in the same range as your data. You haven't included the data you put in the histogram (diagnosis_test_codes), but unless its range is 1 to 5 then your XTick values being set to 1:5 will not work.
  2 Kommentare
bero
bero am 7 Mär. 2016
Sorry 'diagnosis_test_codes' is 'A'...
Adam
Adam am 7 Mär. 2016
Ok, so depending where you want the ticks to be the maths would vary, but for example you could do the following:
nTicks = numel( str );
xLim = get( gca, 'XLim' );
range = diff( xLim );
tickSpacing = range / nTicks;
xTicks = ( ( 1:nTicks ) - 0.5 ) * tickSpacing + xLim(1);
set( gca, 'XTick', xTicks )
set( gca, 'XTickLabel', str )
That would give you 5 ticks in centred locations. If you want A at the very start and E at the very end then you would need to change the maths accordingly.
I would not recommend using 'gca', by the way. I would always keep an explicit axes handle and use that, but since you used gca I have done so too.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by