3
$\begingroup$

We have the following data

data = {{2000, 30, 4}, {2001, 15, 4}, {2002, 5, 5}, {2003, 27, 4}, {2004, 11, 4}, {2005, 1, 5}, 
        {2006, 23, 4}, {2007, 8, 4}, {2008, 27, 4}, {2009, 19, 4}, {2010, 4, 4}, {2011, 24, 4},
        {2012, 15, 4}, {2013, 5, 5}, {2014, 20, 4}, {2015, 12, 4}, {2016, 1, 5}, {2017, 16, 4},
        {2018, 8, 4}, {2019, 28, 4}, {2020, 19, 4}}

where the first column indicates the year, the second column the date, and the third column the month (April or May).

I want to create a custom list plot with a horizontal axis the year and the vertical axis starting from April 1 up to May 31. Any suggestions?

(By the way, the dates correspond to the Orthodox Easter)

EDIT

Using @kglr's approach

enter image description here

EDIT 2

Using @kglr's Update 3 we get

enter image description here

$\endgroup$
4
$\begingroup$
data2 = DateObject[{#[[1]], #[[3]], #[[2]]}] & /@ data;

values = #["ISOYearDay"] -  DateObject[{#["Year"], 4, 1}]["ISOYearDay"] & /@ data2;

labels = DateString[#, {"Month", "/", "Day"}] & /@ data2;

DateListPlot[values -> labels, {data[[1, 1]]},
  Mesh -> Full, ImageSize -> Large, FrameTicks -> {{None, None}, {Automatic, Automatic}}]

enter image description here

Update: Add vertical ticks from April 1st to May 31st:

DateListPlot[values, {data[[1, 1]]}, Mesh -> Full, ImageSize -> Large, 
 FrameTicks -> {{Transpose[{values, labels}], None}, {Automatic, Automatic}}]

enter image description here

To get less cluttered vertical ticks use custom ticks:

ClearAll[vTicks]
vTicks[nt_: 10] := Module[{vf = #["ISOYearDay"] - 
      DateObject[{#["Year"], 4, 1}]["ISOYearDay"] &[DateObject@#] &, 
    from = AbsoluteTime@{2000, 4, 1}, to = AbsoluteTime@{2000, 5, 31}}, 
   MapAt[vf, #, {All, 1}] & @ Select[from <= #[[1]] <= to &][
    System`DateListPlotDump`DateTicks[ {from, to}, nt, {"Month", "/", "Day"}]]];

DateListPlot[values, {data[[1, 1]]}, Mesh -> Full, ImageSize -> Large, 
 FrameTicks -> {{vTicks[], None}, {Automatic, Automatic}},
 GridLines -> {None, vTicks[10][[All, 1]]}]

enter image description here

Update 2: Loss off horizontal minor ticks mention in OP's update is likely to be version/os related.

Both in version 11.3.0 (Windows 10 /64 bit) and in version 12.0 (Wolfram Cloud) we do see the minor ticks in the date axis:

randomvalues = RandomInteger[{1, 61}, 201];
DateListPlot[randomvalues, {1900}, 
 FrameTicks -> {{vTicks[], Automatic}, {Automatic, Automatic}}]

enter image description here

Update 3: Custom ticks with major ticks every 50 years and minor ticks every 5 years:

dTicks = Join[{AbsoluteTime[{#}], ToString@#, {.02, 0}} & /@ Range[1900, 2100, 50], 
  {AbsoluteTime[{#}], "", {.01, 0}} & /@ Range[1900, 2100, 5]];

SeedRandom[1]
randomvalues = RandomInteger[{1, 61}, 201];

DateListPlot[randomvalues, {1900}, ImageSize -> Large, Mesh -> All, 
 FrameTicks -> {{vTicks[], Thread[{vTicks[][[All, 1]], ""}]}, {dTicks, Automatic}},
 GridLines -> {None, vTicks[][[All, 1]]}]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ A good start! But if the values are much more, let's say from 1900 to 2100, then all the labels are almost on top of each other. Can we delete the labels and have a vertical axis starting from April 1, to May 31? $\endgroup$ – Vaggelis_Z Mar 14 at 16:55
  • $\begingroup$ When I apply the new code to my actual data I have no vertical ticks. It seems that your approach works only for the test data, while it should work for any given range of years, let's say from 1900 to 2100. $\endgroup$ – Vaggelis_Z Mar 14 at 17:57
  • $\begingroup$ See my edit. The vertical ticks are fine but now I don't have small ticks at the horizontal axis. $\endgroup$ – Vaggelis_Z Mar 14 at 18:08
  • $\begingroup$ I have version 12.0.0 for Microsoft Windows (64-bit) and I cannot reproduce the minor ticks. $\endgroup$ – Vaggelis_Z Mar 14 at 20:12
  • $\begingroup$ See my new edit. Now the ticks in the lower horizontal axis seem correct (even though the major and minor ticks are the same) but the minor ticks are missing in the upper horizontal axis. $\endgroup$ – Vaggelis_Z Mar 15 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.