MATLAB Answers

Colorbar axis scaling

3 views (last 30 days)
S K
S K on 18 May 2011
Answered: Chad Greene on 10 Nov 2014
I want to print the dates from 1970 to 2010 as ticks of color bar. I have written following code and it shows the date from 1970 to 1978 only. Also I want all the color spectrum to appear in the colorbar but it is only showing the white color.
x=1:41;
clc;
t=x;
clf;
for i=1:41
plot(t(i),x(i),'--rs','MarkerSize',20,'Color',[i/41 (i*0.5)/41 (i*.1)/41]);
hold on;
end
Cbarlabels=['1970';'1971';'1972';'1973';'1974';'1975';'1976';'1977';'1978';'1979';'1980';'1981';'1982';'1983';'1984';'1985';'1986';'1987';'1988';'1989';'1990';'1991';'1992';'1993';'1994';'1995';'1996';'1997';'1998';'1999';'2000';'2001';'2002';'2003';'2004';'2005';'2006';'2007';'2008';'2009';'2010'];
colormap(jet(41));
h=colorbar('YTickLabel',Cbarlabels);
set(h,'YLim',[1970 2010]);

  0 Comments

Sign in to comment.

Answers (2)

Doug Hull
Doug Hull on 18 May 2011
The key is to have the right number of elements in yTick:
x=1:41;
clc;
t=x;
clf;
for i=1:41
plot(t(i),x(i),'--rs','MarkerSize',20,'Color',[i/41 (i*0.5)/41 (i*.1)/41]);
hold on;
end
Cbarlabels=['1970';'1971';'1972';'1973';'1974';'1975';'1976';'1977';'1978';'1979';'1980';'1981';'1982';'1983';'1984';'1985';'1986';'1987';'1988';'1989';'1990';'1991';'1992';'1993';'1994';'1995';'1996';'1997';'1998';'1999';'2000';'2001';'2002';'2003';'2004';'2005';'2006';'2007';'2008';'2009';'2010'];
colormap(jet);
h=colorbar;
set(h,'YTick',linspace(1,64,41))
set(h,'YTickLabel',Cbarlabels);

  0 Comments

Sign in to comment.


Chad Greene
Chad Greene on 10 Nov 2014
cbdate can do this easily.

  0 Comments

Sign in to comment.

Sign in to answer this question.

Tags