6

What it the best way to plot a vertical line using Octave?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Austin A
  • 2,990
  • 6
  • 27
  • 42

3 Answers3

7

So, I have two methods for this. One, I found, and the other I made up.

Method 1: From here.

%% Set x value where verticle line should intersect the x-axis.
x = 0;
%% plot a line between two points using plot([x1,x2],[y1,y2])
plot([x,x],[-10,10]);

Method 2: A slightly different approach, exact same result

%% Setup a vector of x values
x = linspace(0,0,100);
%% Setup a vector of y values
y = linspace(0,10,100);
%% Plot the paired points in a line
plot(x,y);

I think Method 2 may write more information to memory before the plot process and it's a line longer, so in my eyes, Method 1 should be the better option. If you prefer Method 2, make sure your x and y vectors are the same dimension or you'll end up with a bunch of dots where you're line should be.

Austin A
  • 2,990
  • 6
  • 27
  • 42
  • 2
    your Method 2 is not only more memory before the plot, it takes more memory all the time. The data used to make a plot is still in the figure (take a look at the `cdata` in the figure properties). And your method 1 works fine, why making it more complicated by showing a worse way to do exactly the same thing? – carandraug Sep 08 '14 at 13:14
  • @carandraug the second method is even the recommended way in the octave programming tutorial to get a dotted vertical asymptote lines (compare https://en.wikibooks.org/wiki/Octave_Programming_Tutorial/Getting_started) – rexford Jul 10 '18 at 19:52
4

Unfortunately the Octave documentation for doing obvious things can be ridiculously lousy with no working examples. Drawing a simple line on top of a plot is one.

As already mentioned, it's is very silly to plot straight lines in octave. It's a waste of memory and processing. Instead use the line() function, to draw on top of your plot.

The line() function require 2 non-standard x-values and y-values vectors, instead of the standard point-slope arguments for point A and point B, normally represented by (x1,y1) and (x2,y2). Instead, you need to write this as: X=(x1,x2) and Y=(y1,y2). Thus confusing every living soul!

Here is an example of the correct way to do this in Octave language:

pkg load statistics     % Need to load the statistics package
x = randn (1,1000);     % Normal Distribution of random numbers
clf; histfit(x)         % Make a histogram plot of x and fit the data
ylim ([-20,100])        % Change the plot limits (to lift graph up)

% Draw the (vertical) line between (0,-10) and (0,90)
line ("xdata",[0,0], "ydata",[-10,90], "linewidth", 3)

With the result:

enter image description here

not2qubit
  • 14,531
  • 8
  • 95
  • 135
  • `plot` produces the exact same result as `line`, it is a high-level interface for the low-level `line` function. The inputs are the same, except with `line` you need to specify everything as key-value pairs. There is no advantage to using `line` IMO. – Cris Luengo Mar 23 '19 at 15:14
0

notation (x1,x2),(y1,y2) is really confusing and against textbooks.

Anyway, this is my way:

figure;
hold on;
% vertical line x=0
plot([0,0],[0,10]);
%horizontal line y=0
plot([0,10],[0,0]);
% vertical line x=2
plot([2,2],[0,10]);
hold off;

enter image description here

John Conde
  • 217,595
  • 99
  • 455
  • 496
TVC
  • 67
  • 4