Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
I think you're missing a ; after the plot = ... command. –  Ajasja Apr 28 '13 at 21:00
    
Mhm, I tried it with a ; and it still doesn't work. –  chromate Apr 28 '13 at 21:09
    
related. A comment from szabolcs suggest that the front end is needed to render custom ticks... –  cormullion Apr 29 '13 at 7:36
add comment

1 Answer 1

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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