Change x axis labeling independent of x values?

19,679

You could customize the tick marks using the XTick and XTickLabel axis properties.

Example:

x = 1:7;
y = rand(size(x));
plot(x,y)
set(gca, 'XTickLabel',{'Mon','Tue','Wed','Thu','Fri','Sat','Sun'})

screenshot

Share:
19,679
stefan.at.wpf
Author by

stefan.at.wpf

Updated on June 17, 2022

Comments

  • stefan.at.wpf
    stefan.at.wpf almost 2 years

    Imagine I am plotting e.g. this:

    plot(1:1500,1:1500)
    

    This will look like the image below, with the x axis starting at 0 and going up to 1500.

    Now I don't want to have that labeling, but instead the x axis labeling shall start with e.g. 1 and then end at 151 (increase by 1/10 for every point on the x axis, additionally an offset of 1).

    I just want to change the labeling of the x axis, I don't want to change the x input vector to the plot function nor do I want to plot other points. I just want the x labeling to start at a different offset and increase in another step size, independent of the x values passed to the plot function.

    Is that possible? How? It would make some things easier for me. Thanks for any hint!

    enter image description here

  • stefan.at.wpf
    stefan.at.wpf almost 12 years
    Thank you, the XTickLabel property seems to be useful to me, as I could generate all the values for it. I am still wondering if it's possible do directly set something like xAxisOffset=1, xAxisStepsize=1/10, so that I don't have to build a full vector. But if I don't understand the properties wrong, there is nothing like that ):
  • Amro
    Amro almost 12 years
    @stean.at.wpf: not explicitly. XTick allows to specify where MATLAB shows tick marks. XTickLabel lets you customize the label at those locations. Note that once you manually set them, you no longer get the auto mode (try plot(1:100) then resize the figure and see how it adjust the tick marks according to the width)
  • Amro
    Amro almost 12 years
    @Amro: maybe I could change my example. What are the exact values you want on your x-axis. Is it 1:1/10:151 ? Also how frequently show you show them, N-values equally spaced?
  • stefan.at.wpf
    stefan.at.wpf almost 12 years
    Amro, for me it works quite well using XTickLabel, so I selected your answer as the correct one :-) (I just put seomthing like 1:1/10:151 as input to XTickLabel and leave the frequency of showing the label to Matlab, that's quite ok).