Axis labeling trickery [message #85781] |
Fri, 06 September 2013 16:18  |
Paul Levine
Messages: 29 Registered: February 2008
|
Junior Member |
|
|
I am plotting some irregularly-spaced observations over 10 years, and I
would like my x-axis to be labeled with exactly those 10 years, with a
tick mark at the beginning of every year.
When I use XTickFormat='Label_Date' and XTicks = 10, the tick marks are
in the right place (January 1) but of course I then have 11 tick marks,
including a label with the year after my 10-year interval. My current
workaround is to draw the labels that I want to see with xyouts (or
cgText), but this leaves me with other problems, so I am hoping there
is a non-workaround way to make the y-axis labels as I want from the
get go.
What follows are two examples, the first of which draws the problematic
labels, the second uses my workaround. The first two lines of each
example generate some random data that simulates the data I'm working
with. Both examples use coyote graphics procedures, but should work
the same with plot for cgPlot and xyouts for cgText
Example 1:
time = timegen(120,start=julday(1,1,2003), units='M', step_size=1)
data = 20*randomu(seed,n_elements(time))-10
void = Label_Date(Date_Format='%Y')
cgPlot, time, data, xrange=[julday(1,1,2003),julday(12,31,2012)],
XTickFormat='Label_Date', XTicks = 10
In this example, my y-axis labels include 2003 through 2013, when I
would like for it to stop at 2012
Example 2:
time = timegen(120,start=julday(1,1,2003), units='M', step_size=1)
data = 20*randomu(seed,n_elements(time))-10
void = Label_Date(Date_Format=' ')
years = indgen(10)+2003
cgPlot, time, data, xrange=[julday(1,1,2003),julday(12,31,2012)],
XTickFormat='Label_Date', XTicks = 10
xoff = 2 ; sets how far to the right of the tick mark the labels should
be drawn
yoff = 1 ; sets how far below the x-axis the labels should be drawn
cgtext, julday(1+xoff,1,years), min(data)-yoff, strtrim(years,1)
This draws exactly the labels I want (2003-2012), and has the added
benefit of letting me scoot the labels over a bit to the right so that
they are centered under the year interval, rather than centered at the
January 1 tick mark. But it introduces the problem of needing to set
the values for xoff, which will depend on the size of the window, and
yoff, which will depend on the range of the daya (in my example, the
data ranges from -10 to 10, so a yoff = 1 works, but if the data range
was from -1 to 1 or was from -1000 to 1000 then it would not work so
well). The xoff problem can be worked around by explicitly setting the
window size, but the yoff problem is data-dependent.
Is there a better way of doing this that does not depend on the data range?
Thanks in advance!
|
|
|
Re: Axis labeling trickery [message #85783 is a reply to message #85781] |
Sat, 07 September 2013 11:50   |
Phillip Bitzer
Messages: 223 Registered: June 2006
|
Senior Member |
|
|
On Friday, September 6, 2013 6:18:06 PM UTC-5, Paul Levine wrote:
> Example 1:
> time = timegen(120,start=julday(1,1,2003), units='M', step_size=1)
>
> data = 20*randomu(seed,n_elements(time))-10
>
> void = Label_Date(Date_Format='%Y')
>
> cgPlot, time, data, xrange=[julday(1,1,2003),julday(12,31,2012)],
>
> XTickFormat='Label_Date', XTicks = 10
>
Looks like this example gives me something different - 2003-2012 is the range of the labels. No 2013, but 2004 and 2008 are duplicated and 2005 is missing. XTicks=9 gives the correct labels, though (ten year span => 9 tick intervals and 10 tickmarks).
Just for reference:
IDL> print, !VERSION
{ x86_64 darwin unix Mac OS X 8.2.2 Jan 23 2013 64 64}
|
|
|
Re: Axis labeling trickery [message #85784 is a reply to message #85783] |
Sat, 07 September 2013 12:47   |
Paul Levine
Messages: 29 Registered: February 2008
|
Junior Member |
|
|
On 2013-09-07 18:50:22 +0000, Phillip Bitzer said:
> On Friday, September 6, 2013 6:18:06 PM UTC-5, Paul Levine wrote:
>
>> Example 1:
>> time = timegen(120,start=julday(1,1,2003), units='M', step_size=1)
>>
>> data = 20*randomu(seed,n_elements(time))-10
>>
>> void = Label_Date(Date_Format='%Y')
>>
>> cgPlot, time, data, xrange=[julday(1,1,2003),julday(12,31,2012)],
>>
>> XTickFormat='Label_Date', XTicks = 10
>>
>
> Looks like this example gives me something different - 2003-2012 is the
> range of the labels. No 2013, but 2004 and 2008 are duplicated and 2005
> is missing. XTicks=9 gives the correct labels, though (ten year span =>
> 9 tick intervals and 10 tickmarks).
>
> Just for reference:
> IDL> print, !VERSION
> { x86_64 darwin unix Mac OS X 8.2.2 Jan 23 2013 64 64}
I mixed up a line in my example, so the results (same as what you got)
are different than what I described. What I should have written was to
have the end of xrange be julday(1,1,2013) rather than
julday(12,31,2012), which would have produced what I described, so:
time = timegen(120,start=julday(1,1,2003), units='M', step_size=1)
data = 20*randomu(seed,n_elements(time))-10
void = Label_Date(Date_Format='%Y')
cgPlot, time, data, xrange=[julday(1,1,2003),julday(1,1,2013)],
XTickFormat='Label_Date', XTicks = 10
Either way, though, the problem with setting XTicks = 9 is that while
the year labels print what I want, the tick marks themselves are no
longer spaced one year apart on January 1 of each year. Running the
same code above but with Date_Format='%M %D %Y') and XTicks=9
illustrates why I need 10 rather than 9 tick intervals.
|
|
|
Re: Axis labeling trickery [message #85785 is a reply to message #85784] |
Sat, 07 September 2013 12:56   |
Fabzi
Messages: 305 Registered: July 2010
|
Senior Member |
|
|
Hi,
I am not sure to understand the problem but I know that label_date can
be very annoying sometimes ...
What about doing the same, but without label_date?
time = timegen(120,start=julday(1,1,2003), units='M', step_size=1)
data = 20*randomu(seed,n_elements(time))-10
; Without "label_date"
x_name = STRING(INDGEN(11) + 2003, FORMAT='(I04)')
x_locs = timegen(11,start=julday(1,1,2003), units='Y', step_size=1)
cgPlot, time, data, xrange=[julday(1,1,2003),julday(1,1,2013)], $
XTICKV=x_locs, XTicks=10, XTICKNAME=x_name
Cheers,
Fab
On 09/07/2013 09:47 PM, Paul Levine wrote:
>
> Either way, though, the problem with setting XTicks = 9 is that while
> the year labels print what I want, the tick marks themselves are no
> longer spaced one year apart on January 1 of each year. Running the
> same code above but with Date_Format='%M %D %Y') and XTicks=9
> illustrates why I need 10 rather than 9 tick intervals.
|
|
|
Re: Axis labeling trickery [message #85795 is a reply to message #85785] |
Mon, 09 September 2013 14:59   |
Paul Levine
Messages: 29 Registered: February 2008
|
Junior Member |
|
|
On 2013-09-07 19:56:40 +0000, Fabien said:
> Hi,
>
> I am not sure to understand the problem but I know that label_date can
> be very annoying sometimes ...
>
> What about doing the same, but without label_date?
>
> time = timegen(120,start=julday(1,1,2003), units='M', step_size=1)
> data = 20*randomu(seed,n_elements(time))-10
> ; Without "label_date"
> x_name = STRING(INDGEN(11) + 2003, FORMAT='(I04)')
> x_locs = timegen(11,start=julday(1,1,2003), units='Y', step_size=1)
> cgPlot, time, data, xrange=[julday(1,1,2003),julday(1,1,2013)], $
> XTICKV=x_locs, XTicks=10, XTICKNAME=x_name
>
> Cheers,
>
> Fab
Thank you for the suggestion. But that gives the same result as my
first example
time = timegen(120,start=julday(1,1,2003), units='M', step_size=1)
data = 20*randomu(seed,n_elements(time))-10
void = Label_Date(Date_Format='%Y')
cgPlot, time, data, xrange=[julday(1,1,2003),julday(1,1,2013)],
XTickFormat='Label_Date', XTicks = 10
in which there is a label at the "end" of the x-axis for the 11th year.
I guess what I am wanting to do is to have 10 tick intervals, but only
10 labels, with the 11th tick mark remaining unlabeled. If it can't be
done from within the plot or axis routines, I will just have to use
xyouts (or cgText).
|
|
|
Re: Axis labeling trickery [message #85797 is a reply to message #85795] |
Mon, 09 September 2013 15:33   |
suicidaleggroll
Messages: 14 Registered: September 2013
|
Junior Member |
|
|
On Monday, September 9, 2013 3:59:10 PM UTC-6, Paul Levine wrote:
> On 2013-09-07 19:56:40 +0000, Fabien said:
>
>
>
>> Hi,
>
>>
>
>> I am not sure to understand the problem but I know that label_date can
>
>> be very annoying sometimes ...
>
>>
>
>> What about doing the same, but without label_date?
>
>>
>
>> time = timegen(120,start=julday(1,1,2003), units='M', step_size=1)
>
>> data = 20*randomu(seed,n_elements(time))-10
>
>> ; Without "label_date"
>
>> x_name = STRING(INDGEN(11) + 2003, FORMAT='(I04)')
>
>> x_locs = timegen(11,start=julday(1,1,2003), units='Y', step_size=1)
>
>> cgPlot, time, data, xrange=[julday(1,1,2003),julday(1,1,2013)], $
>
>> XTICKV=x_locs, XTicks=10, XTICKNAME=x_name
>
>>
>
>> Cheers,
>
>>
>
>> Fab
>
>
>
> Thank you for the suggestion. But that gives the same result as my
>
> first example
>
>
>
> time = timegen(120,start=julday(1,1,2003), units='M', step_size=1)
>
> data = 20*randomu(seed,n_elements(time))-10
>
> void = Label_Date(Date_Format='%Y')
>
> cgPlot, time, data, xrange=[julday(1,1,2003),julday(1,1,2013)],
>
> XTickFormat='Label_Date', XTicks = 10
>
>
>
> in which there is a label at the "end" of the x-axis for the 11th year.
>
> I guess what I am wanting to do is to have 10 tick intervals, but only
>
> 10 labels, with the 11th tick mark remaining unlabeled. If it can't be
>
> done from within the plot or axis routines, I will just have to use
>
> xyouts (or cgText).
Why not just use xtickv and xtickname to put the labels you want, where you want, called what you want.
years=indgen(10)+2003
xtickv=julday(1,1,years,0)
xtickname=string(years,format='(i4.4)')
plot, time, data, xrange=julday(1,1,[2003,2013]), xtickv=xtickv, xtickname=xtickname, xticks=n_elements(xtickv)-1
or similar
|
|
|
Re: Axis labeling trickery [message #85799 is a reply to message #85797] |
Mon, 09 September 2013 15:51  |
Paul Levine
Messages: 29 Registered: February 2008
|
Junior Member |
|
|
On 2013-09-09 22:33:10 +0000, suicidaleggroll@gmail.com said:
>
> Why not just use xtickv and xtickname to put the labels you want, where
> you want, called what you want.
>
> years=indgen(10)+2003
> xtickv=julday(1,1,years,0)
> xtickname=string(years,format='(i4.4)')
> plot, time, data, xrange=julday(1,1,[2003,2013]), xtickv=xtickv,
> xtickname=xtickname, xticks=n_elements(xtickv)-1
Yes! That was what I was looking for, it worked perfectly
Many thanks!!
|
|
|