Octave plot Command [ 2024 ]
Octave plot Command
In this tutorial, we will learn about the Octave plot command with examples. The plot command is the basic command for generating 2D plots in Octave.
plot Command Syntax
The basic syntax for the command is as follows:
plot(x,y)
Optionally, we can provide many options to the command:
plot(x,y,[options])
x and y are coordinates scalars or vectors. When the x and y are scalars the command plots a single point. If x and y are vectors the command plots all the points joining them with straight lines in a figure window.
The x, y vectors must be of the same length. An error will occur if the vectors are not the same length.
Example
% Example plot command demo % Octave Tutorials - www.TestingDocs.com clear all; clf; % Generate x vector x = linspace(0,4*pi,100); % Generate y vector y = cos(x); % 2D-plot plot(x,y); % Labels and Title for the graph xlabel('x-axis'); ylabel('y-axis'); title('Sample Octave Plot cos(x)');
clear all;
This command clears all the existing variables from the workspace. The practice of clearing the workspace at the start is highly recommended. It also frees up the system memory.
clf;
This command clears the current figure window.
linspace
linspace is a built-in function. The function returns a row vector with N linearly spaced elements between the START and the END.
linspace (START, END, N)
xlabel(‘string’);
The xlabel command will display the label along the x-axis in the figure.
ylabel(‘string’);
The ylabel command will display the label along the y-axis in the figure.
title(‘string’);
The title command will display the title for the figure.
Screenshot
plot
The plot command creates the plot. In Octave, the default plotting style is a thin blue line connecting the points. To change the plot’s appearance, we can add another argument to the plot command.
For example, to change the plot to appear as a thin red line with x marks:
plot(x,y,’r-x’)
hold on;
This command will allow us to plot multiple plots within the same figure window.
grid on;
The grid command will make a grid appear in the figure window.
Octave Tutorials
Octave Tutorial on this website can be found at:
More information on Octave can be found on the official website: