# JMathLabTutorial:Plots

# Plotting

Data may be graphed using the **plot(x,y)**-function, x and y being equalsized vectors, which denote the coordinates of the data points to be plotted. A third optional argument **plot(x,y,option)** specifies plot options like colors and symbols. The graphic may then be decorated (axis, title) and exported as encapsulated-postscript file for inclusion in text documents. With **hold** (or hold on) the graphic gets locked so that subsequent plot commands use the same window. Repeating hold (or hold off) deletes the graphic.

The method:

`plot(x,y [option])`

plots x versus y using option x and y are equalized vectors. option is a string, specifying color (one of r,g,b,y,m,c,w,k) and symbol (one of +,*,o,x). Default: blue lines.

Logarithmic and semi-logarithmic plots are provided with the functions **loglog**, **linlog** and **loglin**.

Here are the major plot methods:

Name(Arguments) | Function | |
---|---|---|

plot(x,y [option]) | Plot x versus y | |

loglog(x,y [option]) | logarithmic plot | |

linlog(x,y [option]) | semi-logarithmic plot | |

loglin(x,y [option]) | semi-logarithmic plot | |

print(name ) | write graphic in eps-file |

Let as show a simple example of plotting sin(x) between 0 and 10 with a step 0.1

However, one can overlay these plots using the "hold" method:

You can plot symbols as well:

Run this plot to see its output.# Plotting data and function

Now let us draw data points with errors and overlay a Gaussian function $y = A*\exp(-((x-x0)/\sigma)^2)$. First we build a function "fit", draw data points and overlay the Gaussian function at fixed values [1.4912 3.9911 1.4481 ]

# Examples

**Example 1** let us plot the function:
$y=\frac{1}{1+2x^2}$ in the range x=0.01...100 linear and logarithmic. The code is below:

**Example 1a** let us plot the function cos(x):

**Example 2**: Display of Lissajous-figures: From the vector t=0:0.1:4*pi; create the trigonometric expressions x=sin(0.5*t+1); and y=cos(1.5*t);. The plot x vs. y is called Lissajous-figure. Create different figures by variating the constants 0.5,1,1.5 in the definition. Partial solution:

**Example 3**
Calculate the first 100 elements of the sequences

$x_n = \frac{n-1}{n}; x_n=\frac{n+1}{n}; x_n=\frac{n+(-1)^n}{n}$

Plot $x_n$ versus $n$ using the command plot(n, xn). Variate the plotoptions (colors, symbols).

**Example 4**
Plot the data points of the following table using plot and colored symbols. Calculate the linear regression using polyfit, and plot the regression line in the same graph. Add title and labels, and export the graphic to a file suitable for inclusion in a text document.

```
x 0 1 2 3 4 5 6 7 8 9
y -3.1 -0.7 1.8 4.1 6.2 8.9 11.3 13.5 16 18.3
```

# Plot2D

The method **plot2d()** is used to plot functions and arrays in 2D.
One can add title, labels, change the minimum and maximum ranges for X-axis and Y-axis.
You can also apply different styles.

First, let us create a canvas and prepare for plotting of data.

There are several methods associated with it:

- clear2d - clear the canvas from data
- export2d(file) - export the plot to the vector format images.

No access to this part. Use Member area to request membership. If you are already a member, login to Member area and come back to this wiki. |

## Plotting analytic functions

To draw a function, first create a function using a symbolic variable "x",
then create a canvas and then use **draw2d(o)** to plot the function.

You can set the X and Y ranges for the plotted functions. In this example we differentiate an analytic function and then plot in in the given range [0-10] in X and [-1-10] in Y:

Plotting of special functions is also possible; they all can accept not only a single value but vectors.

Here we plotted the error function.One can draw multiple functions, setting a new color, style and name for the second function:

It should be noted that functions can also be plotted by parsing a string:

## Plotting data

Let as show a simple example of plotting sin(x) between 0 and 10 with a step 0.1

You can draw the second plot with a different style:

## Plotting data with errors

You can plot data with errors. Errors are shown as lines, and can be attributed to Y-values (asymmetric upper and lower errors) and to X-values (left and right error). Errors can be asymmetric.

In this example, we plot data with symmetric errors on Y (which is 40% of the y-values). The array "z" represents symmetric errors on the Y-values.

In this example, we plot data with asymmetric errors on Y (which is 40% and 80% of the y-values). In this example, the upper error on Y is 40% of the value y, while lower error is 80% of the value y:

Now let us plot errors on X and Y. We assume that left error is 30% of X value, and right value is 60% of x value.

Finally, here is a custom example:

## Plotting options

You can pass the following options to the "draw2d" method:

No access to this part. Use Member area to request membership. If you are already a member, login to Member area and come back to this wiki. |

## Exporting plots into images

There are several methods associated with it: **clear2d** (clear the canvas from data)
and **export2d(file)**, to export
the plot to the vector format image, such as PDF, EPS, as well as PNG and JPG.
When exporting images to the file, use the file extension to define image format. For example,
export2d("image.pdf") indicates that the plot will be saved into the PDF format,
export2d("image.eps") tells that the plot will be saved into the high-quality vector format using EPS/PS format.

# Plot3D

You can plot 3D surface plots (in case of functions) or show data points in 3D using
**plot3D** method. The plots are fully interactive and one can rotate and zoom-in particular regions.

First, let us create a canvas and prepare for plotting of data in 3D.

There are several methods associated with it: **clear3d** (clear the canvas from data) and expert3d(file), to export the plot to the vector format image, such as PDF, EPS, as well as PNG and JPG. You can also put title, label and change the minimum and maximum ranges for X-axis and Y-axis.
You can pass the following options to this canvas:

No access to this part. Use Member area to request membership. If you are already a member, login to Member area and come back to this wiki. |

## Plotting functions in 3D

Functions can be shown as surface plots in 3D.
You will need to define symbolic variables x and y, and then
construct symbolic function. The pass it to the method **draw3d**:
Run this example to find how it works. It will show a pop-up canvas with a
surface plot in 3D:

Alternatively, one can pass a string which specifies a function using "x" and "y" as variables. Run this example to see how it works:

This generates interactive surface plot in 3D. The ranges of X,Y,Z can be specified as option for the method**plot3d**.

## Plotting data in 3D

Data may be graphed using the plot3d function as well. In this case you need to pass 3 vectors

Let as show a simple example of plotting sin(x) between 0 and 10 with a step 0.1

Note that it is convenient to set X,Y,Z ranges during the canvas creating step, in this case the axis labels will be nicely formatted (unlike the example above).

## Exporting plots to images

To clear the plot, use "clear3d" method. One can export the plot in PDF, EPS, PS, PNG, JPG image formats
using the method **export3d(file)** method. For example, export3d("image.eps") will export the graph to EPS/PS format using high-quality vector graphics.

## Drawing options

You can pass the following options to the "draw3d" method: