2
$\begingroup$

How can I use "Filling" in plot with two different styles?

Specifically, I want to change the second "filling" to HatchFill with lines?

Plot[{x, 2 x}, {x, 0, 1}, Filling -> {1 -> {2}, 2 -> Top}]

I come up with a complex method as follow:

Plot[{x, 2 x}, {x, 0, 1}, Filling -> {1 -> {2}}]
Plot[{x, 2 x}, {x, 0, 1}, Filling -> {2 -> Top}, 
 FillingStyle -> HatchFilling[]]
Show[{%1,%2}]

Is there a simple way to express the two needs in one sec? Thx a lot!

$\endgroup$
2
  • $\begingroup$ Plot[{x, 2 x}, {x, 0, 1}, Filling -> {1 -> {2}, 2 -> {Top, Directive[Green, HatchFilling[]]}}] $\endgroup$
    – cvgmt
    Sep 30 at 10:59
  • $\begingroup$ One additional question: is there a directive like HatchFilling to fill a region with dots? :) $\endgroup$
    – Jay Zhang
    Sep 30 at 11:12

1 Answer 1

4
$\begingroup$
Plot[{x, 2 x}, {x, 0, 1},
 Filling ->
  {1 -> {2},
   2 -> {Top, Directive[ColorData[97, "ColorList"][[2]], HatchFilling[]]}}]

enter image description here

Plot[{x, 2 x}, {x, 0, 1},
 Filling ->
  {1 -> {2},
   2 -> {Top, 
     Directive[ColorData[97, "ColorList"][[2]], 
      HatchFilling["Vertical"]]}}]

enter image description here

There are many more HatchFilling options. See Documentation.

Addendum

To fill with dots (as per comment) use PatternFilling:

 Plot[{x, 2 x}, {x, 0, 1}, Filling -> {1 -> {2}, 2 -> {Top,
     PatternFilling[{"Halftone", ColorData[97, "ColorList"][[2]]}, ImageScaled[1/20]]}}]

enter image description here

You can control the number and size of the points with the ImageScaled - parameter.

$\endgroup$
2
  • $\begingroup$ Thanks! BTW, does 97 stand for the color set of the default plot theme? What is the number for the "scientific" theme then? $\endgroup$
    – Jay Zhang
    Sep 30 at 11:20
  • $\begingroup$ Yes, that's the default. With other words, the hatch filling color is the same as the line color. I don't know the number for the "scientific" theme. $\endgroup$
    – eldo
    Sep 30 at 11:26

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.