change the values of X axis in histogram to codes
3 次查看(过去 30 天)
显示 更早的评论
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...
0 个评论
回答(1 个)
Adam
2016-3-7
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 个评论
Adam
2016-3-7
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Histograms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!