This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

Documentation / Mathematica / Add-ons & Links / Standard Packages / Graphics /

Graphics`Graphics`

You can do most standard plots in two dimensions using the built-in functions Plot, ListPlot, and ParametricPlot. In certain situations you may want to display your data or function in a more specialized format, such as a bar chart or a log-log plot. The functions provided in this package may be used to produce such plots.

Log plots.

This loads the package.

In[1]:= << Graphics`Graphics`

This gives a log-log plot of .

In[2]:= LogLogPlot[x^2, {x, 1, 3}]

Out[2]=

Here, expdata is a list of exponential data with a random offset between -1 and 1.

In[3]:= expdata = Table[
{x, .92 10^(.94 x) + Random[Real,
{-1.0, 1.0}]}, {x, 0.2, 1.2, .1}]

Out[3]=

In a log plot, the exponential data appear roughly linear.

In[4]:= LogListPlot[expdata]

Out[4]=

Polar plots.

This plots an ellipse and a limacon on the same graph.

In[5]:= PolarPlot[{4/(2 + Cos[t]), 4 Cos[t] - 2},
{t, 0, 2 Pi}]

Out[5]=

Bar charts.

Graphics`Graphics` has functions for producing a variety of bar charts. Each bar chart function accepts a number of options controlling the style of the charts.

This gives a bar chart comparing two lists of numbers. Note that the lists need not have the same length.

In[6]:= BarChart[{1, -3, 4, 5, 2, 3}, {3, 6, 4, 3}]

Out[6]=

Here the bars are stacked rather than placed side by side.

In[7]:= StackedBarChart[{1, -3, 4, 5, 2, 3},
{3, 6, 4, 3}]

Out[7]=

A percentile bar chart scales the stacked bars so that each group has an absolute height of one.

In[8]:= PercentileBarChart[{1, -3, 4, 5, 2, 3},
{3, 6, 4, 3}]

Out[8]=

In this chart the bars are explicitly positioned.

In[9]:= GeneralizedBarChart[
{{1, 3, .2}, {4, 9, 1}, {2.1, -6, .5}},
{{3, 5, .5}, {1.5, -2, 1}, {-.4, 2, .5}}]

Out[9]=

Options for any of the bar chart functions.

The bar chart functions accept both special bar chart options and standard graphics options.

This percentile bar chart is created using bar chart options BarStyle and BarOrientation and graphics options Axes and Frame.

In[10]:= PercentileBarChart[{1,3,-4,5,3.5,3},
{-3,2,5,3},
BarStyle -> {RGBColor[0,1,0],
RGBColor[1,1,0]},
BarOrientation -> Horizontal,
Axes -> False, Frame -> True]

Out[10]=

Options for BarChart and GeneralizedBarChart.

With the functions BarChart and GeneralizedBarChart, BarStyle specifies a function to be applied to the value of a bar, for the purpose of determining the bar's color.

Here the bars are colored according to a BarStyle function that gives one of three different values, depending on bar height.

In[11]:= BarChart[{5, 3, 2, -2, 2, 6},
BarStyle ->
(Which[
# > 4, RGBColor[0,1,0],
# < 0, RGBColor[1,0,0],
True, RGBColor[1,1,0]]&),
GridLines -> Automatic
]

Out[11]=

Other bar chart options.

This is an example of using options to create a business chart. Note the use of standard options such as TextStyle to change the look of the font.

In[12]:= BarChart[{1, 3, 4, 5, 3.5, 3}, {3, 2, 5, 3},
BarSpacing -> -.3, BarGroupSpacing -> .5,
BarStyle -> {GrayLevel[.6], Hue[0]},
BarEdgeStyle ->
{{Dashing[{.01}],Hue[0]},GrayLevel[0]},
BarLabels ->
{"Apr","May","Jun","Jul","Aug","Sep"},
PlotLabel -> "Projected and Current Profit,
Tourist Season",
TextStyle -> {"FontFamily" -> "Helvetica",
"FontSize" -> 9}
]

Out[12]=

Using PieChart.

PieChart generates a pie chart from a list of positive numbers. Several options are available for controlling the style of the pie.

Options for PieChart.

The PieStyle and PieLabels options are applied cyclically if there are not enough styles or labels to correspond with the number of wedges (in the same way that PlotStyle works). The PieLineStyle option applies equally to all lines in the chart. The PieExploded option moves designated wedges away from the center of the chart. The default is None, that is, no wedges are moved. You can specify a number, distance pair instead of just the index of a wedge to indicate how far out the wedge should be moved. The wedges are numbered counterclockwise, starting from the middle right.

This combines plots of a pie, an exploded pie, and a partially exploded pie.

In[13]:= DisplayTogetherArray[
PieChart[{.2,.3,.1}],
PieChart[{.2,.3,.1},
PieExploded->All],
PieChart[{.2,.3,.1},
PieExploded->{{3,.2}}]
]

Out[13]=

A pie may be colored cyclically.

In[14]:= PieChart[{.1, .2, .3, .4},
PieStyle->{
GrayLevel[.3], GrayLevel[.8]}]

Out[14]=

Pie wedges may be labeled.

In[15]:= PieChart[{12, 21, 18},
PieLabels -> {"Joe", "Helen", "Bob"},
PlotLabel -> "Sales"
]

Out[15]=

Displaying plots together.

It is often useful to generate several plots and show them together. The conventional way of doing this is to generate each plot separately with the option DisplayFunction -> Identity and then combine the plots with Show. These functions automate that process. DisplayTogether must be given inputs that create graphics that can be combined with Show for this function to be effective. DisplayTogether also accepts options that are suitable for the final output. DisplayTogetherArray accepts the same sort of inputs, but displays them in a GraphicsArray. The input plot commands should be entered in an array whose structure matches that of the desired output array. DisplayTogetherArray accepts options suitable for GraphicsArray.

This is a set of random data following a square law.

In[16]:= data = Table[{n/15, (n/15)^2 + 2 + Random[Real,{-.3,.3}]},
{n, 15}];

This fits a linear combination of , , and to the data.

In[17]:= fit = Fit[data, {1, x, x^2}, x]

Out[17]=

This fits a linear combination of and to the data.

In[18]:= altfit = Fit[data, {1,x^3}, x]

Out[18]=

Here the two fitted curves are combined with a plot of the data.

In[19]:= DisplayTogether[
Plot[altfit, {x,0,1},
PlotStyle -> Hue[.6]],
ListPlot[data,
PlotStyle -> {Hue[0],
PointSize[.03]}],
Plot[fit, {x,0,1},
PlotStyle -> {GrayLevel[0],
Dashing[{.03}]}]
]

Out[19]=

The pie charts previously combined in a side by side arrangement are combined here in a different array arrangement. The structure of the array in which the chart commands are input to DisplayTogetherArray is reflected in the output arrangement of the charts.

In[20]:= DisplayTogetherArray[
{{PieChart[{.2,.3,.1}],
PieChart[{.2,.3,.1},
PieExploded -> All]},
{PieChart[{.2,.3,.1},
PieExploded->{{3,.2}}]}
}
]

Out[20]=

Variations on ListPlot.

Graphics`Graphics` also implements a number of ListPlot variants. These include some techniques for labeling plots of data, and a function to place error bars on data. The package Graphics`MultipleListPlot` has alternatives to these plots and other modifications to the shape of points used in plots of data.

Here the data are defined using the digits of Pi.

In[21]:= data = First[RealDigits[N[Pi]]]

Out[21]=

The digits of Pi are plotted using the integers as plotting symbols.

In[22]:= TextListPlot[data]

Out[22]=

This gives the same plot, except that the integers serve as labels instead of plotting symbols.

In[23]:= LabeledListPlot[data]

Out[23]=

This creates a new list with the magnitude of the error, , appended to each data pair in expdata.

In[24]:= erexpdata = Map[Append[#, 1.0]&, expdata]

Out[24]=

Here is a plot of the data in expdata with error bars.

In[25]:= errorp = ErrorListPlot[erexpdata]

Out[25]=

See the package Graphics`MultipleListPlot` for another way of plotting error bars that allows multiple data sets and lines between the points, among other features.

Using Histogram.

Histogram is used for plotting categorized data frequencies. It gives a bar chart where the width of each bar is proportional to the width of the interval defining the respective category, and the height of the bar is proportional to the frequency with which the data fall in that category. Setting FrequencyData -> True specifies that the data given to Histogram is to be considered not raw data ready for categorization, but frequency data associated with the categories specified by the HistogramCategories option.

This loads a package for generating pseudorandom numbers having a normal distribution.

In[26]:= Needs["Statistics`NormalDistribution`"]

Here is a data set normally distributed, with zero mean and unit variance.

In[27]:= normdata = RandomArray[NormalDistribution[], 100];

Histogram determines the categories automatically, choosing intervals defined by simple numbers.

In[28]:= Histogram[normdata]

Out[28]=

Here is a set of data representing the number of householders in residence for durations of years, where the categories are , , , , and .

In[29]:= countdata = {6, 39, 30, 27, 24};

The option HistogramCategories can be used to specify the histogram categories, as defined by a list of cutoffs. Note that when the bar widths are unequal, the bar heights are scaled by the bar widths to give frequency densities.

In[30]:= Histogram[countdata, FrequencyData->True,
HistogramCategories -> {0, 1, 5, 10, 20, 50}]

Out[30]=

Special histogram options.

The possible settings for the HistogramCategories option are Automatic, a positive integer , or a list of cutoffs. If an integer is specified, then the intervals are taken to be of equal width and to cover the range specified by the HistogramRange option. If ApproximateIntervals -> False, then the histogram will have precisely categories, but if ApproximateIntervals -> True, the requested number of categories may be adjusted in order to give interval boundaries expressed in terms of simple numbers.

The possible settings for the HistogramRange option are Automatic, which specifies that all data is to be included in the histogram, or min, max which specifies the lower and upper bounds on the data to be included.

The possible settings for the HistogramScale option are Automatic, True, or a positive number . If Automatic is specified, then the bar heights won't be scaled by the bar widths (yielding frequency densities) unless the bar widths are unequal. Setting HistogramScale -> True will give frequency densities regardless of bar widths, and setting HistogramScale -> m will scale the bar heights so that the areas of the bars sum to . Setting HistogramScale -> 1 gives a plot approximating the probability density function of the data.

Here HistogramCategories specifies the cutoffs used to categorize the normally distributed data. The setting Ticks -> IntervalCenters puts ticks at the interval centers, and the setting HistogramScale -> 1 scales the plot so that the area of the histogram equals unity.

In[31]:= Histogram[normdata,
HistogramCategories ->
{-3.6, -2.4, -1.4, -.6, 0, .6, 1.4, 2.4, 3.6},
Ticks -> IntervalCenters, HistogramScale -> 1]

Out[31]=

If you plot a histogram of frequency data without specifying the categories, the categories are taken to be of uniform width: , , and so on.

In[32]:= Histogram[countdata, FrequencyData->True]

Out[32]=

The Histogram function accepts special histogram options; special bar chart options, such as BarEdges, BarEdgeStyle, BarStyle, and BarOrientation; and standard graphics options like Ticks. Histogram accepts special Ticks settings IntervalBoundaries and IntervalCenters.

Settings for the Ticks option of Histogram.