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!