Matlab figure: axis re-labelling

  • Context: MATLAB 
  • Thread starter Thread starter Moly
  • Start date Start date
  • Tags Tags
    Axis Figure Matlab
Click For Summary

Discussion Overview

The discussion revolves around how to re-label the x-axis in MATLAB plots to display actual time values instead of index numbers. Participants explore various methods for achieving this, focusing on the integration of time steps and the corresponding adjustments needed for tick marks and labels on the plot.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks to replace the default index-based x-axis labels with actual time values, specifically wanting to display a range from 0 to 5 instead of 0 to 1200.
  • Another participant suggests using the documentation for the plot function as a potential resource.
  • A participant shares their attempt to set custom tick marks and labels, indicating a need for dynamic adjustments based on changing parameters.
  • There is a suggestion to use the command plot(t, y) to plot y against time, which would automatically label the plot correctly.
  • Some participants emphasize that using plot(y) results in plotting against index numbers rather than time, which may lead to misinterpretation of the data.
  • One participant provides a detailed method for manually setting tick marks and labels based on the desired scale, including calculations for tick spacing.
  • A later reply confirms that the proposed method for manual labeling worked successfully.

Areas of Agreement / Disagreement

Participants generally agree on the need to adjust the x-axis labels, but there are differing opinions on the best method to achieve this. Some advocate for using the time variable directly in the plot function, while others prefer to manually set the tick marks and labels.

Contextual Notes

Participants express uncertainty about the best approach to dynamically adjust labels based on varying time parameters, indicating a reliance on manual calculations for specific cases.

Who May Find This Useful

This discussion may be useful for MATLAB users looking to customize plot axes, particularly in contexts involving time series data or integration results.

Moly
Messages
20
Reaction score
0
Hi all.

i am integrating in MATLAB and plotting the results in a y-verically and t-horizontally. Matlab automatically plots y versus the time step which is great. but what i need to do is re-label the t-axis so the actual time appears instead of the time step- so instead of from 0 to 1200, it would be 0-5. Any idea how to do that?
 
Physics news on Phys.org
This may be a bit out of leftfield, but have you tried

Code:
doc plot
 
I did... and tried this
set(gca,'XTick',-pi:pi/2:pi)
set(gca,'XTickLabel',{'-pi','-pi/2','0','pi/2','pi'})

but could not figure out a way for the conversion... here are more specifics...
i am running the integration for t=1 and time step dt=.0063 which makes the total number of n around 160. so when the figure is plotted it is plotted from 0 to 160 (the n's) but instead i want it to show from 0 to 1 with increments of .1

for these specific parameters i did the following and it is now working out and not sure why:
set(gca,'XTick',0:.1:1)
set(gca,'XTickLabel',{'.1','.2','0.3','.4','.5'})

also, i often chance t, so is there a way to have MATLAB calculate it for me so i don't have to hardwire with every change in t.

i hope this clarifies my confusion :)
 
What command are you using to plot it?

plot(y);
will plot it by index number.

plot(t, y);
will plot y versus t.

If you want t to be 0 to 1 in increments of 0.0063, then
t=0:0.0063:1;
 
this is what i am using plot(y);
and it is plotting y vs. t. the plotting itself is not the problem...
i simply want the t-axis to be re-labeled... instead of the tick marks showing from 0 to 160, i want the change to be only in the tick marks and want it to show from 0 to 1 with increments of 0.1. as i said, the graph is fine, i just want the numbers that show on the figure to be different
 
But did you try plot(t, y)? That will label the plot automatically.

Using just plot(y) is not plotting it versus time, it is plotting it versus index number, your 1 to 160 that you are seeing. As long as the data you are plotting is evenly spaced, using just plot(y) is ok. However, if you have a data set like this:

t=[ 0 1 2 5 20]
y=[ 1 2 3 4 5]

will produce very different plots using
plot(y) instead of plot(t,y)

In this case the plot(y) will show the wrong picture. plot(t,y) will show the right one.

But if really insist on doing the labels manually, then use
set(gca,'XTick',0:.1:1)
set(gca,'XTickLabel',[0:.1:1])
 
Last edited:
Ok, I had a chance to check this out with MATLAB at work and I see where the problem is. Here is what you can do if you want to label the axis yourself. If you use plot(y), then as I said before, MATLAB is plotting y against the index number. So for the case that you gave in your first post, let's say that MATLAB is plotting 0 to 1200. You need to tell MATLAB where to put tick marks USING THIS SCALE. So if you want 10 equal spaces, then this is a delta of 1200/10 = 120. So you need to tell MATLAB to put a tick mark at 0, 120, 240, etc by

set(gca, 'XTick', 0:120:1200);

Now you need to setup the labels you want to put there. If you want 0 to 5, then with 10 spaces, you want the labels every 5/10, or 0.5. Tell MATLAB to make labels of 0, 0.5, 1.0, etc by

set(gca, 'XTickLabel', [ 0:0.5:5 ] );


To do this automatically, use
xlim=get(gca, 'XLim');
to put the actual plot limits in the variable xlim, xlim(1) is the lower bound, xlim(2) is the upper bound. In our example this would be 0 and 1200.

Do the math using xlim to get the tick spacing you want. Then use the above set commands with your calculated new tick mark positions.
 
Just tried it. Worked perfectly. Thanks a lot Chrisas.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K