Changing numbers near X-axis when plotting

조회 수: 6 (최근 30일)
Andrey
Andrey 2014년 6월 14일
편집: Star Strider 2014년 6월 14일
Hi, I import some computation results (single dimension arrays) to Matlab and trying to plot them. However I don't want X-axis labels to be shown as array indexes, I need them to be shown as [Array index]*some_number.
Those computations are are obtained from a difference scheme (uniform grid), for example:
I have X-segment [0,10] and 101 points in my scheme (covering the whole segment with a step 0.1), so as a result I get an array res_1,res_2,...res_99,res_101.
I read it via dlmread(res) and plot, thus getting X-axis labels: 1,2,...,99,101.
But I'd like to have X-axis labels to be shown as: 0,0.1,...,9.9,10.
It seems I should include multiplication of each number on X-axis when drawing X-axis labels (multiplying by step of the grid, 0.1 in current example), but I can't figure how to do this.
Thanks.

채택된 답변

Star Strider
Star Strider 2014년 6월 14일
편집: Star Strider 2014년 6월 14일
This works!
For a single plot:
figure(2)
plot(V)
xtk = get(gca, 'XTick') % Get 'XTick' values
xtklbl = xtk * 0.1; % Multiply by ‘0.1’
set(gca, 'XTick', xtk, 'XTickLabel',xtklbl) % Replace 'XTickLabel' values
grid
xlabel('X-Axis Indices x 0.1')
axis([0 100 ylim])
For subplots:
V = randi(25, 1, 101); % Create data
figure(1)
subplot(2,1,1) % Plot ‘V’ using indices
plot(V)
grid
xlabel('X-Axis Indices')
axis([0 100 ylim])
hsp2 = subplot(2,1,2) % Plot ‘V’ using indices, then change them
plot(V)
xtk = get(hsp2, 'XTick') % Get 'XTick' values
xtklbl = xtk * 0.1; % Multiply by ‘0.1’
set(hsp2, 'XTick', xtk, 'XTickLabel',xtklbl) % Replace 'XTickLabel' values
grid
xlabel('X-Axis Indices x 0.1')
axis([0 100 ylim])

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by