How to change matlab x-axis on a subplot
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Cam Salzberger
el 23 de Oct. de 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 comentarios
Filip Krähenbühl
el 24 de Oct. de 2017
2 comentarios
Cam Salzberger
el 24 de Oct. de 2017
Editada: Cam Salzberger
el 25 de Oct. de 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
Ver también
Categorías
Más información sobre Graphics Performance en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

