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

How do I make graphs of (easy) functions like those in textbooks?

99 views
Skip to first unread message

cdj

unread,
Apr 4, 2003, 1:45:44 AM4/4/03
to
Hi,

I'm sure that questions like this have been asked/answered before, but
I'm not sure what keywords to search on to find them... some obvious
candidates didn't get me anywhere...

Take, for example, f(x)= (x-3)^2 - 1

I'd greatly appreciate seeing the Mathematica code that does the
following:

(a) Plots the parabola itself (duh).
(b) Does so on a a gridded piece of "graph paper", with the axes
substantially darker than the rest of the grid.
(c) Both x and y axes are numbered at the units: 0, +-1, ..., +-10.
The numbers should be to the left of the y axis, and below the x axis.
The origin doesn't have to be labelled, if it's ugly, or too hard.
(d) The y axis is labelled "y" at the top of the graph, x axis is
labelled "x" on the right.
(e) Puts little arrows on the 4 tips of the axes (signifying that they
continue arbitrarily far).
(f) The following points are explicitly represented with
reasonable-sized dots: (0,8), (2, 0), (3,-1), (4,0), and (6,8).
(g) It would be nice, but not essential, if the tips of the parabola
itself had little arrows (again signifying that it continues upward
with increasing abs(x) values.

Sorry for all the conditions - just trying to be somewhat precise
about what I meant by graphs "like those in textbooks". Once I get the
code for this, I'm sure I can modify it to other similar tasks...

Can all these desiderata be obtained in *one* Mathematica graph? Or is
there another graphics/graphing program that is better-suited to such
tasks?

Thanks a bunch for any help,

cdj

David Park

unread,
Apr 5, 2003, 2:12:14 AM4/5/03
to
For anyone who has the most recent DrawGraphics, cdj's plot may be made with
the following code.

Needs["DrawGraphics`DrawingMaster`"]

f[x_] := (x - 3)^2 - 1

I want to plot only over the domain that gives the displayed range. That
makes it easier to attach arrows at both ends of the curve.

Solve[f[x] == 10]
{{x -> 3 - Sqrt[11]}, {x -> 3 + Sqrt[11]}}

xygrid := CustomGridLines[Identity, {-10, 10, 1}, {LightBlue}];
xyticks := CustomTicks[Identity, {-10, 10, 1, 1}]

plot1 =
Module[{g, greverse},
g = Draw[f[x], {x, 3 - Sqrt[11], 3 + Sqrt[11]}];
greverse = g /. Line[a_] :> Line[Reverse[a]];
Draw2D[
{VenetianRed,
g,
ArrowCurve[g, 1, HeadCenter -> 1/2, HeadLength -> 0.03],
ArrowCurve[greverse, 1, HeadCenter -> 1/2, HeadLength -> 0.03],
Black, AbsoluteThickness[1],

Arrow[{0, 0}, #, HeadCenter -> 1/2, HeadLength -> 0.03] & /@ {{0,
10}, {0, -10}, {10, 0}, {-10, 0}},

CirclePoint[#, 4, Black, White] & /@ {{0, 8}, {2, 0}, {3, -1}, {4,
0}, {6, 8}}},

AspectRatio -> Automatic,
PlotRange -> {{-1, 1}, {-1, 1}}10.02,
Axes -> True,
AxesStyle -> White,
AxesLabel -> {x, y},
Ticks -> {xyticks, xyticks},
GridLines -> {xygrid, xygrid},
PlotLabel -> "Book Plot\n\n",
ImageSize -> 600]];

David Park
dj...@earthlink.net
http://home.earthlink.net/~djmp/

Bill Rowe

unread,
Apr 5, 2003, 2:26:52 AM4/5/03
to
On 4/4/03 at 1:25 AM, a_cj...@hotmail.com (cdj) wrote:

>Take, for example, f(x)= (x-3)^2 - 1

>I'd greatly appreciate seeing the Mathematica code that does the
>following:

>(a) Plots the parabola itself (duh).

>(b) Does so on a a gridded piece of "graph paper", with the axes
>substantially darker than the rest of the grid.

>(c) Both x and y axes are numbered at the units: 0, +-1, ..., +-10.
>The numbers should be to the left of the y axis, and below the x axis.
>The origin doesn't have to be labelled, if it's ugly, or too hard.

>(d) The y axis is labelled "y" at the top of the graph, x axis is
>labelled "x" on the right.

>(e) Puts little arrows on the 4 tips of the axes (signifying that they
>continue arbitrarily far).

>(f) The following points are explicitly represented with
>reasonable-sized dots: (0,8), (2, 0), (3,-1), (4,0), and (6,8).

>(g) It would be nice, but not essential, if the tips of the parabola
>itself had little arrows (again signifying that it continues upward
>with increasing abs(x) values.

The following code does most of what you listed above

<< Graphics`Arrow`;
Plot[(x - 3)^2 - 1, {x, -1, 10}, PlotRange -> {{-1, 7}, {-1, 10}},
AxesStyle -> {Thickness[ .005]}, Ticks -> {Range[-1,7], Range[-1, 10]},
AspectRatio -> 1, PlotPoints -> 50, AxesLabel -> {"x", "y"},
Epilog -> {PointSize[ .015], Point/@ {{0, 8}, {2, 0}, {3, -1}, {4, 0}, {6, 8}},
Arrow[{6, 0}, {7, 0}], Arrow[{0, 0}, {-1, 0}],
Arrow[{0, 0}, {0, -1}], Arrow[{0, 9}, {0, 10}]}];

What is missing is the arrows on the ends of the parabola and the background grid. The background grid could be added with a GridLines->Automatic directive or a specific function for drawing the GridLines. To my eye, adding grid lines to this plot detracts from the information being displayed. I also think the heavy axes and arrows on the end of the axes distracts attention from the data (parabola and plotted points).

Visually, I think the following code produces a much better plot.

(Plot[(x - 3)^2 - 1, {x, -1, 10}, PlotRange -> {{-1,7}, {-1, 10}},
AxesLabel -> {"\<x\>", "\<y\>"}, Ticks -> {Range[-1, 7], Range[\(-1\), 10]},
AspectRatio -> 1, PlotPoints -> 50, AxesOrigin -> {-1, -1},
Epilog -> {PointSize[ .015], Point /@ {{0, 8}, {2, 0}, {3, -1}, {4, 0}, {6, 8}}}];


Which is better obviously depends on your taste and purpose.

In any case a good reference on creating graphics in Mathematica is

The Mathematica Graphics Guidebook by Cameron Smith and Nance Blachman

This reference is somewhat dated since it only covers Mathematica version 2 and earlier. Despite this I still think it is one of the best guides to Mathematica graphics that has been written.

An excellent general guide to displaying graphic information is

The Visual Display of Quantitative Information written by Edward Tufte

This reference shows many examples of very bad and very good graphic displays. Tufte also give general guidelines for creating good graphics. Adding emphasis to the axes by making it darker and adding arrows is definitely inconsistent with the guidelines Tufte gives.

Adding emphasis to the axes draws attention to the axes and away from the data (parabola and plotted points). Since the only reason for producing a graph is to display the data, other elements of the graph should help clarify the data, not draw attention away from the data.

Dr Bob

unread,
Apr 5, 2003, 2:29:59 AM4/5/03
to
I used David Park's drawing package for this, which you can get at

http://home.earthlink.net/~djmp/

First, load the package.

Needs["DrawGraphics`DrawingMaster`"]

Here's a routine for building the graph paper gridlines.

gridLines[f_, xSpan : {_Integer | _Real, _Integer | _Real}] :=
Block[{ySpan, xGrid, yGrid, xGridFine, yGridFine, xGranularity,
yGranularity},
xGranularity = Round[xSpan.{-1, 1}/20];
xGridFine = Range[Sequence @@ Inner[#1@#2 &, {Floor, Ceiling},
xSpan, List], xGranularity];
xGrid = Select[xGridFine, Mod[#, 5xGranularity] == 0 &];
ySpan = {Min@#, Max@#} &[f /@ Join[xGridFine, xSpan]];
yGranularity = Round[ySpan.{-1, 1}/20];
yGridFine = Range[Sequence @@ Inner[#1@#2 &, {Ceiling,
Floor}, ySpan, List], yGranularity];
yGrid = Select[yGridFine, Mod[#, 5yGranularity] == 0 &];
{Join[{#, {Blue}} & /@ xGrid, {#, {PowderBlue}} & /@ xGridFine],
Join[{#, {Blue}} & /@ yGrid, {#, {PowderBlue}} & /@ yGridFine]}
]

Here's a routine for the plot. It supplies some options, but also allows
them to be overridden or supplemented. It also allows graphics to be
inserted along with the main function plot.

niceDraw[f_, graphicsPrimitives___,
xSpan : {_Integer | _Real, _Integer | _Real}, opts___] :=
Draw2D[{Draw[f@x,
Evaluate@{x, Sequence @@ span}],
graphicsPrimitives},
opts,
GridLines -> gridLines[f, span],
PlotRange -> All, Frame -> True, Axes -> True, AspectRatio ->
1,
ImageSize -> 400,
AxesStyle -> {Black, Thickness[0.005]},
FrameLabel -> {x, y}, RotateLabel -> False];

Here's an example:

f[x_] := (x - 3)^2 - 1

niceDraw[f, AbsolutePointSize[5],
Point /@ {{0, 8}, {2, 0}, {3, -1}, {4, 0}, {6, 8}}, {-5, 15},
AspectRatio -> 0.7];

Bobby

On Fri, 4 Apr 2003 01:25:49 -0500 (EST), cdj <a_cj...@hotmail.com> wrote:

> Hi,
>
> I'm sure that questions like this have been asked/answered before, but
> I'm not sure what keywords to search on to find them... some obvious
> candidates didn't get me anywhere...
>

> Take, for example, f(x)= (x-3)^2 - 1
>
> I'd greatly appreciate seeing the Mathematica code that does the
> following:
>
> (a) Plots the parabola itself (duh).
> (b) Does so on a a gridded piece of "graph paper", with the axes
> substantially darker than the rest of the grid.
> (c) Both x and y axes are numbered at the units: 0, +-1, ..., +-10.
> The numbers should be to the left of the y axis, and below the x axis.
> The origin doesn't have to be labelled, if it's ugly, or too hard.
> (d) The y axis is labelled "y" at the top of the graph, x axis is
> labelled "x" on the right.
> (e) Puts little arrows on the 4 tips of the axes (signifying that they
> continue arbitrarily far).
> (f) The following points are explicitly represented with
> reasonable-sized dots: (0,8), (2, 0), (3,-1), (4,0), and (6,8).
> (g) It would be nice, but not essential, if the tips of the parabola
> itself had little arrows (again signifying that it continues upward
> with increasing abs(x) values.
>

> Sorry for all the conditions - just trying to be somewhat precise
> about what I meant by graphs "like those in textbooks". Once I get the
> code for this, I'm sure I can modify it to other similar tasks...
>
> Can all these desiderata be obtained in *one* Mathematica graph? Or is
> there another graphics/graphing program that is better-suited to such
> tasks?
>
> Thanks a bunch for any help,
>
> cdj
>
>

--
maj...@cox-internet.com
Bobby R. Treat


Dr Bob

unread,
Apr 5, 2003, 2:31:00 AM4/5/03
to
Oops. I had a small bug there (span where I should have had xSpan), so I'm
sending a new copy of the gridLines function. I'm sending another example,
too.

gridLines[f_, xSpan : {_Integer | _Real, _Integer | _Real}] :=
Block[{ySpan, xGrid, yGrid, xGridFine, yGridFine, xGranularity,
yGranularity},
xGranularity = Round[xSpan.{-1, 1}/20];
xGridFine = Range[Sequence @@ Inner[#1@#2 &, {Floor, Ceiling},
xSpan, List], xGranularity];
xGrid = Select[xGridFine, Mod[#, 5xGranularity] == 0 &];
ySpan = {Min@#, Max@#} &[f /@ Join[xGridFine, xSpan]];
yGranularity = Round[ySpan.{-1, 1}/20];
yGridFine = Range[Sequence @@ Inner[#1@#2 &, {Ceiling,
Floor}, ySpan, List], yGranularity];
yGrid = Select[yGridFine, Mod[#, 5yGranularity] == 0 &];
{Join[{#, {Blue}} & /@ xGrid, {#, {PowderBlue}} & /@ xGridFine],
Join[{#, {Blue}} & /@ yGrid, {#, {PowderBlue}} & /@ yGridFine]}
]

f[x_] := (x - 3)^2 - 1
niceDraw[f, Red, AbsolutePointSize[7],
Point@{#,f@#} & /@ {0, 2, 3, 4, 6},


{-5, 15}, AspectRatio -> 0.7];

Bobby

Display all headersTo: cdj <a_cj...@hotmail.com>, math...@smc.vnet.net,
David Park <dj...@earthlink.net>Subject: Re: How do I make graphs
of (easy) functions like those in textbooks?Reply-To: majort@cox-
internet.comFrom: Dr Bob <maj...@cox-internet.com>Organization: Space
CorpsDate: Fri, 04 Apr 2003 21:55:38 -0600

cdj

unread,
Apr 5, 2003, 5:33:17 PM4/5/03
to
Bill Rowe <list...@earthlink.net> wrote in message news:<b6m0fs$ehv$1...@smc.vnet.net>...

> Adding emphasis to the axes draws attention to the axes and away from the >data (parabola and plotted points). Since the only reason for producing a >graph is to display the data, other elements of the graph should help clarify >the data, not draw attention away from the data.

First off: thank you (and everyone else) who responded - the responses
were very helpful.

Next: thanks for the aesthetic advice, but I was just ticking off
features of a graph in a high school text book I was looking at. I
agree that I'm not gonna want all of them, but I just wanted to see
the code for the richest graphic I could think of, and whittle down to
reasonableness...

Finally: Thanks for the references.

Thanks again all,

cdj

cdj

unread,
Apr 7, 2003, 4:50:47 AM4/7/03
to
Dr Bob <maj...@cox-internet.com> wrote in message news:<b6m0nk$eid$1...@smc.vnet.net>...

> Oops. I had a small bug there (span where I should have had xSpan), so I'm
> sending a new copy of the gridLines function. I'm sending another example,
> too.

Hmmm... I get bunches of "possible spelling errors" and such things
when I run the code. I've got Mathematica version 4.1 - you?

When I go to display the graph itself, only the chosen red points show
up - the curve doesn't, and it craps out with some kind of error...

cdj

0 new messages