3
$\begingroup$

How can I plot two graphs together generated from ListPlot or ListLinePlot command, both t and s have the same number of elements.

I thought Show would solve the problem however it does not, it just displays the first graph mentioned.

eg

t = {{2,1.4}, {5.6,1.9}, {7.9,2.6}, {8.4,2.8}, {10.3,3.1}};

s = {0.2, 0.26, 0.28, 0.31, 0.33};

If I do

tplot=ListPlot[t];
splot=ListPlot[s, PlotStyle -> Red];
Show[tplot, splot]

I will get tplot displayed only or splot if it comes first.

What do I need to do?

$\endgroup$
3
  • $\begingroup$ Why do you think the Plot Ranges are equal? They aren't ... $\endgroup$ Nov 29, 2014 at 22:09
  • $\begingroup$ Sorry I mean the plot range for x, they have the same number of elements which is 5. $\endgroup$
    – Crisp
    Nov 29, 2014 at 22:14
  • $\begingroup$ The plot ranges for x are not the same: it's 2 to 10.3 for t and 1 to 5 for s (remember that if you only specify the y values for s, the x values are taken to be 1, 2, 3, and so on). $\endgroup$
    – seismatica
    Nov 29, 2014 at 22:43

2 Answers 2

4
$\begingroup$

No, the Plot Ranges aren't similar. You need to force them:

pr[n_] := {Min@#, Max@#} &@({t, Transpose[{Range@Length@s, s}]}[[All, All, n]])
tplot = ListLinePlot[t, PlotRange -> {pr@1, pr@2}];
splot = ListLinePlot[s, PlotStyle -> Red, PlotRange -> {pr@1, pr@2}];
GraphicsRow[{Show[splot, tplot], Show[tplot, splot]}]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ this is the answer!! but I might have to spend some time to understand it. $\endgroup$
    – Crisp
    Nov 29, 2014 at 22:21
5
$\begingroup$

The reason is because Show only use the options from the first graphic. Therefore, if you put tplot before splot, the range would be taken as that of tplot (2 to 10.3), and you would not be able to see the {1,0.2} point from splot. Conversely, if splot is in front, the plot follows the y range of splot, which only goes up to 0.33. Therefore, you would not see tplot because it does not fall into that y range.

To fully display the plots of both, you can specify the plot range of the ListPlot or ListLinePlot that is in front to include the range of the second one, using PlotRange (or the more limited DataRange). For example:

splot = ListPlot[s, PlotStyle -> Red, PlotRange -> {{0, 11}, {0, 4}}];
tplot = ListPlot[t];
Show[splot, tplot]

This was what belisarius did in his answer but I think he went one step further and specify the range so that there's no extra space beyond the plot points.

You can also specify Show itself since any options in Show will overwrite the options in the individual plots. Again, use PlotRange, or DataRange.

tplot = ListPlot[t];
splot = ListPlot[s, PlotStyle -> Red];
Show[splot, tplot, PlotRange -> {{0, 11}, {0, 4}}]

Lastly, you can just use ListPlot or ListLinePlot for both lists (s and t) since they are capable of plotting multiple lists of data points. The plot will automatically include all the points in both lists.

ListPlot[{t, s}]

Mathematica graphics

$\endgroup$
1
  • 1
    $\begingroup$ +1 I bypassed the ListPlot[{t, s}] tip. And perhaps that is the better recommendation $\endgroup$ Nov 29, 2014 at 23:57

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