This file gives an overview of the various plots in Euler. Have a look into the following notebooks for more examples. 02 - Introduction to 2D Graphics 03 - Introduction to 3D Graphics Euler has 2D and 3D plots. We start with 2D plots.
The most important plotting function for planar plots is plot2d. The function is implemented in the Euler language in the file "plot.e", which is loaded at the start of the program. plot2d accepts expressions, functions and data. The plot range is set with the following assigned parameters a,b - x-range (default -2,2) c,d - y-range (default: scale with values) r - alternatively a radius around the plot center cx,cy - the coordinates of the plot center (default 0,0) Here is the most basic example, which used the default range, and makes the y-range fit the plot of the function. If you end a command line with a colon ":", the plot will be inserted into the text window.
>plot2d("x^2"):
An alternative is the command insimg(lines), which inserts the plot occupying a specified number of text lines. To see the full version, press the tabulator key, or use the screen layout with a separate plot window (disable: Extras - Graphics in Text Window)
>plot2d("x^3-x",0,2); insimg(20);
The images in the notebook (including the Latex formulas etc.) are stored in the same directory as the notebook, by default in a subdirectory named "images". They are also used by the HTML export of the notebook. You can simply mark any images and copy it to the clipboard with Ctrl-C. The function or the expression in plot2d is evaluated adaptively. For more speed, switch off adaptive plots with <adaptive and specify the number of subintervals with n=... This should be necessary in rare cases only. The following works even as an adaptive plot.
>plot2d("sign",-1,1):
By default the image in the graphics window is square. This yields exact scales, if a square plot window is used with the parameter r, and an optional plot center cx,cy.
>plot2d("x^x",r=1.2,cx=1,cy=1):
Note that x^x is not defined for x<=0. The plot2d function catches this error, and starts plotting as soon as the function is defined. This works for all function, which return NAN out of their range of definition.
>plot2d("log(x)",-0.1,2):
For other plot ranges, the parameter square=true (or >square) selects a the y-range automatically so that the result is a square plot window. Note that by default, Euler uses a square space inside the plot window.
>plot2d("x^3-x",>square):
If you need another format for your image, you can - disable "Graphics in Text Window" in Extras, - switch off the "Square Graphics" flag in the graphics settings, - resize the graphics window to another format. - and enter "reset" or restart Euler. A better method is - to set the aspect window, - and to crop graphics to this window using the file menu. This will save the graphics with that crop. The function "insimg" automatically takes care of the selected aspect window, or you select a proper crop (from the top of the window) with the crop parameter.
>aspect(2); plot2d("x^3-x",-1,1); insimg(); aspect;
The function plot2d will also accept the names of numerical or symbolic functions.
>function f(x) := x^3-x >plot2d("f",r=1):
Here is a summary of the accepted functions - expressions or symbolic expressions in x - function names as "f" - symbolic functions just by the name f For symbolic functions, the name alone works.
>function f(x) &= diff(x^x,x)
x x (log(x) + 1)
>plot2d(f,0,2):
Of course, for expressions or symbolic expressions the name of the variable is enough to plot them.
>expr &= sin(x)*exp(-x)
- x E sin(x)
>plot2d(expr,0,3pi):
For the line style there are various options. - style="...". Select from "-", "--", "-.", ".", ".-.", "-.-". - color: See below for colors. - thickness: Default is 1. Colors can be selected as one of the default colors, or as an RGB color. - 0..15: the default color indices. - color constants: white, black, red, green, blue, cyan, olive, lightgray, gray, darkgray, orange, lightgreen, turquoise, lightblue, lightorange, yellow - rgb(red,green,blue): parameters are reals in [0,1].
>plot2d("exp(-x^2)",r=2,color=red,thickness=3,style="--"):
To plot more than one function into one window, call plot2d one more time, and use >add.
>plot2d("cos(x)",r=2); plot2d("x",style=".",>add):
Alternatively, a vector of strings can be used.
>plot2d(["sin(x)","cos(x)"],0,2pi):
Another alternative is to use the matrix language of Euler. If an expression produces a matrix of functions, with one function in each row, all these functions will be plotted into one plot. For this, use a parameter vector in form of a column vector.
>n=(1:10)'; plot2d("x^n",0,1):
Expressions and one-line functions can see global variables. If you cannot use a global variable, you need to use a function with an extra parameter, and pass this parameter as a semicolon parameter. Take care, to put all assigned parameters to the end of the plot2d command. In the example we pass a=5 to the function f, which we plot from -10 to 10.
>function f(x,a) := 1/a*exp(-x^2/a) >plot2d("f",-10,10;5,thickness=2,title="a=5"):
The following parameters produce logarithmic plots. logplot=1: y-logarithmic logplot=2: x-y-logarithmic logplot=3: x-logarithmic
>plot2d("exp(x^2)+exp(2*x)",1,10,logplot=1):
Simple decorations can be - a title with title="..." - x- and y-labels with xl="...", yl="..." - another text label with label("...",x,y) The label command will plot into the current plot at the plot coordinates (x,y).
>plot2d("log(x)/x",0.5,5,title="y=log(x)/x",xl="x",yl="y"); ... label("(1,0)",1,0):
There is also the function labelbox(), which can show the functions and a text. It takes vectors of strings and colors, one item for each function.
>function f(x) &= x^2*exp(-x^2); ... plot2d(&f(x),a=-3,b=3,c=-1,d=1); ... plot2d(&diff(f(x),x),>add,color=blue,style="--"); ... labelbox(["function","derivative"],styles=["-","--"],colors=[black,blue]):
The box is anchored at the top right by default. But you can move it to any place you like. The anchor position is the top right corner of the box, and the numbers are fractions of the size of the graphics window. The width is automatic. For point plots, the labelbox works too. Add a parameter >points, or a vector of flags, one for each label. In the following example, there is only one function. So we can use strings instead of vectors of strings. We set the text color to black for this example.
>n=10; plot2d(0:n,bin(n,0:n)); plot2d(0:n,bin(n,0:n),>points,>add); ... labelbox("Binomials",styles="[]",>points,x=0.3,y=0.1,tcolor=black):
A similar feature is the function textbox(). The width is by default the maximal widths of the text lines. But it can be set by the user too.
>function f(x) &= exp(-x)*sin(2*pi*x); ... plot2d("f(x)",0,2pi); ... textbox(["Example of a damped oscillation",&f(x)],w=0.5):
We already mentioned the >user flag, which works for plot2d and plot3d. The user can - zoom with + or - - move the plot with the cursor keys - select a plot window with the mouse - reset the view with space - exit with return
>plot2d("exp(x)*sin(x)",user=true, ... title="+/- or cursor keys (return to exit)"):
The built-in function mousedrag() waits for mouse or keyboard events. It reports mouse down, mouse moved or mouse up, and key presses. The function dragpoints() makes use of this, and lets the user drag any point in a plot. We need a plot function first. For an example, we interpolate in 5 points with a polynomial. The function should plot into a fixed plot area.
>function plotf(xp,yp,select) ...
d=interp(xp,yp); plot2d("interpval(xp,d,x)";d,xp,r=2); plot2d(xp,yp,>points,>add); if select>0 then plot2d(xp[select],yp[select],color=red,>points,>add); endif; title("Drag one point, or press space or return!"); endfunction
Note the semicolon parameters in plot2d, d and xp, which are passed to the evaluation of the interp() function. Without this, we could write a plotinterp function first, accessing the values globally. Now we generate some random values, and let the user drag the points.
>t=-1:0.5:1; dragpoints("plotf",t,random(size(t))-0.5):
There is also a function, which plots another function depending on a vector of parameters, and lets the user adjust these parameters. First we need the plot function.
>function plotf([a,b]) := plot2d("exp(a*x)*cos(2pi*b*x)",0,2pi;a,b);
Then we need names for the parameters, initial values and a nx2 matrix of ranges, optionally a heading line.
>dragvalues("plotf",["a","b"],[-1,2],[[-2,2];[1,10]], ... heading="Drag these values:",hcolor=black):
If you want to use mousedrag for your own function, you need to refer to the reference for details. mousedrag function The following is a simple demonstration of the function. The user can draw over the plot window, leaving a trace of points.
>function dragtest ...
plot2d(none,r=1,title="Drag with the mouse, or press any key!"); start=0; repeat {flag,m,time}=mousedrag(); if flag==0 then return; endif; if flag==2 then hold on; mark(m[1],m[2]); hold off; endif; end endfunction
>dragtest
Plotting an expression is only an abbreviation for data plots. From the range and the x-values the plot2d function will compute the data to plot, adaptively by default. But you can enter data directly. - Use row vectors for x and y for one function. - Matrices for x and y are plotted line by line. Here is an example with one row for x and y.
>x=-10:0.1:10; y=exp(-x^2)*x; >plot2d(x,y):
In the following example, we generate the Bernstein-Polynomials.
We generate a matrix of values with one Bernstein-Polynomial in each row. For this, we simply use a column vector of i. Have a look into the introduction notebook about the matrix language to learn more details.
>x=0:0.01:1; n=10; i=(0:n)'; y=bin(n,i)*x^i*(1-x)^(n-i); ... plot2d(x,y):
The x-values need not be sorted. (x,y) simply describes a curve. If x is sorted, the curve is a graph of a function. In the following example, we plot the spiral
>t=linspace(0,1,1000); ... plot2d(t*cos(2*pi*t),t*sin(2*pi*t),r=1):
Alternatively, it is possible to use two expressions for curves. The following plots the same curve as above.
>plot2d("x*cos(2*pi*x)","x*sin(2*pi*x)",xmin=0,xmax=1,r=1);
Data plots are really polygons. In the following example we plot a hexagon using a closed curve with 7 points. - filled=true fills the plot. - style="...": Select from "#", "/", "\", "\/". - fillcolor: See above for available colors.
>t=linspace(0,2pi,6); ... plot2d(cos(t),sin(t),>filled,style="/",fillcolor=red,r=1.2):
Data can also be plotted as points. Use points=true for this. The plot works like polygons, but draws only the corners. - style="...": Select from "[]", "<>", "o", ".", "..", "+", "*", "[]#", "<>#", "o#", "..#", "#", "|".
>n=10; i=0:n; >plot2d(i,bin(n,i)/2^n,a=0,b=10,c=0,d=0.3); >plot2d(i,bin(n,i)/2^n,points=true,style="ow",add=true,color=blue); >insimg;
Moreover, data can be plotted as bars. In this case, x should be sorted and one element longer than y. The bars will extend from x[i] to x[i+1] with values y[i]. If x has the same size as y, it will be extended by one element with the last spacing. Fill styles can be used just as above.
>n=10; k=bin(n,0:n); ... plot2d(-0.5:n+0.5,k,bar=true,fillcolor=lightgray):
For distributions, there is the parameter distribution=n, which counts values automatically and prints the relative distribution with n sub-intervals.
>plot2d(normal(1,1000),distribution=10,style="\/"):
With the parameter even=true, this will use integer intervals.
>plot2d(intrandom(1,1000,10),distribution=10,even=true); ... insimg;
Note that there are many statistical plots, which might be useful. Have a look at the introduction to statistics.
>columnsplot(getmultiplicities(1:6,intrandom(1,6000,6))); ... insimg;
A vector of complex numbers is automatically plotted as a curve in the complex plane with real part and imaginary part. In the example, we plot the unit circle with
>t=linspace(0,2pi,1000); ... plot2d(exp(I*t)+exp(4*I*t),r=2):
A matrix of complex numbers will automatically plot as a grid in the complex plane. In the example, we plot the image of the unit circle under the exponential function.
>r=linspace(0,1,20); t=linspace(0,2pi,80)'; z=exp(r*exp(I*t)); ... plot2d(z,a=0,b=4,c=-2,d=2):
A vector of intervals is plotted against x values as a filled region between lower and upper values of the intervals. This is can be useful to plot errors of the computation. But it can also be used to plot statistical errors.
>t=0:0.1:1; ... plot2d(t,interval(t-random(size(t)),t+random(size(t))),style="|"); ... plot2d(t,t,add=true); ... insimg;
For functions of two variables use plot3d. This function can plot functions, surfaces or data. For the graph of a function, use - a simple expression in x and y, - or a function of two variables. The default is a filled wire grid with different colors on both sides.
>plot3d("x^2+y^2"):
User interaction is possible with the user=true parameter. The user can press the following keys. - left,right,up,down: turn the viewing angle - +,-: zoom in or out - a: produce an anaglyph (see below) - l: toggle turning the light source (see below) - space: reset to default - return: end interaction
>plot3d("exp(-x^2+y^2)",user=true, ... title="Turn with the vector keys (press return to finish)");
The plot range for functions can be specified with - a,b: the x-range - c,d: the y-range - r: a symmetric square around (0,0). - n: number of subintervals for the plot. For symbolic or numerical functions either use a semicolon parameter or a global variable.
>function f(x,y,a) := 1/a*exp(-(x^2+y^2)/a)
We pass the additional parameter 5 as semicolon parameter to f. Moreover, we turn of function scaling. Normally, all funcitons are scaled so that the plot does not look too flat or high. We scale the complete plot a bit.
>plot3d("f";5,r=10,n=40,fscale=4,scale=1.4):
The view can be changed in many different ways. - distance: the viewing distance to the plot. - zoom: the zoom value. - angle: the angle to the negative y-axis in radians. - height: the height of the view in radians. The default values can be inspected or changed with the function view. It returns the parameters in the order above.
>view
[ 5 2.5 2 0.4 ]
A closer distance needs less zoom. The effect is more like a wide angle lens. In the following example, angle=0 and height=0 look from the negative y-axis. The axis labels for y are hidden in this case.
>plot3d("x^2+y",distance=3,zoom=1.5,angle=0,height=0):
You might have noticed that the plot looks always to the center of the plot cube. We can move the center with the center parameter.
>plot3d("x^4+y^2",a=0,b=1,c=-1,d=1,angle=-20°,height=20°, ... center=[0.8,0,0],zoom=2.5):
The plot is scaled to fit into a unit cube for viewing. So there is no need to change the distance or zoom depending on the size of the plot. The labels refer to the actual size, however. If you turn this off with scale=false, you need to take care, that the plot still fits into the plotting window, by changing the viewing distance or zoom, and moving the center.
>x=-2:0.1:2; y=x'; ... plot3d(x,y,5*exp(-x^2-y^2),r=2,<scale,distance=13,height=50°, ... center=[0,0,-2]):
Plot of functions are scaled automatically in z-direction, so that they look nice. But this can be turned off with fscale=false.
>plot3d("exp(-x^2-y^2)",r=2,fscale=false,zoom=3.5,height=60°):
The parameter polar=true selects a polar plot with rotating image.
>plot3d("x^2+y^2",polar=true,r=1):
The parameter rotate rotates a function in x around the x-axis. - rotate=1: Uses the x-axis - rotate=2: Uses the z-axis
>plot3d("x^2+1",a=-1,b=1,rotate=true):
Here are two more parameters. frame=false: turns off the frame around the plot. wire=true: does not fill the plot.
>plot3d("sqrt(x^2+1)",a=0,b=10,rotate=2,frame=false, ... wire=true,zoom=4.5):
Anaglyph plots for red/cyan glasses can be produced with the parameter anaglyph=true. The 3D effect is amazing, but you need red/cyan glasses to appreciate it.
>plot3d("sin(x)*cos(y)",r=pi,anaglyph=true); insimg(35);
Euler can draw the heights of functions on a plot with real light shading. - hue=true: Turns on light shading instead of wires. - contour=true: Plots automatic contour lines on a plot. - level: A vector of values for the contour lines.
>plot3d("x*y",contour=true):
Instead of a shading, you can use spectral colors. By default, the z-values of the plot are used.
>plot3d("x^2+y^3",>contour,>spectral,angle=30°):
Euler can plot specific contour lines, or a single contour line. In the following example, we plot the set, where
i.e., the set
>plot3d("x^y-y^x",level=0,a=0,b=10,c=0,d=10, ... contourwidth=2,contourcolor=red,n=100):
The light source can be changed with l and the cursor keys during the user interaction. It can also be set with parameters. light: a direction for the light amb: ambient light between 0 and 1 Note that the program does not make a difference between the sides of the plot. There are no shadows. For this you would need Povray.
>plot3d("-x^2-y^2", ... hue=true,light=[0,1,1],amb=0,user=true, ... title="Press l and cursor keys (return to exit)"):
The color parameter changes the color of the surface. You can use any color as described above.
>plot3d("-x^2-y^2",color=rgb(0.2,0.2,0),hue=true,frame=false, ... zoom=3):
The color 0 gives a special rainbow effect.
>plot3d("x^2/(x^2+y^2+1)",color=0,hue=true):
Contour plots are also possible in the plane using plot2d. Such plots plot the solutions of functions of two variables. Let us plot the set
We can add a shading to the plot, which indicated large and small values, and we can change the contourcolor and the contour width.
>plot2d("x^2+y^3",level=1,r=1.5,hue=true, ... contourcolor=yellow,contourwidth=3):
We can add other contour lines using add=true.
>plot2d("x^2+y^3",level=-10:10,add=true,contourcolor=yellow):
There are also implicit plots in three dimensions. The solutions of
can be visualized in cuts parallel to the x-y-, the x-z- and the y-z-plane. implicit=1: cut parallel to the y-z-plane implicit=2: cut parallel to the x-z-plane implicit=4: cut parallel to the x-y-plane Add these values, if you like. In the example we plot
>plot3d("x^2+y^3+z*y-1",r=5,implicit=3):
Just as plot2d, plot3d accepts data. You need to specify matrices x,y,z with the coordinates of the plane.
Since x,y,z are matrices, we assume that (t,s) run through a square grid. As a result, you can plot images of rectangles in space. You can use the Euler matrix language to produce the coordinates effectively. Here is an example, which is the graph of a function.
>t=-1:0.1:1; s=(-1:0.1:1)'; ... plot3d(t,s,t*s):
However, we can make all sorts of surfaces. Here is the same surface as a function
>plot3d(t*s,t,s,angle=180°):
With more effort, we can produce many surfaces. In the following example we make a shaded view of a distorted ball. The usual coordinates for the ball are
with
We distored this with a factor
>t=linspace(0,2pi,320); s=linspace(-pi/2,pi/2,160)'; ... d=1+0.2*(cos(4*t)+cos(8*s)); ... plot3d(cos(t)*cos(s)*d,sin(t)*cos(s)*d,sin(s)*d,hue=1, ... light=[1,0,1],frame=0,zoom=5):
To plot point data in the space, we need three vectors for the coordinates of the points. The styles are just as in plot2d with points=true;
>n=500; ... plot3d(normal(1,n),normal(1,n),normal(1,n),points=true,style="."); ... insimg;
For curves in the plane we use a sequence of coordinates and the parameter wire=true.
>t=linspace(0,4pi,1000); ... plot3d(cos(t),sin(t),t/2pi,>wire):
Bar plots are possible too. For this, we have to provide x: row vector with n+1 elements y: column vector with n+1 elements z: nxn matrix of values. z can be larger, but only nxn values will be used. In the example, we first compute the values. Then we adjust x and y, so that the vectors center at the values used.
>x=-1:0.1:1; y=x'; z=x^2+y^2; ... xa=(x|1.1)-0.05; ya=(y_1.1)-0.05; ... plot3d(xa,ya,z,bar=true):
It is possible to split the plot of a surface in two or more parts.
>x=-1:0.1:1; y=x'; z=x+y; d=zeros(size(x)); ... plot3d(x,y,z,disconnect=2:2:20):
The plot3d function is nice to have, but it does not satisfy all needs. Besides more basic routines, it is possible to get a framed plot of any object you like. Though Euler is not a 3D program, it can combine some basic objects. We try to visualize a paraboloid and its tangent.
>function myplot ...
y=0:0.05:1; x=(0.1:0.05:1)'; plot3d(x,y,0.2*(x-0.1)/2,<scale,<frame,>hue, .. hues=0.5,color=orange); h=holding(1); plot3d(x,y,(x^2+y^2)/2,<scale,<frame,>hue); holding(h); endfunction
Now framedplot provides the frames, and sets the views.
>framedplot("myplot",[0.1,1,0,1,0,1],angle=-45°, ... center=[0,0,-0.7],zoom=6):
plot2d and plot3d are built on more basic plot routines. You might need these basic routines for speedy animations. Note, however, that Euler uses a buffer to store all its graphics, which cannot be disabled, since it is used for insimg() and for graphics export. More information on the basic plot routines are contained in the core documentation of Euler, and in some Euler files. ../reference/eulercore ../reference/plothelp ../reference/framed ../reference/plot If you decide to write your own plotting stuff, you can use the following function for a start. It plots a star like shape with equal spaced angles given a vector of radii. Optionally, the user can change the color and the fill style, and it is possible to add labels. Note, that there is already a function starplot in Euler. We do not assume that hold is off, since the function might be used in a split window. Then clg would delete the rest of the plots. Moreover, we carefully restore all styles and colors.
>function starplot1 (v, style="/", color=green, lab=none) ...
if !holding() then clg; endif; w=window(); window(0,0,1024,1024); h=holding(1); r=max(abs(v))*1.2; setplot(-r,r,-r,r); n=cols(v); t=linspace(0,2pi,n); v=v|v[1]; c=v*cos(t); s=v*sin(t); cl=barcolor(color); st=barstyle(style); loop 1 to n polygon([0,c[#],c[#+1]],[0,s[#],s[#+1]],1); if lab!=none then rlab=v[#]+r*0.1; {col,row}=toscreen(cos(t[#])*rlab,sin(t[#])*rlab); ctext(""+lab[#],col,row-textheight()/2); endif; end; barcolor(cl); barstyle(st); holding(h); window(w); endfunction
There is no grid or axis ticks here. Moreover, we use the full window for the plot. We call reset before we test this plot to restore the graphics defaults. This is not necessary, if you are sure that your plot works.
>reset; starplot1(normal(1,10)+5,color=red,lab=1:10):
Sometimes, you may want to plot something that plot2d cannot do, but almost. In the following function, we do a logarithmic impulse plot. plot2d can do logarithmic plots, but not for impulse bars.
>function logimpulseplot1 (x,y) ...
{x0,y0}=makeimpulse(x,log(y)/log(10)); plot2d(x0,y0,>bar,grid=0); h=holding(1); frame(); xgrid(ticks(x)); p=plot(); for i=-10 to 10; if i<=p[4] and i>=p[3] then ygrid(i,yt="10^"+i); endif; end; holding(h); endfunction
Let us test it with exponentially distributed values.
>x=1:10; y=-log(random(size(x)))*200; ... logimpulseplot1(x,y):
Let us animate a 2D curve using direct plots. The plot(x,y) command simply plots a curve into the plot window. setplot(a,b,c,d) sets this window. The wait(0) function forces the plot to appear on the graphics windows. Otherwise, the redraw takes place in sparse time intervals.
>function animliss (n,m) ...
t=linspace(0,2pi,500); f=0; c=framecolor(0); l=linewidth(2); setplot(-1,1,-1,1); repeat clg; plot(sin(n*t),cos(m*t+f)); wait(0); if testkey() then break; endif; f=f+0.02; end; framecolor(c); linewidth(l); endfunction
Press any key to stop this animation.
>animliss(2,3);
If the animation is too complicated, e.g. if it takes too much time to compute, Euler can use frames to pre-compute the animation. One function, which makes use of this technique is rotate. It can change the angle of view and redraw a 3D plot. The function calls addpage() for each new plot. Finally it animates the plots. Please study the source of rotate to see more details. >type rotate
>function testplot () := plot3d("x^2+y^3"); >rotate("testplot");