How to plot and display a square in Octave?

10,561

Solution 1

I believe this is an issue with Gnuplot's windows output device. Compare it against the wxt device:

Gnuplot 4.4.3, WinXP

# Gnuplot, wxWidgets terminal
set terminal wxt size 200,400
set size ratio -1        # set size square
plot x

# Gnuplot, Windows terminal
set terminal windows size 200,400
set size ratio -1        # set size square
plot x

gnuplot_wxWidgets_terminal gnuplot_Windows_terminal

Note that for the "win terminal", size affects the figure size including window title bar and status bar, while for the "wx terminal" it only sets the inner drawing area


Octave 3.4.2, WinXP

Unfortunately, when I tried this in Octave, it still was not what it should be for both terminal types. In fact, the problem is that resizing the figure using set(gcf,'position',[..]) had no effect:

# Octave, backend=Gnuplot, terminal=wxt/windows
graphics_toolkit gnuplot     # backend gnuplot
setenv('GNUTERM','wx')       # wx/windows
figure, set(gcf,'position',[100 100 200 400])
plot(-10:10, -10:10, 'r'), legend('x')
axis([-10 10 -10 10])
axis equal                   # axis square

Therefore, I had to manually resize the figures using the mouse to the specified size (200,400) (yes, I actually pulled a virtual ruler and measured the pixels!). Finally call refresh command to replot:

octave_gnuplot_wx_resized octave_gnuplot_windows_resized

The good news is that once you correctly set the figure size, axis equal is working for both terminals types.

On the other hand, the new FLTK backend is behaving correctly without any hacks, so you might wanna switch to it:

# Octave, backend=FLTK
graphics_toolkit fltk        # backend fltk
figure, set(gcf,'position',[100 100 200 400])
plot(-10:10, -10:10, 'r'), legend('x')
axis([-10 10 -10 10])
axis equal

octave_fltk


MATLAB

For reference, here is the MATLAB output:

%# MATLAB
figure, set(gcf,'position',[100 100 200 400])
plot(-10:10, -10:10, 'r'), legend('x')
axis equal
axis([-10 10 -10 10])

matlab

Solution 2

This works for me (Octave on Linux):

x = [0, 1, 1, 0, 0]';
y = [0, 0, 1, 1, 0];
plot(x, y)
axis([-1,2, -1,2])
axis equal % or axis square

However, this only works until you resize the figure window, so I admit it is a little fidgety. So to get what you want with Octave, I guess you will have to place your figure window and make all changes before calling axis equal. I had very little luck with calling axis equal multiple times.

I guess this is related to limitations in GnuPlot (but I have no hard data supporting that claim), so you could try out other plotting libraries to see whether the exhibit the same behavior.

edit: For completeness a plot of what my code produces (if I refrain from resizing the figure window)

enter image description here

If you don't get anything working, you can try to debug the Octave code you are calling. In MATLAB you can inspect the corresponding code by typing edit axis, but in Octave I guess you have to give the complete path to the axis.m file (which is mentioned in help axis).

Share:
10,561
Jiri Kriz
Author by

Jiri Kriz

Enthusiastic programmer, mathematician and hiker.

Updated on June 05, 2022

Comments

  • Jiri Kriz
    Jiri Kriz almost 2 years

    I cannot achieve to plot a square in Octave. I cannot force equally scaled axes, so I am getting a rectangle instead:

    enter image description here

    The following trials are not working:

    x = [0, 1, 1, 0, 0]';
    y = [0, 0, 1, 1, 0];
    plot(x, y), axis equal, axis([-1,2, -1,2])
    % figure('Position', [10,10,100,100]); %[startx,starty,width,height]
    % plot(x, y)
    

    Probably I would need to specify a fixed window size and equally scaled axes. I would be satisfied, when the first such display window would show a correct square. A luxury solution would make the window (or its content) not interactively resizable.

    Remarks:

    1. I have Octave 3.2.4 on Windows XP.
    2. The suggestion in Stackoverflow does not work.