How do I insert special tick labels onto an axis?
39 views (last 30 days)
Show older comments
I have a vector of years and corresponding depths which I would like to plot in a bar chart. The years are labelled on the x axis. Say these years are 1800, 1820, 1840, 1860, ...., 1980. The first data point is at 1818. However, I would like to label one special year of 1788 on the x-axis before these automatic labels, with the y values being 0 for each x-axis point until 1818, which is the first one.
I have added the point (1788,0) to my data set which I am plotting, however I am having trouble adding the label at an appropriate distance relative to all the other labels.
This is what I have previously tried:
set(gca, 'xticklabel', {1788, 1800, 1820, 1840, 1860, 1880, 1900, 1920, 1940, 1960, 1980}) However, this gives me the label '1788' on the x-axis at the same spacing as all the other labels, but I want it to be positioned at the appropriate scale.
I have also tried With xticksat= [1788] but this doesn't work at all and gives me an error when I try to run the function.
Does anybody have any suggestions?
Help would be very much appreciated.
0 Comments
Accepted Answer
Star Strider
on 29 Jan 2015
It may be necessary to define the 'XTick' locations as well.
See if this does what you want:
x = [1788, 1800, 1820, 1840, 1860, 1880, 1900, 1920, 1940, 1960, 1980];
y = randi(25, 11, 1); % Created Data
figure(1)
bar(x, y)
set(gca, 'XTick', x, 'xticklabel', {1788, 1800, 1820, 1840, 1860, 1880, 1900, 1920, 1940, 1960, 1980})
2 Comments
Star Strider
on 29 Jan 2015
My pleasure!
It didn’t do that when I ran the same code in R2014b, so there may be version differences. Another option is to use the 'FontSize' option to reduce the size of the tick labels. In R2014b it is also possible to rotate them, although in earlier versions you have to use the text function if you want to rotate the tick labels.
More Answers (0)
See Also
Categories
Find more on Labels and Annotations 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!