Changing numbers near X-axis when plotting
5 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Star Strider
on 14 Jun 2014
Edited: Star Strider
on 14 Jun 2014
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 Comments
More Answers (0)
See Also
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!