Message Boards Message Boards

GROUPS:

Any suggestions for displaying text Grid with Overlay plot?

Posted 3 months ago
622 Views
|
6 Replies
|
6 Total Likes
|

I want to present a text Grid and an Overlay Plot on the same line (Row) with useful scaling. I am stuck with an Overlay because I am displaying a 2-axes TimeSeries. The "My Goal" image was created by editing 2 JPEGs. How can I create the equivalent presentation directly in Mathematica? I am using Version 11.1, without the new presentation features of 11.3. My goal:

My Goal

The closest I have gotten with the following demo data:

(* as a test case, create 2 fake time series *)
d1 = {1, 2, 3, 4, 5};
d2 = {10, 20, 20, 30, 40};
dateList = Table[DateList[{2014 + x, 1}], {x, 0, 4}];
yearList = 
  MapThread[DateString, {dateList, Table["Year", Length[dateList]]}];
ts1 = TimeSeries[d1, {dateList}];
ts2 = TimeSeries[d2, {dateList}];

was initially presentable table (show above) and a NON-useful plot using:

(* on my first try at a presentation,  I did the following: *)
   labeledTable = 
     Grid[{Text@Style[#, "TableHeader"] & /@ {"\nYear", "Size of\n   D1", 
         "Size of\n   D2"}, {Column[yearList], Column[d1], Column[d2]}}, 
      Alignment -> {Center}, Frame -> All]
    DateListPlot[{ts1, ts2}]

HiddenDetails

The plot needed 2 axes. I believe the TimeSeries forced use of an Overlay:

    (* to make a 2 axes DateListPlot, it seems one must use an Overlay... *)
plot1 = 
      DateListPlot[ts1, PlotStyle -> Red, 
       Frame -> {True, True, False, False},
       ImagePadding -> 25, FrameTicks -> {None, All, None, None}, 
       FrameStyle -> {{Directive[Red], None}, {None}}];
    plot2 = DateListPlot[ts2, PlotStyle -> Blue, 
       Frame -> {False, False, False, True}, ImagePadding -> 25, 
       FrameTicks -> {{None, All}, {None, None}}, 
       FrameStyle -> {{None, Directive[Blue]}, {None}}];
    overPlot = Overlay[{plot1, plot2}]

This was nice, but I can't get the desired presentation format, combining the plot with the table. The problem seems to be that an Overlay is not a scalable Graphic, and neither Row[labeledTable,overPlot] nor Grid[labeledTable,overPlot] is very pretty.

YUK!!

I would appreciate any suggestions, even if you only have a better way to generate or display the test data grid (like how to center a 2-line column header over decimal data).

6 Replies

diller,

First, there is a good reference for plotting with two Y axes on the Wolfram site. The function can be easily modified to do DateListPlots by changing "Plot" and removing the range list and adjusting a few parameters. Then your plot will not be an overlay and will be a bit more general. See How to | Generate Plots with Two Vertical Scales. This will not fix your second problem but it may be useful.

To fix the second problem, you need to specify the ImageSize for the plots (which will carryover to the overlay). If you do this I think you get what you want.

plot1 = DateListPlot[ts1, PlotStyle -> Red, 
   Frame -> {True, True, False, False}, ImagePadding -> 25, 
   FrameTicks -> {None, All, None, None}, 
   FrameStyle -> {{Directive[Red], None}, {None}}, ImageSize -> Large];
plot2 = DateListPlot[ts2, PlotStyle -> Blue, 
   Frame -> {False, False, False, True}, ImagePadding -> 25, 
   FrameTicks -> {{None, All}, {None, None}}, 
   FrameStyle -> {{None, Directive[Blue]}, {None}}, 
   ImageSize -> Large];
overPlot = Overlay[{plot1, plot2}]

Now you can use Row or Grid:

Row[{labeledTable, overPlot}]

To get:

enter image description here

Of course the Plot is a bit too big so you can adjust the sizes accordingly. ImageSize can also take numerical sizes. You can also specify the size of your grid. I hope this helps.

Regards,

Neil

I like Neil's method with ImageSize->Large . Nice!

It is interesting that if I open the documentation browser and search for "Generate Plots with Two Vertical Scales", I don't find this guide. I wonder what I need to type to get there in Mathematica.

Hi Gustavo,

As Neil Singer already mentioned in privious discussion. Here the Hyperlink with an example :

"http://community.wolfram.com/groups/-/m/t/1388426"

Title: How to Generate Plots with Two Vertical Scales

Suppose two functions have the same domain and different ranges. Plotting them together using Plot uses the same scale for the values. To compare the functions, TwoAxisPlot plots them with different scales for the values (on the left and right) so that their ranges appear to be the same.

Here it is not obvious whether there is a simple relation between the two functions:

Plot[{Sin[x], 3 Sin[x] + 5 Cos[x]}, {x, 0, 4 Pi}]

The custom function defined here, TwoAxisPlot, rescales two plots to appear to have the same range. The ranges of the functions are shown on the left and right sides of the frame:

TwoAxisPlot[{f_, g_}, {x_, x1_, x2_}] := 
 Module[{fgraph, ggraph, frange, grange, fticks, 
   gticks}, {fgraph, ggraph} = 
   MapIndexed[
    Plot[#, {x, x1, x2}, Axes -> True, 
      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}, {Automatic, Automatic}}]]

The combined plot illustrates the fact that for some and :

TwoAxisPlot[{Sin[x], 3 Sin[x] + 5 Cos[x]}, {x, 0, 4 Pi}]

enter image description here

Regards,....Jos

guide/HowToTopics >> howto/CreatePlots >> howto/GeneratePlotsWithTwoVerticalScales

yee not so easy to find this in the Search hit list.

maybe sometimes Browse is the way to go

Gustavo,

I agree it is not easy to find. I often use a search engine (duckduckgo) to search the mathematica documentation if I can't find it quickly using the built-in documentation search. In this case I remembered it existed so I did a web search and not a documentation search. However, I also found it relatively quickly by opening up the documentation for "Plot" and followed the links at the bottom for "Basic Plotting" tutorial and then following a link to "How To: Create Plots". This index had many useful pages including plotting with two axes. I find the documentation works well if you find a command that is close and follow the links.

I suggest reporting this issue to support and maybe they will make the searching more affective in the future.

Regards,

Neil

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