You are a guest. Restricted access. Read more

# 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.

`plot2d()`

There are several methods associated with it:

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

This box indicates that you are not a full member and cannot access this part of the documentation.

## 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.

```syms x
a=x*cos(x)
plot2d()
draw2d(a)```

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:

```syms x % symbolic x
y=diff(x*cos(x),x) %  differentiate x*cos(x)
plot2d('minx=0;maxx=10;miny=-1;maxy=10')  % make canvas
draw2d(y) % plot the result```

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

```x=0.01:0.05:1 % create a vector from 0.01 to 1 with step 0.005
y=erf(x)
printf('%f',y)
plot2d()  % make canvas
draw2d(x,y)```

Here we plotted the error function. Other functions are described in functions.

One can draw multiple functions, setting a new color, style and name for the second function:

```syms x
a=x*cos(x)
plot2d()
draw2d(a)
b=x*sin(x)
draw2d(b,'name=newfunction;linestyle=2;color=red;')```

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

```plot2d()
draw2d('x*cos(x)')```

## Plotting data

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

```x=0:0.1:10; y=sin(x)
plot2d('titlex=X;titley=Y;')
draw2d(x,y)
draw2d(x,y*2,'name=newdata;')```

You can draw the second plot with a different style:

```x=0:0.1:10; y=sin(x)
plot2d()
draw2d(x,y)
draw2d(x,y*2,'style=l;color=blue;')```

## 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.

```x=0:1:10; y=sin(x);
z=ones(1,length(x)) * 0.3 % vector with errors. All have 0.3 value
plot2d()
draw2d(x,y,z)```

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:

```x=0:1:10; y=sin(x);
z=ones(1,length(x))  % errors with value 1
plot2d()
draw2d(x, y, 0.4*z, 0.8*z) % errors are asymmetric```

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.

```x=0:1:10; y=10*sin(x);
z=ones(1,length(x))  % errors with value 1
plot2d()
draw2d(x, y, 0.3*z, 0.6*z, 3*z, 5*z)```

Finally, here is a custom example:

```x=[1, 2]; y=[10, 20]
exLeft=[0.5, 0.5]
exRight=[0.3, 0.3]
eyUpper=[5, 5]
eyLower=[2, 2]
plot2d()
draw2d(x, y, exLeft, exRight, eyUpper, eyLower)```

## Plotting options

You can pass the following options to the “draw2d” method:

This box indicates that you are not a full member and cannot access this part of the documentation.

## Displaying histograms

You can use the draw2d() method to display histograms. Read the section Histograms to learn how to create histograms.

## 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.