Matlab figure: axis re-labelling

In summary, MATLAB is plotting 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?You can use the command set(gca,'XTickLabel',{'.1','.2','0.3','.4','.5'}) to label the y-axis. This will make it so that the y-axis will show 0-1 with increments of .1.
  • #1
Moly
21
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
  • #2
This may be a bit out of leftfield, but have you tried

Code:
doc plot
 
  • #3
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 :)
 
  • #4
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;
 
  • #5
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
 
  • #6
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:
  • #7
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.
 
  • #8
Just tried it. Worked perfectly. Thanks a lot Chrisas.
 

1. How do I change the axis labels on my Matlab figure?

To change the axis labels on your Matlab figure, you can use the xlabel() and ylabel() functions. These functions allow you to specify the label text and other formatting options for the x-axis and y-axis, respectively.

2. Can I change the font and size of the axis labels on my figure?

Yes, you can change the font and size of the axis labels by using the FontName and FontSize parameters in the xlabel() and ylabel() functions. For example, xlabel('Time', 'FontName', 'Arial', 'FontSize', 12) will set the x-axis label to 'Time' with a font of Arial and size 12.

3. How do I rotate the axis labels on my figure?

To rotate the axis labels on your figure, you can use the xtickangle() and ytickangle() functions. These functions allow you to specify the rotation angle for the x-axis and y-axis labels, respectively. For example, xtickangle(45) will rotate the x-axis labels by 45 degrees.

4. Is it possible to add a title to my Matlab figure?

Yes, you can add a title to your Matlab figure by using the title() function. This function allows you to specify the title text and other formatting options, such as font and size.

5. How can I customize the appearance of the tick marks and gridlines on my figure?

You can customize the appearance of the tick marks and gridlines on your figure by using the set() function. This function allows you to change various properties of the axes, such as tick length, tick labels, and gridline style. You can also use the xlim() and ylim() functions to set the range of values for the x-axis and y-axis, respectively.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
995
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
744
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
Back
Top