5
$\begingroup$

Is it possible to fill the region between two ListPlots' in Show[]. Something like:

plot1 = ListPlot[f1,Joined->True]
plot2 = ListPlot[f2,Joined->True]
both = Show[plot1,plot2,Filling->{1->{2}}]

my lists f1 and f2 are huge and complicated and has to be computed and plotted separately, i.e. a solution of the form ListPlot[f1,f2,{x,xmin,xmax},Filling->...] is not what I'm looking for. Is this possible and how?

Minimal Example:

list1 = Table[-(x - 2)^2 + 1, {x, 0, 3, 0.1}];
list2 = Table[(x - 1)^2, {x, 0, 3, 0.1}];
plot1 = ListPlot[list1, Joined -> True,DataRange -> {0, 3}]
plot2 = ListPlot[list2, Joined -> True,DataRange -> {0, 3}]
both = Show[plot1,plot2]

Here I would like the region between the plots shaded.

As per the comments I should add that:

The lists list1 and list2 two themselves are lists/matrices. They have dimensions such as 6 times 110 each. Then the object temp = {list1, list2} has Dimension[temp] = {2,6,110} which cannot be ListPlotted straight away.

$\endgroup$
7
  • $\begingroup$ Just join f1and f2points in the same ListPlot, whatever your lists will be. Isn't contradictory to plot each list separately, to combine them afterwards with Show instead of plotting them together with ListPlot? $\endgroup$ Jan 30, 2018 at 18:09
  • $\begingroup$ One could do that but rather not in this case. $\endgroup$ Jan 30, 2018 at 18:14
  • $\begingroup$ Doesn't the following work? ListPlot[{list1, list2}, Joined -> True, DataRange -> {0, 3}, Filling -> {2 -> {1}}]. $\endgroup$
    – JimB
    Jan 30, 2018 at 18:37
  • $\begingroup$ @JimB I added some more info to my question (final paragraph). $\endgroup$ Feb 1, 2018 at 16:15
  • $\begingroup$ If the dimensions of list1 and list2 are {110,6} and the horizontal and vertical coordinates are in respective positions 3 and 5 in the list of 6 variables, then you could certainly use ListPlot[{list1[[All, {3 5}]], list2[[All, {3, 5}]]}]. $\endgroup$
    – JimB
    Feb 1, 2018 at 16:35

2 Answers 2

8
$\begingroup$

If you must do this after the lists have been plotted, you can simply grab the lists from the plot,

lines = FirstCase[#, Line[x_] :> x, Nothing, Infinity] & /@ {plot1, 
    plot2};
ListLinePlot[lines, Filling -> {1 -> {2}}]

enter image description here

$\endgroup$
4
  • $\begingroup$ Well actually I have solved my problem since writing the question (by simply saving the lists separately and then ListPlot them together, hence no need for new code etc) but I'll leave the question alone since it has engaged some people. Thanks anyway! $\endgroup$ Jan 30, 2018 at 18:52
  • $\begingroup$ Could add your solution as an answer as well, in case anyone else stumbles upon this in the future :) $\endgroup$
    – ktm
    Feb 1, 2018 at 16:18
  • $\begingroup$ We're working on the solution :-) but the question differs from this in that there we shade the region before using Show : mathematica.stackexchange.com/q/164961/10325 $\endgroup$ Feb 1, 2018 at 17:23
  • $\begingroup$ @user6014 The solution is in this link mathematica.stackexchange.com/a/164969/10325 $\endgroup$ Feb 1, 2018 at 19:10
4
$\begingroup$

Here's one way to manually tackle the problem. Might be a little rough around the edges and need some clean up to be more robust, but its a start.

Generate polygon filling:

f1 = Table[{x, Sin[x]}, {x, 0, 10, .1}];
f2 = Table[{x, Cos[x]}, {x, 0, 10, .1}];
mins = If[#[[1, 2]] < #[[2, 1]], #[[1]], #[[2]]] & /@ 
   Transpose[{f1, f2}];
maxes = If[#[[1, 2]] < #[[2, 1]], #[[2]], #[[2]]] & /@ 
   Transpose[{f1, f2}];
joined = Join[mins, Reverse@maxes];
gr = Graphics[{LightBlue, Polygon[joined]}]

enter image description here

Combine with ListPlots:

plot1 = ListPlot[f1, Joined -> True];
plot2 = ListPlot[f2, Joined -> True];
both = Show[{plot1, plot2, gr}]

enter image description here

$\endgroup$
3
  • $\begingroup$ For this to work one needs to now the analytic form of the lists right (or at least a fit to them)? $\endgroup$ Jan 30, 2018 at 18:17
  • $\begingroup$ I don't think so, I think the biggest requirement for this is that the two lists have points that line up exactly on the same x coordinates, like in your minimal example. $\endgroup$
    – ktm
    Jan 30, 2018 at 18:18
  • $\begingroup$ If they are parallel, you know that one list is always has a value lesser than the other list for every x value, so you can just do f1 = Table[{x, 2 x + 1}, {x, 0, 10, .1}]; f2 = Table[{x, 2 x + 2}, {x, 0, 10, .1}]; joined = Join[f1, Reverse@f2]; gr = Graphics[{LightBlue, Polygon[joined]}] without picking mins/maxes programmatically. If almost parallel means they still intersect than this isn't relevant. $\endgroup$
    – ktm
    Jan 30, 2018 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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