Graphing with Octave, Octave Kernel and Ipython Notebook

Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5334
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

GNU Octave is an open source software featuring a high-level programming language. Octave is very useful in solving linear and nonlinear problems numerically, and for performing other numerical computations using a language that is mostly compatible with MATLAB, see an example.

If you're using a Linux-based operating system, such as Ubuntu, the Octave PPA is maintained and supported by Octave developers, so you can install the latest stable version of GNU Octave by executing the following lines one by one on the Terminal:

  1. sudo apt-add-repository ppa:octave/stable
  2. sudo apt-get update
  3. sudo apt-get install octave


GNU Octave is a great software for producing high quality plots. In this simple tutorial, we will use Octave, Ipython notebook and Octave kernel for Jupyter to show how this combination of software provides more graphing control and capabilities. The easiest way to install Ipython notebook is by installing Anaconda distribution, which you can do by following instructions here.

We recommend that, you install Octave kernel with pip, just by simply executing the following command in the Terminal:

$pip install octave_kernel

After Installing Ipython notebook and Octave kernel, you can now respectively; use Ipython Notebook, Ipython console and Ipython qtconsole, which you can evoke as follows.

Ipython notebook:

$ipython notebook

After launching Ipython notebook, in its interface, select Octave from the 'New' menu or from the File --> New Notebook.

Ipython console:

$ipython console --kernel octave

Ipython qtconsole:

$ipython qtconsole --kernel octave

Alright, now let's use Ipython notebook to do some graphing.


1. Vertical lines with random heights

  1. x = 0:15;
  2. plot (repmat (x, 2, 1), rand (2, numel (x)), "-s");
  3. axis ([0 10 0 1]);
  4. xlabel("X Axis");
  5. ylabel("Y Axis")
  6. title ("Vertical bars with random heights");
  7. saveas(1, "vertical_bars.png");


Running the code above will result to the figure

Image


2. Trigonometric functions

  1. x = 0:0.1:2*pi;
  2. plot(x, sin(x), "-", x, 1 + sin(x), "--", x, cos(x), ":");
  3. xlabel("<-- X -->");
  4. ylabel("<-- Y -->");
  5. legend("sine", "1 + sine", "cosine");
  6. title("Trigonometric functions");
  7. saveas (1, "sine_cosine.png");

Image

  1. t = linspace(0,6*pi,100);
  2. plot(sin(t))
  3. %grid on
  4. hold on
  5. plot(cos(t), 'r')
  6. saveas (1, "sine_waves.png");
  7. title("Sine waves")


Image


3. Sinusoidal

  1. x = -10:0.1:10;
  2. y = 10:-0.1:-10;
  3. z = x .* sin(x - y);
  4. plot3(x, y, z, "-", "LineWidth", 4);
  5. xlabel("X ->");
  6. ylabel("Y ->");
  7. zlabel("Z ->");
  8. title("Sinusoidal");
  9. set(gca, "linewidth", 4, "fontsize", 12);
  10. grid("on");
  11. saveas(1, "Sinusoidal.png")


Image


4. Surfaces

  1. % A 2-D grid with uniformly spaced x-coordinates and y-coordinates in the interval [-2,2]
  2. x = -2:0.25:2;
  3. y = x;
  4. [X,Y] = meshgrid(x);
  5. F = X.*exp(-X.^2-Y.^2);
  6. surf(X,Y,F)
  7. set(gca, "linewidth", 4, "fontsize", 12);
  8. saveas(1, "surface.png")
  9. grid on

Image
With grids off:

  1. % Surface - A 2-D grid with uniformly spaced x-coordinates and y-coordinates in the interval [-2,2]
  2. x = -2:0.25:2;
  3. y = x;
  4. [X,Y] = meshgrid(x);
  5. F = X.*exp(-X.^2-Y.^2);
  6. surf(X,Y,F)
  7. set(gca, "linewidth", 4, "fontsize", 12);
  8. grid("off")
  9. saveas(1, "no_grid_surface.png")

Image

5. 3-Dimensional Graphs

  1. x = 0:0.1:2*pi;
  2. y = 0:0.1:2*pi;
  3. z = sin(x)' * sin(y);
  4. mesh(x, y, z);
  5. xlabel("<-- X ");
  6. ylabel("<-- Y ");
  7. zlabel("<-- Z");
  8. title("3-Dimensional waves");
  9. saveas(1, "3_D_waves.png")

Image
  1. tx = ty = linspace (-8, 8, 41)';
  2. [xx, yy] = meshgrid (tx, ty);
  3. r = sqrt (xx .^ 2 + yy .^ 2) + eps;
  4. tz = sin (r) ./ r;
  5. mesh (tx, ty, tz);
  6. grid on
  7. xlabel("X Axis")
  8. ylabel("Y Axis")
  9. zlabel("Z Axis")
  10. title("3-D Waves")
  11. saveas(1, "3_D_waves2.png")

Image

These plots can be greatly enhanced if we use Gnuplot as a plotting engine instead of a default Octave functionality.
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5334
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#2

You can use Gnuplot as a plotting engine for Octave by including it as follows:

  1. graphics_toolkit("gnuplot")


Or you can configure Octave to use Gnuplot by default by adding the command to .octaverc file. See more>>>
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply

Return to “MATLAB/Octave”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 5 guests