How to plot and display a square in Octave?

拥有回忆 提交于 2019-12-08 00:53:31

问题


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

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.

回答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

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:

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


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])




回答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)

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).



来源:https://stackoverflow.com/questions/7370867/how-to-plot-and-display-a-square-in-octave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!