Octave:Plotting

From AIMSWiki

WARNING: This page is still under heavy construction
Table of contents

2D plots

plot(y)

If a single data argument is supplied, it is taken as the set of Y coordinates and the X coordinates are taken to be the indices of the elements, starting with 1.

plot(x, y)
  • If the first argument is a vector and the second is a matrix, the vector is plotted versus the columns (or rows) of the matrix. (using whichever combination matches, with columns tried first.)
  • If the first argument is a matrix and the second is a vector, the the columns (or rows) of the matrix are plotted versus the vector. (using whichever combination matches, with columns tried first.)
  • If both arguments are vectors, the elements of Y are plotted versus the elements of X.
  • If both arguments are matrices, the columns of Y are plotted versus the columns of X. In this case, both matrices must have the same number of rows and columns and no attempt is made to transpose the arguments to make the number of rows match.

Non-linear plots

semilogx, semilogy, loglog, polar

Formatting

    `-'
          Set lines plot style (default).

    `.'
          Set dots plot style.

    `@'
          Set points plot style.

    `-@'
          Set linespoints plot style.

    `^'
          Set impulses plot style.

    `L'
          Set steps plot style.

    `N'
          Interpreted as the plot color if N is an integer in the range
          1 to 6.

    `NM'
          If NM is a two digit integer and M is an integer in the range
          1 to 6, M is interpreted as the point style.  This is only
          valid in combination with the `@' or `-@' specifiers.

    `C'
          If C is one of `"r"', `"g"', `"b"', `"m"', `"c"', or `"w"',
          it is interpreted as the plot color (red, green, blue,
          magenta, cyan, or white).

    `";title;"'
          Here `"title"' is the label for the key.

    `+'
    `*'
    `o'
    `x'
          Used in combination with the points or linespoints styles,
          set the point style.

     The color line styles have the following meanings on terminals that
     support color.

          Number  Gnuplot colors  (lines)points style
            1       red                   *
            2       green                 +
            3       blue                  o
            4       magenta               x
            5       cyan                house
            6       brown            there exists

     The FMT argument can also be used to assign key titles.  To do so,
     include the desired title between semi-colons after the formatting
     sequence described above, e.g. "+3;Key Title;" Note that the last
     semi-colon is required and will generate an error if it is left
     out.


Some examples of using these formats is given below:

plot(data(:,1), '*', "markersize", 15, data(:,5), 'o', "markersize", 15);

plot (x, y, "@12", x, y2, x, y3, "4", x, y4, "+")

A Plot Example Script

This script will plot a Fourier Series of a square wave.

%This script will plot a Fourier Series of a square wave.
clf;
t=0:.01:10;
T=2.5
M=10	
sum1=0;
for m=1:2:M,
	sum1 = sum1+4/m/pi*sin(2*pi*m*t/T);
end
plot(t,sum1,'b-',t(1:10:end),sum1(1:10:end),'r*')
title('Fourier Series Representation of a Square Wave')
xlabel('time (seconds)')
ylabel('Function')
grid on;
axis([0,10,-2,2])
legend('Five Terms','Five Terms Sampled')
print("squarewave.png","-dpng")  % Prints the plot to a png file called squarewave.png

Image:squarewave.png

Commands related to plot

xlabel -This command followed by ('whatever title you want here') put the "whatever title you want here" on your x axis label.

ylabel zlabel -These commands are the same as the xlabel except for the y and z axes.

title -This command is similar to xlabel and ylabel. Type: title('Put your title here') and that title will show in your plot window.

replot -This command refreshes the plot window, use it in case your plot doesn't work the first time.

GNUplot commands in Octave

legend('Title') - creates a legend for the various data sets; first entry is the first entry in the plot() command; has format ('Title1','Title2', . . .). Adding a legend('boxon') below legend creates a box around the legend (higher visibility). (see above example)

tics('Axis', tick_location, labels) - changes the tick marks on the specified axis (either 'x', 'y', or 'z'). tick_location is a 1xN array of numbers. If no labels are given, will use numbers as labels. labels is the labels of the tick_location, with format ['Label1';'Label2'; . . .]. If fewer labels than tick_locations are given, the labels will restart with the first entry.


Note: It is important that you place these commands on the lines after your "plot" command.

3D plots

mesh, meshgrid, surf

Using plot3

This sample code creates an inverted cone.

t = 0:0.1:500*pi;
r = linspace (0, 1, numel (t));
z = linspace (0, 1, numel (t));
plot3 (r.*sin(t), r.*cos(t), z);

Using gnuplot

plot the Lorenz functions in 3d

The Lorenz functions are a set of continous functions. Using the lsode built in function we can get the solution for the functions

[X,s1,s2] = lsode("function",X_0,tspan)
  • the first entry is a string with the name of a function which returns the equations to be solved and it should be of the format function(X_0,tspan)
  • the second corresponds to the initial conditions at time t=0
  • the third entry corresponds to the initial and final value of the time

Contour plots

contour

Images

colormap, image, imshow, hsv2rgb


Plotting Color plots in octave

Octave can be used to produce many types of plots. You can even use it to make color plots such as a heat map.

Below is some sample code that is the bare minimum you can use to make a heat map plot. However, it does require that you input your own data. Currently, at the end of the script is a command to print the plot to a txt file.

%The below script will plot a heat map once you input your data
x_values = [0.10 : 0.005 : 0.60];
y_values = [0.10 : 0.005 : 0.60];
contourf(x_values, y_values, data); % supply your own data...
axis square;
colorbar;
xlim([0.1 0.6]);
ylim([0.1 0.6]);
print('-dtex', 'my_plot.tex');


Enjoy

Saving and printing graphs

Print a graph to a printer or save it to a file

print("-Pprinter")
print("filename", "-ddevice")

Devices:

         `ps'
         `ps2'
         `psc'
         `psc2'
               Postscript (level 1 and 2, mono and color)

         `eps'
         `eps2'
         `epsc'
         `epsc2'
               Encapsulated postscript (level 1 and 2, mono and color)

         `ill'
         `aifm'
               Adobe Illustrator

         `cdr'
         `corel'
               CorelDraw

         `hpgl'
               HP plotter language

         `fig'
               XFig

         `dxf'
               AutoCAD

         `mf'
               Metafont

         `png'
               Portable network graphics

         `pbm'
               PBMplus

If the device is omitted, it is inferred from the file extension, or if there is no filename it is sent to the printer as postscript.


Return to the Octave index