Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Hatched shading?

257 views
Skip to first unread message

Alex

unread,
Jul 16, 2010, 5:18:37 AM7/16/10
to
Is it possible to plot something like this in Mathematica?

http://img682.imageshack.us/img682/1638/hatched.png

How might I do the hatched shading, with customizable colors/thickness?

David Park

unread,
Jul 17, 2010, 8:17:38 AM7/17/10
to
Here is a solution with Presentations. There are many elements to the plot
so it's a little detailed. I think it is better to have a complete single
input description of the graphic, than to make a partial plot and then use
DrawingTools. What would happen if you had to change the function?

The graphic might be better and simpler to make if you used color Filling
instead of hatching. Filling is a little less "busy" and distracting.
Hatching was a device to overcome limitations in print media. The
limitations mostly don't exist anymore. So here is the filling solution
first.

Needs["Presentations`Master`"]

f[x_] := 1/x;
Draw2D[
{(* Draw the curve with a fill *)
Draw[f[x], {x, 1, 5},
AxesOrigin -> {0, 0},
Filling -> Axis,
FillingStyle -> Blend[{Orange, White}, .6]],
(* Draw the boundary lines *)
AbsoluteThickness[2],
Line[{{1, 0}, {1, 1.1 f[1]}}],
Line[{{5, 0}, {5, 1.1 f[1]}}],
Line[{{.9, 0}, {5.1, 0}}],
(* Add the labels and point *)
Text[Style["Boundary Condition", 14, Bold], {.8, .5}, {0, 0}, {0,
1}],
Text[Style["Boundary Condition", 14, Bold], {5.2, .5}, {0, 0}, {0,
1}],
Text[Style["Initial Condition", 14, Bold], {3, -0.1}, {0, 0}],
Text[Style["Solution Known", 14], {3, 0.15}, {0, 0}],
Text[Style["\[CapitalGamma]", 14], {1.5, f[1.5]}, {-3, 0}],
AbsoluteThickness[1],
CirclePoint[{3.5, f[3.5]}, 3, Black, Green],
Text[Style["P", 14, Bold], {3.5, f[3.5]}, 1.5 {-1, -1}]},

AspectRatio -> .6,
PlotRange -> {{.5, 5.5}, {-.25, 1}},
PlotRangePadding -> {.1, .05},
Frame -> False,
ImageSize -> 400]

Here is the graphic using hatching. We supply a Table of lines using a
RegionFunction to limit their extent. It is necessary to specify PlotRange
or Mathematica does not always obey the RegionFunction!

f[x_] := 1/x;
Draw2D[
{(* Draw the hatch lines first *)
Table[
Draw[.5 x - x0, {x, -1.5, 5},
RegionFunction ->
Function[{x, y}, 1 < x < 5 \[And] 0 <= y <= f[x]],
PlotRange -> {{1, 5}, {0, 1}},
PlotStyle -> Directive[AbsoluteThickness[3], LightGray]], {x0, -1,
5, .075}],
(* Draw the curve without a fill *)
Draw[f[x], {x, 1, 5}],
(* Draw the boundary lines *)
AbsoluteThickness[2],
Line[{{1, 0}, {1, 1.1 f[1]}}],
Line[{{5, 0}, {5, 1.1 f[1]}}],
Line[{{.9, 0}, {5.1, 0}}],
(* Add the labels and point *)
Text[Style["Boundary Condition", 14, Bold], {.8, .5}, {0, 0}, {0,
1}],
Text[Style["Boundary Condition", 14, Bold], {5.2, .5}, {0, 0}, {0,
1}],
Text[Style["Initial Condition", 14, Bold], {3, -0.1}, {0, 0}],
Text[Style["Solution Known", 14], {3, 0.15}, {0, 0}],
Text[Style["\[CapitalGamma]", 14], {1.5, f[1.5]}, {-3, 0}],
AbsoluteThickness[1],
CirclePoint[{3.5, f[3.5]}, 3, Black, Green],
Text[Style["P", 14, Bold], {3.5, f[3.5]}, 1.5 {-1, -1}]},

AspectRatio -> .6,
PlotRange -> {{.5, 5.5}, {-.25, 1}},
PlotRangePadding -> {.1, .05},
Frame -> False,
ImageSize -> 400]


David Park
djm...@comcast.net
http://home.comcast.net/~djmpark/

Bob Hanlon

unread,
Jul 17, 2010, 8:15:09 AM7/17/10
to

Plot[
{Table[(x - k)/3, {k, -3, 3, .15}],
f[x]},
{x, 0, 3},
PlotRange -> {0, 1},
RegionFunction ->
Function[{x, y}, 0 < y <= f[x]],

PlotStyle -> {
Directive[
AbsoluteThickness[3],
LightBlue],
Directive[
Thick,
Blue]}]


Bob Hanlon

---- Alex <abud...@gmail.com> wrote:

=============

Bob Hanlon

unread,
Jul 18, 2010, 1:06:58 AM7/18/10
to
I forget to copy the definition of f[x] that I was using as an example

f[x_] = Exp[-x]


Bob Hanlon

---- Bob Hanlon <han...@cox.net> wrote:

=============

ADL

unread,
Jul 20, 2010, 3:42:54 AM7/20/10
to
Following what Bob brilliantly suggested, I found a possible bug in
Mathematica 7.0.1 for Windows.
If you type the following, you will get a couple of red lines getting
out of their boundary:

f[x_] := Sin[x];

Plot[
{Table[(x - k)/3, {k, -3, 3, .10}], f[3x]},


{x, 0, 3},
PlotRange -> {0, 1},

RegionFunction -> Function[{x, y}, 0 < y <= f[3x]],
PlotStyle -> {
Directive[ AbsoluteThickness[4], Red],
Directive[ Thick, Blue]
}
]

Does anybody else confirms this?

ADL

E. Martin-Serrano

unread,
Jul 21, 2010, 7:12:24 AM7/21/10
to

Hi,

Yes.

Using your code I also got two protruding red lines, one in each blue plot.

Version: {7.0 for Microsoft Windows (32-bit), Release: 1}

E. Martin-Serrano

Bob Hanlon

unread,
Jul 21, 2010, 7:13:44 AM7/21/10
to

I see similar artifacts on a Mac. Playing with PlotPoints can stop this

$Version

7.0 for Mac OS X x86 (64-bit) (February 19, 2009)

f[x_] := Sin[x];

Plot[{
Table[(x - k)/3, {k, -3, 3, .10}],

f[3 x]},


{x, 0, 3},
PlotRange -> {0, 1},
RegionFunction ->

Function[{x, y}, 0 < y <= f[3 x]],


PlotStyle -> {
Directive[AbsoluteThickness[4], Red],

Directive[Thick, Blue]},
PlotPoints -> 40]


Bob Hanlon

---- ADL <alberto...@tiscali.it> wrote:

=============

David Park

unread,
Jul 21, 2010, 7:14:26 AM7/21/10
to
The problem with RegionFunction is how many points does Mathematica use,
especially with a straight line, and how does the algorithm determine where
the boundary should be. Suppose one point is well within the region and the
next point is well without. I'm sure that Mathematica doesn't calculate the
exact intersections with boundaries. So I think the only reliable method is
to use some brute force and trim the lines if they extend outside the
region. Again, here is a Presentations solution where it is convenient to
treat the hatching and the curve separately.

Needs["Presentations`Master`"]

f[x_] := Sin[x];

Draw2D[
{(* Draw hatch lines first *)
Table[
Draw[(x - k)/3, {x, -1.5, 3},
RegionFunction ->
Function[{x, y}, 0 < x < 3 \[And] 0 <= y <= f[3 x]],
PlotRange -> {{1, 3}, {0, 1}},
PlotPoints -> 200,
PlotStyle ->
Directive[AbsoluteThickness[2], Red]] /. {x_?NumberQ,
y_?NumberQ} :>
If[0 < x < 3 \[And] 0 <= y <= f[3 x], {x, y},
Unevaluated[Sequence[]]], {k, -3, 3, .1}],



(* Draw the curve without a fill *)

Blue, AbsoluteThickness[2],
Draw[f[3 x], {x, 0, 3}, PlotRange -> {0, 1}]
},

AspectRatio -> .6,
PlotRange -> {{0, 3}, {0, 1}},


PlotRangePadding -> {.1, .05},

Axes -> True,
ImageSize -> 400]

If you do the same graphic with say 25 points you will see that Mathematica
did not always use points very close to the boundary.

Alexey Popkov

unread,
Jul 21, 2010, 7:10:46 AM7/21/10
to
Hello,

I confirm this behavior on Windows XP SP3 with Mathematica 7.0.1. All the
red lines get out of the region defined by RegionFunction.


"ADL" <alberto...@tiscali.it> news:i23k1u$eqc$1...@smc.vnet.net...

annetts729

unread,
Jul 22, 2010, 5:44:33 AM7/22/10
to
On Jul 21, 7:10 pm, "Alexey Popkov" <lehi...@gmail.com> wrote:
> Hello,
>
> I confirm this behavior on Windows XP SP3 with Mathematica 7.0.1. All the
> red lines get out of the region defined by RegionFunction.
>
> "ADL" <alberto.dilu...@tiscali.it> news:i23k1u$eqc$1...@smc.vnet.net...

>
> > Following what Bob brilliantly suggested, I found a possible bug in
> > Mathematica 7.0.1 for Windows.
> > If you type the following, you will get a couple of red lines getting
> > out of their boundary:
>
> > f[x_] := Sin[x];
>
> > Plot[
> > {Table[(x - k)/3, {k, -3, 3, .10}], f[3x]},
> > {x, 0, 3},
> > PlotRange -> {0, 1},
> > RegionFunction -> Function[{x, y}, 0 < y <= f[3x]],
> > PlotStyle -> {
> > Directive[ AbsoluteThickness[4], Red],
> > Directive[ Thick, Blue]
> > }
> > ]
>
> > Does anybody else confirms this?
>
> > ADL

Yeah -- there are red lines.

But I'd suggest it's an artifact of plotting rather than a bug per se.

Look at

f[x_] := Sin[x];

Plot[{Table[(x - k)/3, {k, -3, 3, .10}], f[3 x]}, {x, 0, 3},


PlotRange -> {0, 1},

RegionFunction -> Function[{x, y}, 0 < y <= f[3 x]],


PlotStyle -> {Directive[AbsoluteThickness[4], Red],

Directive[Thick, Blue]},
PlotPoints -> #, ImageSize -> 500] & /@ {25, 50, 75, 100}

Regards,

Dave.

0 new messages