• Aucun résultat trouvé

Special Two-Dimensional Plots

Dans le document Second Edition (Page 135-148)

Two-Dimensional Graphics

4.3 Special Two-Dimensional Plots

Discrete functions, i.e., functions defined on a discrete set, can be visualized using the special plotting function ListPlot.

ListPlot[{y1, y2,...}] plots points whose y-coordinates are y1, y2, . . . The x-coordinates are taken to be the positive integers, 1, 2, . . .

ListPlot[{{x1, y1}, {x2, y2},...}] plots the points (x1, y1), (x2, y2), . . . Standard graphics options are permitted. The form of the command would then be

ListPlot[{y1, y2, ...}, options] or

ListPlot[{{x1, y1}, {x2, y2},...}, options]

The most useful graphics options used with ListPlot are

PlotStyle ãPointSize[d] where d specifies the diameter of the point as a fraction of the overall width of the graph. The default value is .008. In addition, the following symbolic forms can be used: Tiny, Small, Medium, and Large. These specify point sizes independent of the total width of the graphic.

PlotStyle ã AbsolutePointSize[d] where d is measured in printer’s points, equal to 1 of an inch. 72

PlotMarkers ã Automatic will cause the point markers to take different shapes, e.g., circles, squares, diamonds, etc. This is useful when two or more sets of points are to be plotted.

Filling ã Axis fills the graph vertically to the horizontal axis.

Filling ã Bottom fills the graph vertically to the bottom of the graph.

Filling ã Top fills the graph vertically to the top of the graph.

Filling ã v fills the graph vertically to the value v.

EXAMPLE 31 The following plots a list of the squares of the positive integers 1 through 20.

squares = Table[k2, {k, 1, 20}];

ListPlot[squares]

5 10 15 20

100 200 300 400

ListPlot[squares, PlotStyle ã PointSize[.03]]

5 10 15 20

100 200 300 400

EXAMPLE 32

randomintegers = Table[RandomInteger,[{1, 20}], {k, 1, 30}];

ListPlot[randomintegers]

5 5

10 15

10 15 20 25 30

The Table command generates a list of 30 random integers, each between 1 and 20.

The Table command generates a list of 30 random integers, each between 1 and 20.

ListPlot[randomintegers,Filling ã Axis]

5 10 15

5 10 15 20 25 30

ListLinePlot[{y1, y2,...}] plots points whose y-coordinates are y1, y2, . . . and connects them with line segments. The x-coordinates are taken to be the positive integers.

ListLinePlot[{{x1, y1}, {x2, y2},...}] plots the points (x1, y1), (x2, y2), . . . and con-nects them with line segments.

ListLinePlot[list1, list2, ...] plots multiple lines through points defined by list1, list2, . . .

The options for ListPlot may be used for ListLinePlot. The Filling option may be used to create a filled polygon that describes the data.

EXAMPLE 33 (Continuation of Example 32) ListLinePlot[randomintegers]

15

10

5

5 10 15 20 25 30

ListLinePlot[randomintegers, Filling ã Axis]]

5 10 15

5 10 15

20 25 30

Different types of bar graphs can be drawn with Mathematica, using the command BarChart.

Note: Starting with version 7, BarChart can be found in the Mathematica kernel. If you are using version 6, you will find BarChart in the package BarCharts` which must be loaded prior to use. See the Documentation Center for appropriate usage.

BarChart[datalist] draws a simple bar graph. datalist is a set of numbers enclosed within braces.

BarChart[{datalist1, datalist2,...}] draws a bar graph containing data from multiple data sets. Each data list is a set of numbers enclosed within braces.

EXAMPLE 34

dataset1 = {1, 2, 3, 4, 5}; dataset2 = {6, 5, 4, 3, 2}; g1 = BarChart[dataset1];

g2 = BarChart[{dataset1, dataset2}];

GraphicsArray[{g1, g2}]

If a customized look is desired, there are a variety of options that can be invoked. The format of the command with options becomes

BarChart[datalist,options]

BarChart[{datalist1, datalist2,...}, options]

Some of the more popular options are

Chartstyleãg specifies that style option g should be used to draw the bars. Examples of style options are GrayLevel, Hue, Opacity, RGBColor, and Colors (Red, Blue, etc.).

Chartstyleã{g1, g2,...} specifies that style options g1, g2, . . . should be used cyclically.

ChartLayoutã"layout" specifies that a layout of type layout should be used to draw the graph.

Examples of layouts are "Stacked", in which case the bars are stacked on top of each other rather than placed side by side, and "Percentile", which generates a stacked bar chart with the total height of each bar constant at 100%.

BarSpacing controls the spacing between bars and between groups of bars. The default is BarSpacingãAutomatic which allows Mathematica to control the spacing.

BarSpacingãs allows a space of s between bars within each data set. The value of s is measured as a fraction of the width of each bar.

BarSpacingã{s, t} allows a space of s between bars within each data set and a value of t determines the space between data sets. The values of s and t are measured as a fraction of the width of each bar.

In each of the preceding BarSpacing commands, the values of s and t may be replaced by the pre-defined symbols None, Tiny, Small, Medium and Large.

BarOrigin ã edge controls where the bars originate from. The default value of edge is Bottom.

Other acceptable values are Top, Left, and Right.

ChartLabels ã {label1, label2,...} specifies the labeling for each bar corresponding to each value in the data list.

EXAMPLE 35

dataset1 = {1, 2, 3, 4, 5}; dataset2 = {6, 5, 4, 3, 2};

g1 = BarChart[{dataset1, dataset2}, ChartLayout ã "Stacked"];

g2 = BarChart[{dataset1, dataset2}, ChartLayout ã "Percentile"];

GraphicsArray[{g1, g2}]

5 10 15 20

20 40 60 80 100

EXAMPLE 36

dataset = {6, 3, 4, 1, 5};

BarChart[dataset, ChartLabels ã{"Bar1", "Bar2", "Bar3", "Bar4", "Bar5"}]

Bar1 Bar2 Bar3 Bar4 Bar5

1 2 3 4 5 6

EXAMPLE 37

dataset = {6, 3, 4, 1, 5}; g1 = BarChart[dataset];

g2 = BarChart[dataset, BarOrigin ã Top];

g3 = BarChart[dataset, BarOrigin ã Left];

g4 = BarChart[dataset, BarOrigin ã Right];

GraphicsArray[{{g1, g2}, {g3, g4}}]

1 2 3 4 5 6

2

4

6

1 2 3 4 5 6 6 4 2

Pie Charts may be constructed using the PieChart command.

Note: Starting with version 7, PieChart can be found in the Mathematica kernel. If you are using version 6, you will find PieChart in the package PieCharts` which must be loaded prior to use. See the Documentation Center for appropriate usage.

PieChart[datalist] draws a simple pie chart. datalist is a list of numbers enclosed within braces.

PieChart[{datalist1, datalist2,...}] draws a pie chart containing data from multiple data sets.

Each data set is a list of numbers enclosed within braces.

Similar to BarChart, there are options that can be invoked to enhance the display. The format of the command with options becomes

PieChart[datalist,options]

PieChart[{datalist1, datalist2,...}, options]

Some of the available options associated with PieChart are

Chartstyle ã g specifies that style option g should be used to draw the bars. Examples of style options are GrayLevel, Hue, Opacity, RGBColor, and Colors (Red, Blue, etc.).

Chartstyleã{g1, g2,...} specifies that style options g1, g2, . . . should be used cyclically.

SectorSpacing determines the spacing between concentric sectors for different data sets and the spac-ing between sectors within a data set.

SectorSpacing ã s determines the spacing between concentric sectors for different data lists.

The value of s is measured as a fraction of the radial width of the sectors.

SectorSpacing ã {s, t} allows a space of s between sectors corresponding to each data set and a space of t between concentric sectors for different data sets. The values of s and t are measured as a fraction of the radial width of the sectors.

In each of the preceding SectorSpacing commands, the values of s and t may be replaced by the predefined symbols None, Tiny, Small, Medium and Large.

Note: Clicking on any sector of a pie chart will cause it to shift radially outward by an amount s.

EXAMPLE 38

dataset = {1.5, 3, 4.5, 9}; g1 = PieChart[dataset];

g2 = PieChart[dataset, SectorSpacing ã{Tiny, None}];

GraphicsArray[{g1, g2}]

EXAMPLE 39

datalist = {1.5, 3, 4.5, 9};

g1 = PieChart[datalist, ChartLabels ã {"First Sector", "Second Sector", "Third Sector", "Fourth Sector"}]

First sector Second sector

Third sector

Fourth sector

SOLVED PROBLEMS

4.18 Plot the first 50 prime numbers.

SOLUTION

primelist = Table[Prime[k], {k, 1, 50}];

ListPlot[primelist]

10 20 30 40 50

50 100 150 200

4.19 Plot the points (0, 0), (2, 7), (3, 5), and (4, 11) and connect them with line segments.

SOLUTION

list = {{0, 0}, {2, 7}, {3, 5}, {4, 11}}; ListLinePlot[list, PlotMarkers ã Automatic]

1 2 3 4

2 4 6 8 10

4.20 Plot the set of points corresponding to the first ten primes, the first ten Fibonacci numbers, and the first ten perfect squares. First plot individual points and then plot them connected with line segments.

SOLUTION

PlotLegends`

list1 = Table[Prime[n], {n, 1, 10}];

list2 = Table[Fibonacci[n], {n, 1, 10}];

list3 = Table[n2, {n, 1, 10}];

ListPlot[{list1, list2, list3}, PlotMarkers ã Automatic, PlotLegend ã {"Primes", "Fibonacci", "Squares"}, LegendPosition ã {1,0}]

Primes Fibonacci Squares 100

80 60 40 20

2 4 6 8 10

ListLinePlot[{list1, list2, list3}, PlotMarkers ã Automatic, PlotLegend ã {"Primes", "Fibonacci", "Squares"}, LegendPosition ã {1,0}]

100 80 60 40 20

2 4 6 8 10

Primes Fibonacci Squares

4.21 The monthly sales for XYZ Corp. (in thousands of dollars) were

JAN FEB MAR APR MAY JUNE JULY AUG SEPT OCT NOV DEC

13.2 15.7 17.4 12.6 19.7 22.6 20.2 18.3 16.2 15.0 12.1 8.6

Construct a bar graph illustrating this data.

SOLUTION

months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

salesdata = {13.2, 15.7, 17.4, 12.6, 19.7, 22.6, 20.2, 18.3, 16.2, 15.0, 12.1, 8.6};

BarChart[salesdata, ChartLabels ã months]

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 5

10 15 20

4.22 Construct a pie chart illustrating the data of the previous problem.

SOLUTION

months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

salesdata = {13.2, 15.7, 17.4, 12.6, 19.7, 22.6, 20.2, 18.3, 16.2, 15.0, 12.1, 8.6};

PieChart[salesdata, ChartLabels ã months]

Jan Feb

Mar Apr

May

Jun

Jul

Sep Aug Oct Nov Dec

4.4 Animation

Animation effects can be produced quickly and easily through the use of the Animate command. This command displays several different graphics images rapidly in succession, producing the illusion of movement. The form of the command is

Animate[expression,{k, m, n, i}]

where expression is any Mathematica command with parameter k which varies from m to n in increments of i (optional; if omitted,i varies continuously from m to n).

The following example gives an interesting animated description of the behavior of the odd powers of xn as n gets larger.

EXAMPLE 40

Animate[Plot[xk, {x, –1, 1}, PlotRange ã {–1, 1}, Ticks ã False], {k, 1, 19, 2}]

The speed of the animation and the direction are easily controlled by clicking on the , , and buttons. The animation can be paused, using the button.

To allow the user more control over the animation, the Manipulate command can be used.

Manipulate works very much the same way as Animate except it allows the user to control the param-eter directly with a slider.

Manipulate[expression,{k, m, n, i}]

EXAMPLE 41

Manipulate[Plot[xk, {x, –1, 1}, PlotRange ã {–1, 1}, Ticks ã False], {k, 1, 19, 2}] Click here for animation controls

Click here for an options menu

A convenient way of controlling expressions involving integer parameters is by clicking on “radio buttons.” This can be accomplished with the option ControlType ã RadioButton.

EXAMPLE 42

Manipulate[Plot[xk, {x, –1, 1}, PlotRange ã {–1, 1}, Ticks ã False],

{k, 1, 19, 2}, ControlType ã RadioButton]

expression may involve two or more parameters. In this case the form of the command is

Animate[expression,{k1, m1, n1, i1}, {k2, m2, n2, i2},...]

Manipulate[expression,{k1, m1, n1, i1}, {k2, m2, n2, i2},...]

Each parameter can be controlled independently (speed, direction, pause).

EXAMPLE 43

Animate [Plot[a Sin[b x], {x, 0, 2 o}, PlotRange ã {–10, 10}], {a, 0, 10}, {b, 0, 10}]

a b

10

–10 5

–5

1 2 3 4 5 6

EXAMPLE 44 This animation shows a circle of varying radius whose center varies from (–1, –1) to (1, 1).

Pause each variable (x, y, r) to see the effect.

Animate[Graphics[Circle[{Sin[x], Cos[y]}, r], Axes ã True,

PlotRange ã {{–2, 2}, {–2, 2}}], {x, 0, 2 o}, {y, 0, 2 o}, {r, 0, 1}]

x y r

2

1

–2 –1 1 2

–1

–2

Animate and Manipulate are not limited to the presentation of graphics. We will use these com-mands in other contexts in later chapters.

SOLVED PROBLEMS

4.23 Construct an animation of the Spiral of Archimedes, r = q as q varies from 8 π to 10 π. SOLUTION

Animate [PolarPlot[θ, {θ, 0, 8o+ e}, TicksãFalse,

PlotRangeã{{– 10o, 10o}, {–10o, 10o}}], {e, 0, 2o}]

4.24 Use Manipulate to simulate a point “rolling” along a sine curve from 0 to 2 π. SOLUTION

First we construct the sine curve.

sincurve = Plot[Sin[x], {x, 0, 2 o}, TicksãFalse]

Now we animate the sequence of points as red disks of radii 0.05.

Manipulate[Show[sincurve, Graphics[{Red, Disk[{x, Sin[x]}, 0.05]}], PlotRange ã {{0, 2 o}, {–1, 1}},

AspectRatio ã Automatic], {x, 0, 2 o}].

Move the slider to control the movement of the disk.

133

Dans le document Second Edition (Page 135-148)