6

For "RegionPlot", how to fill lines, points, or grids in different areas?

I have tried "Mesh", but it fills all region in same mesh.

I want to get the result just like this picture,

enter image description here

Thank you!

hu_rrr
  • 81
  • 4

2 Answers2

14
regions = {(x - 2)^2 < (y - 2)^3 + 1, (x - 2)^2 > (y - 2)^3 + 1 && 
    y > x - 3, y < Min[2, x - 3], x - 3 > y > 2 && x > 5};

You can specify PlotStyle using PatternFiling:

plotstyles = MapThread[PatternFilling[{#, #2}, ImageScaled[#3]] &, 
    {{"Diamond", "HalftoneGrid", "Grid", "Chevron"}, 
    ColorData[97] /@ Range[4], {1/40, 1/40, 1/40, 1/50}}];

RegionPlot[regions, {x, 0, 10}, {y, 0, 5}, AspectRatio -> Automatic, ImageSize -> 700, BoundaryStyle -> Thick, PlotLegends -> SwatchLegend["Expressions", LegendMarkerSize -> 40], PlotStyle -> plotstyles] /. s_SwatchLegend :> (s /. ImageScaled[a_] :> ImageScaled[10 a])

enter image description here

Replace the last entry in plotstyles with HatchFilling:

plotstyles[[4]] = Directive[ColorData[97] @ 4, HatchFilling["Horizontal"]];

to get

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
  • Thanks! I think it's perfect, is this specific to 13? I'm still using 12, and maybe I need to update. – hu_rrr Apr 12 '22 at 09:48
  • @hu_rrr, HatchFilling and PatternFilling were introduced in version 12.1 – kglr Apr 12 '22 at 09:49
  • Thank you, you helped me a lot. – hu_rrr Apr 12 '22 at 09:57
  • @hu_rrr, my pleasure. Welcome to mma.se. – kglr Apr 12 '22 at 10:20
  • May I ask what the meaning of /. s_SwatchLegend :> (s /. ImageScaled[a_] :> ImageScaled[10 a])? – HANG YU Mar 30 '23 at 07:51
  • @HANGYU, this piece of code replaces any expression with head SwatchLegend (s_SwatchLegend) with a new expression where subexpressions in s that match the pattern ImageScaled[something] are replaced with ImageScaled[10 something] (see ReplaceAll in the docs) – kglr Apr 01 '23 at 21:49
  • 1
    @kglr, oh, I see, thank you very much, this command adjusts the Legends and makes them more clear. – HANG YU Apr 03 '23 at 02:15
5

If it is good enough for you, you may fill with colors, using the option Filling:

Plot[{Sin[2 x], 1 + x^2, 2 + x}, {x, 0, 1}, Filling -> Automatic]

enter image description here

Daniel Huber
  • 51,463
  • 1
  • 23
  • 57
  • Thank you! But I also want to get 3 regions that are "color1+line/point/mesh1","color2+line/point/mesh1", and "color1+line/point/mesh2". – hu_rrr Apr 12 '22 at 08:35
  • Related, see https://mathematica.stackexchange.com/questions/49202/hatched-filling-for-listplot/49206#49206 – Acus Apr 12 '22 at 08:54
  • I have understood, thank you very much!!! – hu_rrr Apr 12 '22 at 09:06