24

I plotted the function $y=x^2$ using the Plot command

Plot[x^2, {x,-5,5}]

I need to shade the area under the curve using a pattern (dots pattern), not using a solid color or a hue. Is it possible? I have found no help about it.

mmal
  • 3,508
  • 2
  • 18
  • 38
James
  • 241
  • 1
  • 3

6 Answers6

29

This is a simple little hack that will replace the polygons created by your Filling command with a set of random points. By default I'm scaling the number of random points by the number of points in the polygon, so that the density of points stays relatively constant.

dotFillPlot[plot_, ndots_: 5] := 
 plot // Normal // 
  ReplaceAll[
   Polygon[a__] :> {PointSize[Small], 
     Point[RandomPoint[Polygon[a], ndots Length@a]]}]

dotFillPlot@Plot[Sin[x], {x, 0, 2 π}, Filling -> Axis]
dotFillPlot@
 Plot[Evaluate[Table[BesselJ[n, x], {n, 4}]], {x, 0, 10}, 
  Filling -> Axis]

Mathematica graphics

I'd rather have a regular grid of points, but that will require a more elaborate function I think - using a Texture with the polygons.

If you don't care for the dot's appearance, then you might prefer to manually set the Opacity for the dots rather than taking the value from the polygon. If you put Opacity[0.6] right before RandomPoint in the function definition, then you get the following plot:

Mathematica graphics

Jason B.
  • 68,381
  • 3
  • 139
  • 286
14

Try the following. This function makes the dots:

lst[m_, stepx_, stepy_] := 
  Flatten[Table[Table[{x, y}, {y, 0, x^2, stepy}], {x, -m, m, stepx}],
    1];

Here m is the interval of your plot (in your case m=5), stepx and step y are the distances between the dots in the x and y directions.

This makes the image:

Show[{
  Plot[x^2, {x, -5, 5}],
  Graphics[{Blue, PointSize[0.01], Point@lst[5, 0.5, 1.5]}]
  }]

you can play with stepx and stepy to adjust the distances and with the color and PointSize to get the desired view. the result is:

enter image description here

Have fun!

Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96
8

One can fill with an arbitrary Texture using this post-processing function:

fixFill = Normal[#] /. 
   Polygon[x_] :> 
     Polygon[x, VertexTextureCoordinates ->
       Rescale /@ (x\[Transpose])\[Transpose]] &;

Basic usage:

tex = ExampleData[{"TestImage", "Apples"}];

Plot[x^2, {x, -5, 5}
 , Filling -> Bottom
 , FillingStyle -> Texture[tex]
] // fixFill

enter image description here

This works with complex Filling specifications as well:

tex2 = ExampleData[{"TestImage", "Sailboat"}];

Plot[{x^2, 30 Cos[x/3]}, {x, -5, 5}
 , Filling -> {
     1 -> {Axis, Texture[tex]}, 
     2 -> {{1}, {None, Texture[tex2]}}
    }
] // fixFill

enter image description here

(* slow to load if you have not used ExampleData["ColorTexture"] before *)

Manipulate[
 Plot[x^2, {x, -5, 5}
   , Filling -> Bottom
   , FillingStyle -> Texture[texture]
 ] // fixFill,
 {texture, ExampleData /@ ExampleData["ColorTexture"]}
]

enter image description here

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
6

Perhaps something like this?

plotDot[f_, plotRange_, points_] := Show[
  Plot[f[x], {x, -5, 5}, PlotRange -> plotRange],
  ListPlot[
    Select[
      Map[
        {#[[1]]*(plotRange[[1, 2]] - plotRange[[1, 1]])/points + plotRange[[1, 1]],
          #[[2]]*(plotRange[[2, 2]] - plotRange[[2, 1]])/points * GoldenRatio 
          + plotRange[[2, 1]]
        } &, DeleteCases[Flatten[Table[If[EvenQ[x + y], {x, y}],
          {x, 0, points}, {y, 0, points}], 1], Null]],
        (#[[2]] < f[#[[1]]]) &
      ], PlotMarkers -> {Automatic, 6}
    ]
  ];

(* Examples *)
plotDot[#^2 &, {{-5, 5}, {-10, 25}}, 40]
plotDot[#^3 &, {{-5, 5}, {-50, 50}}, 50]
plotDot[Sin[#] &, {{-5, 5}, {-2, 2}}, 60]

Polka dot

You can control the number of dots with the third argument of plotDot. Sadly, you always have to specify the plot range.

mszynisz
  • 824
  • 5
  • 9
  • 1
    plotDot[f_, {x_, min_, max_}, np_Integer?Positive, opts___] := Module[{plt, pts, rng}, plt = Plot[f, {x, min, max}, Evaluate @ FilterRules[{opts}, Complement[Options[Plot], Options[Graphics]]]]; rng = Charting`get2DPlotRange[plt]; pts = Select[RescalingTransform[{{0, np}, {0, np}}, rng][Select[Flatten[CoordinateBoundsArray[{{0, np}, {0, np}}], 1], EvenQ[Total[#]] &]], (#[[2]] < Function[x, f][#[[1]]]) &]; Show[plt, ListPlot[pts, PlotMarkers -> {Automatic, 6}, Evaluate @ FilterRules[{opts}, Complement[Options[ListPlot], Options[Graphics]]]], FilterRules[{opts}, Options[Graphics]]]] – J. M.'s missing motivation Feb 01 '17 at 07:17
  • That's one way to come up with something that doesn't need an explicit plot range specification. – J. M.'s missing motivation Feb 01 '17 at 07:18
  • 1
    @J.M. That's great. I would only change rng definition to not include padding: rng = Charting`get2DPlotRange[plt, False]; – mszynisz Feb 01 '17 at 07:38
6

Since V 12.1 we have PatternFilling

Plot[x^2, {x, -5, 5},
 Filling -> Axis,
 FillingStyle -> 
  PatternFilling[{"HalftoneGrid", ColorData[97, "ColorList"][[1]]}, 
   ImageScaled[1/20]]]

enter image description here

eldo
  • 67,911
  • 5
  • 60
  • 168
2
cba = CoordinateBoundsArray[{{-5, 5}, {0, 25}}, {0.25, 1}];
pts = Map[Select[0.05 < Last@# < First@(#^2) &], cba, {1}];
Plot[x^2, {x, -5, 5}
 , PlotStyle -> Thick
 , Epilog -> {Red, Point /@ pts}
 ]

enter image description here

Syed
  • 52,495
  • 4
  • 30
  • 85