3.2 First Plots

The basic workhorse command of PyXPlot is the plot command, which is used to produce all plots. The following simple example would plot the trigonometric function $\sin (x)$:

plot sin(x)
\includegraphics[width=8cm]{examples/eps/ex_intro_sine}

This is one of a large number of standard mathematical functions which are built into PyXPlot. We will meet more of these in due course, but a complete list can be found in Appendix 2.

As well as plotting functions, it is also possible to plot data stored in files. The following would plot data from a file data.dat, taking the $x$-coordinate of each point from the first column of the datafile, and the $y$-coordinate from the second. The datafile is assumed to be in plain text format1, with columns separated by whitespace and/or commas2:

plot 'data.dat'

Several items can be plotted on the same graph by separating them by commas, as in

plot 'data.dat', sin(x), cos(x)

and it is possible to define one’s own variables and functions, and then plot them, as in the example

a = 0.02
b = -1
c = 5
f(x) = a*(x**3) + b*x + c
plot f(x)
\includegraphics[width=8cm]{examples/eps/ex_intro_func}

A complete list of the mathematical operators which can be used to put together algebraic expressions can be found in Table 3.1.

Symbol

Description

Operator Associativity

**

Algebraic exponentiation

right

-

Unary minus sign

left

not

Logical not

left

*

Algebraic multiplication

left

/

Algebraic division

left

%

Modulo operator

left

+

Algebraic sum

left

-

Algebraic subtraction

left

<<

Left binary shift

left

>>

Right binary shift

left

<

Magnitude comparison

right

>

Magnitude comparison

right

<=

Magnitude comparison

right

>=

Magnitude comparison

right

==

Equality comparison

right

!=

Equality comparison

right

<>

Alias for !=

right

&

Binary and

left

\^{}

Binary exclusive or

left

|

Binary or

left

and

Logical and

left

or

Logical or

left

Table 3.1: A list of mathematical operators which PyXPlot recognises, in order of descending precedence. Items separated by horizontal rules are of differing precedence; those not separated by horizontal rules are of equivalent precedence. The third column indicates whether strings of operators are evaluated from left to right (left), or from right to left (right). For example, the expression x**y**z is evaluated as (x**(y**z)).

Footnotes

  1. If the filename of a datafile ends with a .gz suffix, it is assuming to be gzipped plaintext, and is decoded accordingly. Other formats of datafile can be opened with the use of input filters; see Section 5.1.
  2. This format is compatible with the Comma Separated Values (CSV) format produced by many applications, including Microsoft Excel.