MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Two Axis DateListPlot

  • To: mathgroup at smc.vnet.net
  • Subject: [mg103312] Two Axis DateListPlot
  • From: Armand Tamzarian <mike.honeychurch at gmail.com>
  • Date: Mon, 14 Sep 2009 07:13:05 -0400 (EDT)

Further to my post, here is a two axis DateListPlot. This is based on
the TwoAXisListPlot code that floats around this group, which I
believe originated from WRI tech support. The use of Frame->True
breaks AbsoluteOptions (Ticks.FrameTicks) for many types of plots so
by setting Axes->True, Frame->False we can get this to work and then
set Frame->True after the relevant stuff has been extracted.


Clear[TwoAxisDateListPlot];

TwoAxisDateListPlot[f_List, g_List, opts : OptionsPattern[]] :=
 Module[{p1, p2, fm, fM, gm, gM, old, new, newg},

  p1 = DateListPlot[f, Axes -> True, Frame -> False,
    PlotRange -> Automatic];
  p2 = DateListPlot[g, Axes -> True, Frame -> False,
    PlotRange -> Automatic];

  {fm, fM} = AbsoluteOptions[p1, PlotRange][[1, 2, 2]];
  {gm, gM} = AbsoluteOptions[p2, PlotRange][[1, 2, 2]];

  old = AbsoluteOptions[p2, Ticks][[1, 2, 2]];

  new = Flatten[{Rescale[First[#1], {gm, gM}, {fm, fM}], Rest[#1]},
      1] & /@ old;

  newg = {#[[1]], Rescale[#[[2]], {gm, gM}, {fm, fM}]} & /@ g;

  DateListPlot[{f, newg},
   Axes -> False,
   Frame -> True,
   FrameTicks -> {Automatic, Automatic, None, new},
   PlotRange -> {fm, fM},
   opts
   ]
  ]


a test:

f = Table[{{2009, i, 0}, RandomReal[{-2, 2}]}, {i, 10}];
g = Table[{{2009, i, 0}, RandomReal[{-1000, 1000}]}, {i, 10}];

TwoAxisDateListPlot[f, g, Joined -> True]

Mike

On Sep 11, 6:57 pm, Armand Tamzarian <mike.honeychu... at gmail.com>
wrote:
 Show[pricesPlot]
>
> This is easier ( to read at least):
>
> spotPrices = First@Import["http://www.eia.doe.gov/emeu/international/
> Crude1.xls","XLS"];
>
> spotBrent = Cases[spotPrices[[All, {1, 3}]], {_List, x_?NumericQ}];
> spotWTI = Cases[spotPrices[[All, {1, 4}]], {_List, x_?NumericQ}];
>
> goldPrices = Import["http://www.lbma.org.uk/?area=stats&page=gold/
> 2009dailygold","Data"];
>
> goldPrices = Cases[goldPrices, {x_String, y__?NumericQ} :> {DateList
> [x], y}, \[Infinity]];
>
> >From here I don't exactly know how you want to make your plot (perhaps
>
> scale the gold price?) or which price to choose but this is a simple
> plot:
>
> DateListPlot[{spotPriceWTI, goldPrices[[All, {1, 2}]]}, Joined ->
> True, PlotRange -> All]
>
> ------
>
> If you wanted to only choose days in which you have data for both WTI
> and gold you can use Intersection with a SameTest to create a subset
> of the WTI data (or of course use Part or Take and make the choice
> manually). General, but slow, case:
>
> WTIsubset1=Quiet@Intersection[spotPriceWTI, goldPrices[[All, 1]],
> SameTest -> (#1[[1]] == #2[[{1, 2, 3}]] &)]
>
> Note that the {1,2,3} is necessary because the length of the date
> lists differ for the oil and gold.
>
> However if you only want 2009 data, which you would in this case:
>
> WTIsubset2=Cases[spotPriceWTI, {{2009, __}, __}]
>
> Ideally you'd want to have a two axis plot here but this has always
> been problematic and breaks whenever I try to implement it with
> DateListPlot. Some readers may have a working DateListPlot two axis
> plot solution (?)
>
> Mike



  • Prev by Date: Re: Re: Mapping to a specific column or row in a matrix
  • Next by Date: Re: Re: Unexpected Characters Appearing in
  • Previous by thread: Re: Integrate on StudentTDistribution
  • Next by thread: Re: Re: Unexpected Characters Appearing in