6
$\begingroup$

I am using this code

x = {-2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2};
y = {1.21, 1.24, 1.26, 1.21, 1.28, 1.25, 1.21, 1.23, 1.26};

p1 = ListPlot[Thread[{x, y}], PlotStyle -> Red, Joined -> True];
p2 = ListPlot[Thread[{x, y^2}], PlotStyle -> Red, Joined -> True];
p3 = ListPlot[Thread[{x, y^3}], PlotStyle -> Blue, Joined -> True];
p4 = ListPlot[Thread[{x, y^4}], PlotStyle -> Blue, Joined -> True];
Show[p1, p2, p3, p4, PlotRange -> All]

and the result is plot no.1. How can I ask Mathematica to fill in colors between the curves as plot no.2?

enter image description here

$\endgroup$

2 Answers 2

8
$\begingroup$
ListPlot[
 {Thread[{x, y}], Thread[{x, y^2}],
  Thread[{x, y^3}], Thread[{x, y^4}]},
 Filling -> {
   1 -> {{2}, Opacity[0.3, Red]}, 
   3 -> {{4}, Opacity[0.3, Blue]}
 },
 PlotStyle -> {Red, Red, Blue, Blue}, Joined -> True,
 PlotRange -> {1, 3}
]

plot with colors

$\endgroup$
1
  • 1
    $\begingroup$ +1 More compactly, ListPlot[Thread[{x, y^#}] & /@ Range[4], ...] $\endgroup$
    – Bob Hanlon
    Commented Nov 24, 2020 at 1:00
4
$\begingroup$

You can also construct a TemporalData object from your x and y and use it with ListLinePlot:

td = TemporalData[Table[y^i, {i, 4}], {x}];

llp = ListLinePlot[td,
 PlotStyle -> {Red, Red, RGBColor[.45, .77, .92], RGBColor[.45, .77, .92]}, 
 Filling -> {1 -> {2}, 3 -> {4}}, 
 PlotRange -> {1, All}]

enter image description here

Alternatively, use Table[y^i, {i, 4}] as input data and add the option DataRange -> MinMax[x]:

llp = ListLinePlot[Table[y^i, {i, 4}], 
 DataRange -> MinMax[x],
 PlotStyle -> {Red, Red, RGBColor[.45, .77, .92], RGBColor[.45, .77, .92]}, 
 Filling -> {1 -> {2}, 3 -> {4}}, 
 PlotRange -> {1, All}]

enter image description here

SeedRandom[1]
Normal[llp] /. p_Polygon :> Dynamic[texturedPoly[CurrentValue["Color"]][p]]

enter image description here

where

ClearAll[texturedPoly]
texturedPoly[color_: Red, thickness_: 10, n_: 200, sd_: 100, jitter_: 5][p_Polygon] := 
 Module[{BSF = BSplineFunction[Join @@ (RandomSample[Partition[#, 20]] & @ 
   SortBy[RandomPoint[BoundingRegion[p[[1]], "MinConvexPolygon"], n], First])]}, 
  {Opacity[.8], Texture @ 
   ImageEffect[Graphics[{CapForm["Round"], JoinForm["Round"], 
      color, AbsoluteThickness[thickness], 
      Line[BSF /@ Subdivide[sd]]}], {"Jitter", jitter}], 
   Polygon[p[[1]], VertexTextureCoordinates -> Automatic]}]
$\endgroup$

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.