2
$\begingroup$

I want to define my own ticks in a DateListPlot. It works quite well when I use the code in a Mathematica Notebook but breaks somehow when using it via Mathematica's script engine.

First of all the code:

Clear[fticks,plot];
fticks[min_, max_] := 
  Module[{nTicks = 9}, Table[{min + i (max - min)/nTicks,
     DateString[
      DateList[min + i (max - min)/nTicks], {"Month", ".", "Year"}]
     }, {i, 1, nTicks - 1}]];
plot = DateListPlot[
  FinancialData["IBM", "Jan. 1, 2004"],
  FrameTicks -> {fticks, Automatic, None, None}
  ]
Export[FileNameJoin[{$HomeDirectory, "test-plot.png"}], plot];

In my fticks function I generate some ticks depending on the minand maxvalues of the DateListPlot. This can't be completely wrong since the result works quite well.

Ticks are working when computed in a Mathematica Notebook file

But as soon as I try to calculate it via the Mathematica script engine, the custom ticks fail. (I've saved the code above in a file located at ~/test.m as Mathematica uses *.m files for its scripting engine).

/Applications/Mathematica.app/Contents/MacOS/MathKernel -script ~/test.m 

The result comes without any ticks but a red overlay because of an error. The question is now why is it not working? Interesting is also that while editing the test.m file there is also the possibility to run the package. I don't know why but this also works.

Ticks are not working when computed via the Mathematica script engine

I work in Mathematica 9.0.1.0 running on Mac OS X 10.8. This error also occurs in Mathematica 8 as I've tested.

Thanks in advance for your help.

$\endgroup$
3
  • $\begingroup$ I think you're missing a ; after the plot = ... command. $\endgroup$
    – Ajasja
    Apr 28, 2013 at 21:00
  • $\begingroup$ Mhm, I tried it with a ; and it still doesn't work. $\endgroup$
    – dotcs
    Apr 28, 2013 at 21:09
  • $\begingroup$ related. A comment from szabolcs suggest that the front end is needed to render custom ticks... $\endgroup$
    – cormullion
    Apr 29, 2013 at 7:36

1 Answer 1

1
$\begingroup$

I found a working solution. I played around writing the FrameTicks function as a pure function. The problem seems to be that the argument passed to the fticks function are DateList generated lists and not of type Real when using the script engine. Don't ask me why but they are of type Real when using the graphical frontend (does somebody know why?). This difference seems to cause the error when using the script engine instead of the graphical interface.

To calculate the corresponding Real value I used AbsoluteTiming in my solution fticks2.

It is interesting that the ticks are still not displayed when writing the function not as a pure function (see my attempt fticks3). I'm not sure why this happens. If you can comment on that please let me know.

Here is the solution for the interested people:

Clear[fticks,fticks2,fticks3,plot];

(* not working *)
fticks[min_,max_]:=Module[{nTicks=9},Table[{min+i (max-min)/nTicks,
DateString[DateList[min+i (max-min)/nTicks],{"Month",".","Year"}]
},{i,1,nTicks-1}]];

(* working *)
fticks2=Module[{nTicks=9,min=AbsoluteTime@#1,max=AbsoluteTime@#2},Table[{min+i (max-min)/nTicks,
DateString[DateList[min+i (max-min)/nTicks],{"Month",".","Year"}]
},{i,1,nTicks-1}]]&;

(* not working *)
fticks3[minList_,maxList_]:=Module[{nTicks=9,min=AbsoluteTime@minList,max=AbsoluteTime@maxList},Table[{min+i (max-min)/nTicks,
DateString[DateList[min+i (max-min)/nTicks],{"Month",".","Year"}]
},{i,1,nTicks-1}]];

plot=DateListPlot[
FinancialData["IBM","Jan. 1, 2004"],
FrameTicks->{fticks2,Automatic,None,None}
];
Export[FileNameJoin[{$HomeDirectory,"test.png"}],plot];

This generates the following output and therefore is perfect for my needs.

Output using pure function

So there are still some open questions but also a working solution. Nevertheless thanks to anyone who thought about the problem.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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