Message Boards Message Boards

2
|
50571 Views
|
11 Replies
|
17 Total Likes
View groups...
Share
Share this post:

Overplotting or plotting with two different y axes

Posted 10 years ago
Is there a way to plot two sets of data on the same x axis if they have different y axis ranges?  The final product would be a single frame with different ticks and lables on the left and right axes.  I have tried Show[] and Inset[] to no avail.  Show[] forces both data sets to be on the y-axis specified by the second data set.  I cannot find a way to make Inset work at all.


Thank you very much
POSTED BY: Wei-Li Diana Ma
11 Replies

Hi David,

Thanks for your help !

Cheers,........Jos

POSTED BY: Jos Klaps

Just for fun I thought I would show how one would do the Sandu Ursu example in Presentations. I have to admit this did take me awhile because I don't do much downloading of curated data nor do I make many data plots. Presentations works with graphics primitives and it is much easier to combine them and also to manipulate the primitives. For example, here I decided to separate the two sets of data by moving the Windspeed up by 10 units using a form of the Translate routine. Presentations also has CustomTicks and CustomDateTicks routines so I could obtain a more precise labeling of the dates with the endpoints labeled and I could adjust the right and left ticks to match the range of the two data sets. Also it was easy to label the two data sets directly with Text statements and eliminate a legend.

First we have to import the data and extract the relevant parts.

{T, W} = WeatherData[
     "Copenhagen", #, {{2016, 1, 1}, {2016, 1, 
       20}}] & /@ {"Temperature", "WindSpeed"};
temperatureData = Extract[#, {{1}, {2, 1}}] & /@ T["Path"];
windspeedData = Extract[#, {{1}, {2, 1}}] & /@ W["Path"];

Then we can compose the graphic.

leftTicks = CustomTicks[Identity, {-10, 10, 5, 5}];
rightTicks = CustomTicks[# + 10 &, {0, 25, 5, 5}];
dateTicks = 
  CustomDateTicks[{{2016, 1, 1}, {2016, 1, 22}, {5, "Day"}, 5}, 
   DateString[#, {"DayShort", " ", "MonthNameShort"}] &];

Draw2D[
 {DateListDraw[temperatureData,
   Joined -> True, PlotStyle -> Blue],
  Blue, Text["Temperature \[Degree]C", {3.6611*^9, -9}],
  DateListDraw[windspeedData,
    Joined -> True, PlotStyle -> Orange] // TranslateOp[{0, 10}],
  Orange, Text["Windspeed Km/h", {3.6617*^9, 30}]},

 AspectRatio -> 1/3,
 PlotRange -> {{AbsoluteTime[{2015, 12, 31, 12}], 
    AbsoluteTime[{2016, 1, 21, 12}]}, {-15, 50}},
 Frame -> True, 
 FrameLabel -> {{"Temperature", "Windspeed"}, {"Date", None}},
 FrameTicks -> {{leftTicks, rightTicks}, {dateTicks, Automatic}},
 BaseStyle -> {FontSize -> 12, Bold},
 ImageSize -> 700
 ]

enter image description here

Hi Sandu,

I am sorry, the only option I know for 'DateListPlot' is 'Overplotting'.......Jos

POSTED BY: Jos Klaps

Hi Sandu,

See also my updated comments and code on 'Labeling Two Plot Axis':

Cheers....... Jos

http://community.wolfram.com/groups/-/m/t/759508?ppauth=v5yijZxt

POSTED BY: Jos Klaps
Posted 9 years ago

Is there any chance this functionality can be included in a future Mathematica release ?

Dual/independent horizontal axes would also be useful.

POSTED BY: Steve M
Posted 9 years ago

But, that is not a DateListPlot, Jos. After some time spent searching though my notebooks today, I found this function, which apparently I have used before ( not written by me, but I had to correct it a bit - the second vertical axis did not display the values):

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, new}, {Automatic, Automatic}}, 
   PlotRange -> {fm, fM}, opts]]

Which if applied to some weather data

{T, W} = WeatherData[
"Copenhagen", #, {{2015, 12, 24}, 
DateList[]}] & /@ {"Temperature", "WindSpeed"};

TwoAxisDateListPlot[{T["Dates"], 
T["Path"][[All, 2, 1]]}\[Transpose], {W["Dates"], 
W["Path"][[All, 2, 1]] 10/36}\[Transpose], 
PlotLegends -> {"Temperature", "WindSpeed"}, 
FrameLabel -> {None, "\[Degree]C", None, "m/s"}, 
FrameStyle -> {Automatic, RGBColor[0.368417`, 0.506779`, 0.709798`], 
White, RGBColor[0.880722`, 0.611041`, 0.142051`]}, 
PlotLabel -> "Copenhagen Weather since Christmas"]

Outputs:

enter image description here

POSTED BY: Sandu Ursu

Hi Sandu,

Sorry, ignore my previous file (wrong posted).

I hope this will help !

Igen[x_, n_, Imax_] := Imax/2*(2/\[Pi] - Sin[x] - 4/\[Pi]*\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(r = 1\), \(n\)]
\*FractionBox[\(Cos[2*r*x]\), \((
\*SuperscriptBox[\((2*r)\), \(2\)] - 1)\)]\));
Vgen[x_, Vdc_, h_] := Vdc/2*(1 + 4/\[Pi] \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(r = 1\), \(h\)]
\*FractionBox[\(Sin[\((2  r - 1)\) x]\), \(2  r - 1\)]\))

(* Two Axis Plot Initalisation *)
TwoAxisPlot[{f_, g_}, {x_, x1_, x2_}] :=

 Module[{fgraph, ggraph, frange, grange, fticks, gticks},

  {fgraph, ggraph} = 
   MapIndexed[
    Plot[#, {x, x1, x2}, Axes -> True, ImageSize -> Large, 
      PlotStyle -> ColorData[1][#2[[1]]]] &, {f, g}];

  {frange, 
    grange} = (PlotRange /. AbsoluteOptions[#, PlotRange])[[
      2]] & /@ {fgraph, ggraph};

  fticks = N@FindDivisions[frange, 5];

  gticks = 
   Quiet@Transpose@{fticks, 
      ToString[NumberForm[#, 2], StandardForm] & /@ 
       Rescale[fticks, frange, grange]};

  Show[fgraph, 
   ggraph /. 
    Graphics[graph_, s___] :> 
     Graphics[
      GeometricTransformation[graph, 
       RescalingTransform[{{0, 1}, grange}, {{0, 1}, frange}]], s], 
   Axes -> False, Frame -> True, 
   FrameStyle -> {ColorData[1] /@ {1, 2}, {Automatic, Automatic}}, 
   FrameTicks -> {{fticks, 
      gticks}, {{-3 Pi, -2 Pi, -Pi, 0, Pi, 2 Pi, 3 Pi}, {Automatic}}}

   , FrameLabel -> {{"Y1 label", "Y2 label"}, {None, None}}

   ]
  ]

Manipulate[
 TwoAxisPlot[{Igen[x, n, Imax], Vgen[x, Vdc, h]}, {x, -3 Pi, 
   3 Pi}], {Imax, 1, 10, 1}, {n, 1, 11, 1}, {Vdc, 1, 10, 1}, {h, 1, 
  11, 1}, Initialization :> {Imax = 2, n = 11, Vdc = 5, h = 5},

 ControlPlacement -> Right]

Good luck,......Jos

Attachments:
POSTED BY: Jos Klaps

Hi Sandu,

Maybe this will help !

JanOnly =
  {{{2014, 1, 1}, 58}, {{2014, 1, 2}, 29}, {{2014, 1, 3}, 
    67}, {{2014, 1, 4}, 31}, {{2014, 1, 5}, 72}, {{2014, 1, 6}, 
    40}, {{2014, 1, 7}, 25}, {{2014, 1, 8}, 70}, {{2014, 1, 9}, 
    31}, {{2014, 1, 10}, 14}, {{2014, 1, 11}, 96}, {{2014, 1, 12}, 
    44}, {{2014, 1, 13}, 75}, {{2014, 1, 14}, 75}, {{2014, 1, 15}, 
    42}, {{2014, 1, 16}, 59}, {{2014, 1, 17}, 80}, {{2014, 1, 18}, 
    58}, {{2014, 1, 19}, 7}, {{2014, 1, 20}, 70}, {{2014, 1, 21}, 
    55}, {{2014, 1, 22}, 23}, {{2014, 1, 23}, 73}, {{2014, 1, 24}, 
    78}, {{2014, 1, 25}, 20}, {{2014, 1, 26}, 29}, {{2014, 1, 27}, 
    64}, {{2014, 1, 28}, 34}, {{2014, 1, 29}, 71}, {{2014, 1, 30}, 
    12}, {{2014, 1, 31}, 70}};

DateListPlot[JanOnly
 , FrameTicks -> {Automatic, Range[0, 100, 10]}
 , InterpolationOrder -> 0]

Good luck,........Jos

Attachments:
POSTED BY: Jos Klaps
Posted 9 years ago

Bruce,

I cannot find in the package the code for TwoAxisDateListPlot, did you eliminate it before uploading the package?

POSTED BY: Sandu Ursu
Here is a package with two-axis Plot and ListPlot.  (Not by me.)

After un-ZIP-ing, copy the folder to under the Applications folder under either $BaseDirectory or
$UserBaseDirectory.  (See http://reference.wolfram.com/mathematica/tutorial/ConfigurationFiles.html.)
Get["TwoAxisListPlot`"]

data1 = Table[{x, x^2}, {x, 0, 4, 1/2}];
data2 = Table[{x, Sin[x]}, {x, 0, 2 \[Pi], \[Pi]/6}];

TwoAxisListPlot[{data1, data2}, PlotMarkers -> Automatic]


The functions in the package include,
In[5]:= ?TwoAxisListPlot`*                                                             
TwoAxisDateListLinePlot TwoAxisListLinePlot     TwoAxisPlot
TwoAxisDateListPlot     TwoAxisListPlot 
Attachments:
POSTED BY: Bruce Miller
Posted 10 years ago
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract