9
$\begingroup$

How to fill a region with lines:

Plot[{t + 1, t}, {t, 0, 4}, PlotRange -> {0, 4},Filling -> {1 -> {2}}]

I want to change the filling style to vertical lines, as shown in the right figure below. How should I change the code? enter image description here

$\endgroup$
  • 3
    $\begingroup$ In addition to the answe below, you may want to search this site for "hatched" or "hashed" filling. There have been a few examples, the best of which seem to rely on using a RegionPlot of the region and mesh lines. $\endgroup$ – MarcoB Mar 21 '16 at 4:11
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 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$ – Dr. belisarius Mar 21 '16 at 8:48
  • 3
    $\begingroup$ Strongly related: "Generating hatched filling using Region functionality." $\endgroup$ – Alexey Popkov Mar 21 '16 at 8:57
10
$\begingroup$
funs = {t + 1, t};

Show[{Plot[funs, {t, 0, 4}, PlotRange -> {0, 4}], 
      RegionPlot[funs[[2]] < y < funs[[1]], {t, 0, 10}, {y, 0, 6}, 
                 Mesh -> 60, MeshFunctions -> {#1 &}, BoundaryStyle -> None, 
                 MeshStyle -> {Gray, Thickness[.001]}, PlotStyle -> Transparent]}]

Mathematica graphics

Just for fun,to show the flexibility of this method:

Show[{Plot[funs, {t, 0, 4}, PlotRange -> {0, 4}], 
  RegionPlot[funs[[2]] < y < funs[[1]], {t, 0, 10}, {y, 0, 6}, 
   Mesh -> 60, MeshFunctions -> {Sin[#1] Sin[#2] &}, 
   BoundaryStyle -> None, MeshStyle -> {Gray, Thickness[.001]}, 
   MeshShading -> {Red, Green, None, Yellow}, 
   PlotStyle -> Transparent]}]

Mathematica graphics

$\endgroup$
9
$\begingroup$

you could use GridLines also

Plot[{t + 1, t}, {t, 0, 4}, PlotRange -> {0, 4}, 
 GridLines -> {Range[0, 4, .2], None}, 
 Filling -> {1 -> {Top, White}, 2 -> {Bottom, White}}]

enter image description here

$\endgroup$
5
$\begingroup$

I guess the simplest way is:

Show[
 ListPlot[{
   Table[i*2 + 1, {i, -1, 3, 0.1}],
   Table[i*2 + 3, {i, -1, 3, 0.1}]}, Joined -> True],
 ListPlot[{
   Table[i*2 + 1, {i, -1, 3, 0.1}],
   Table[i*2 + 3, {i, -1, 3, 0.1}]
   }, Joined -> False, Filling -> {1 -> {2}}, 
  PlotStyle -> PointSize[0.001]]
 ]

The point-based representation allows to have a discrete line-based filling.

enter image description here

$\endgroup$
3
$\begingroup$

A modification of Rom38's.

Show[
 Plot[{t + 1, t}, {t, 0, 4}, PlotRange -> {0, 4}],
 ListLinePlot[Table[{{i, i + 1}, {i, i}}, {i, 0, 4, 0.2}], PlotStyle -> {{Gray, Thin}}]
 ]

enter image description here

Another option with Epilog:

Plot[{t + 1, t}, {t, 0, 4}, PlotRange -> {0, 4}, 
 Epilog -> Table[Line @ {{i, i}, {i, i + 1}}, {i, 0, 4, 0.2}]]

enter image description here

$\endgroup$
3
$\begingroup$

Using Epilog

Plot[{t + 1, t}, {t, 0, 4}, 
 PlotRange -> {0, 4}, 
 Epilog -> Line /@ (Thread@{#, {# + 1, #}} & /@ Range[0, 4, 0.2])]

enter image description here

$\endgroup$
2
$\begingroup$

Here's yet another way to get what you are going for, by using ContourPlot

ContourPlot[#, {x, 0, 4}, {y, 0, 4}, ContourShading -> False, 
    Contours -> 20] & /@ {{x == y,
    x + 1 == y},
   Piecewise[{{x, x < y < x + 1}}, Indeterminate]} // Show

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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