How to change matlab x-axis on a subplot
10 views (last 30 days)
Show older comments
This is a extract of my m-file. The code below generates a plot, but the x-axis just wont adjust how I want it. No matter what I try. What's wrong? (I want to replace enumeration, which is from 1 to 20732 with the actual time which should be from 0 to 41.4640 seconds)
And, yes, I'd like to have it in a subplot... also tried to add second x-axis. Sounds good, doesn't work.
figure('Name','Respiration at rest');
fs=500;
stot=(1/fs*20732);
saxis = linspace(1,stot,20732)';
ax1=subplot(2,1,1), plot(ax1,saxis,data_5(:,1))
axis([ax1 ax1],[1 stot -2 4])
xlim(ax1,[1 stot]);
0 Comments
Answers (2)
Cam Salzberger
on 23 Oct 2017
Hello Filip,
When I run the code, the axes is shrunk to the size of a subplot. If you are trying to create a second subplot, you can use something like:
ax2 = subplot(2, 1, 2);
Using axis([ax1 ax1], ...) doesn't do anything different than axis(ax1, ...). It just changes the x and y limits for the same axes (ax1) twice.
It seems like you would like the data to be plotted against the "saxis" values (1 to 20732), but you want the tick marks to show up as different values. You can do this by setting the 'XTick" and 'XTickLabel' properties of the axes.
Remember that the underlying data units will still remain the same, so I'd remove the call to "xlim" and "axis", since you're limiting the window to not display the full range of data. Unless that's what you want, of course.
-Cam
0 Comments
Filip Krähenbühl
on 24 Oct 2017
2 Comments
Cam Salzberger
on 24 Oct 2017
Edited: Cam Salzberger
on 25 Oct 2017
Hello Filip,
Typically, if responding to an answer, we post a comment under it, rather than a new answer.
I ran the code on some random data:
data_5 = sin(linspace(0, 40*pi, 20000)).';
and saw this for the subplot:

This matched what I saw for the x-axis when run with your second bit of code:

However, when I ran your commented-out bit of code, that uses x_data_5 as the x-values, rather than saxis, that's when I saw your "differently-scaled" x-axis:

Just be careful which data you are plotting.
-Cam
See Also
Categories
Find more on Graphics Performance 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!
