22
$\begingroup$

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.

$\endgroup$
3
  • 1
    $\begingroup$ Related: [filling] hatched. $\endgroup$ Aug 8, 2016 at 14:24
  • $\begingroup$ Possible duplicate of How do I plot a histogram with hatched shading? $\endgroup$
    – Hector
    Aug 8, 2016 at 14:28
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Aug 8, 2016 at 16:22

4 Answers 4

27
$\begingroup$

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

$\endgroup$
4
  • $\begingroup$ How does this approach do with PlotTheme -> "Monochrome"? $\endgroup$ Aug 8, 2016 at 15:07
  • $\begingroup$ It does better now, thanks $\endgroup$
    – Jason B.
    Aug 8, 2016 at 15:13
  • 1
    $\begingroup$ You could better scale the number of points with area of a filling polygon, with the help of RegionMeasure. $\endgroup$
    – BoLe
    Aug 9, 2016 at 6:38
  • $\begingroup$ @BoLe - Thanks, I had tried this (with Area instead), but decided it was slowing down the function too much $\endgroup$
    – Jason B.
    Aug 9, 2016 at 19:14
11
$\begingroup$

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!

$\endgroup$
7
$\begingroup$

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

$\endgroup$
4
$\begingroup$

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.

$\endgroup$
3
  • 1
    $\begingroup$ 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]]]] $\endgroup$ Feb 1, 2017 at 7:17
  • $\begingroup$ That's one way to come up with something that doesn't need an explicit plot range specification. $\endgroup$ Feb 1, 2017 at 7:18
  • 1
    $\begingroup$ @J.M. That's great. I would only change rng definition to not include padding: rng = Charting`get2DPlotRange[plt, False]; $\endgroup$
    – mszynisz
    Feb 1, 2017 at 7:38

Your Answer

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

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