Ask Your Question
1

plot in Octave

asked 2014-07-07 04:50:03 +0100

Hans gravatar image

updated 2015-01-13 18:07:21 +0100

FrédéricC gravatar image

To add context as requested by kcrisman, I am using Octave in Cloud.Sagemath.com running scripts in .m files.

How does one plot in Octave? Supposedly the command should be the same as in Matlab, e.g.

x = 1:10; figure, plot(x,x.^2)

However, nothing showed up in the terminal. Please help.

edit retag flag offensive close merge delete

Comments

1

We need more context here. Octave is not part of Sage, though we do have an optional interface to it. If you are using it directly inside of Sage, the answer will probably at least to some extent depend on how you are using Sage (command line, notebook, cloud).

kcrisman gravatar imagekcrisman ( 2014-07-07 14:32:05 +0100 )edit

@kcrisman: I have edited my question adding the context. Thank you.

Hans gravatar imageHans ( 2014-07-08 01:10:03 +0100 )edit

Thanks. The answers given are good, but I wonder if they directly address running the files as scripts - maybe calc314's does?

kcrisman gravatar imagekcrisman ( 2014-07-08 15:45:12 +0100 )edit

Good point! The code I provided will run in a .m file as a script. To get the plot to open automatically, we can use a `system` call to the `open` command. I'll modify my answer.

calc314 gravatar imagecalc314 ( 2014-07-08 19:38:21 +0100 )edit

2 Answers

Sort by » oldest newest most voted
2

answered 2014-07-08 13:27:32 +0100

calc314 gravatar image

updated 2014-07-10 02:07:14 +0100

You can print to a file from Octave in the terminal using the print command and then open the jpg file in SMC using the open command via Octave's system command. Here is my .m file:

x=0:0.1:10;
figure();
plot(sin(x));
print('~/sine.jpg');
system('open sine.jpg');

This will pop open the graph in a new tab within SMC.

If you are working from within a worksheet in SMC, you can get the graph to display within the worksheet by using:

%octave
x = 0:0.1:10;
h = figure('visible', 'off');
plot(sin(x));
saveas(h,"~/fig.png");
%sage salvus.file("fig.png")
edit flag offensive delete link more

Comments

There is a .jpg file created in the directory. I do not see the graph supposedly to be popped open by the last command "system('open sine.jpg');" though.

Hans gravatar imageHans ( 2014-07-09 23:56:23 +0100 )edit

Do you see a new tab in SMC that is labeled sine.jpg?

calc314 gravatar imagecalc314 ( 2014-07-10 00:11:25 +0100 )edit

I said "there is a .jpg file created in the directory" referring to just this sine.jpg file. I was asking what effect the last command "system('open sine.jpg');" has. Supposedly it is to pop open a new tab, but I do not see anything.

Hans gravatar imageHans ( 2014-07-10 00:16:37 +0100 )edit

I understood that. That `system` command should open the new tab within SMC. I'm not sure why that's not working. It is working fine for me in SMC.

calc314 gravatar imagecalc314 ( 2014-07-10 01:42:21 +0100 )edit

I've added some code to my original answer that will work within an SMC worksheet.

calc314 gravatar imagecalc314 ( 2014-07-10 02:08:10 +0100 )edit
0

answered 2014-07-08 03:58:25 +0100

slelievre gravatar image

I tried your example on cloud.sagemath.com, by opening a new terminal, typing "octave", and copy-pasting the line you gave. This produced an ascii-art plot of "y = x^2" in the required range.

~/2014-07-07$ octave
GNU Octave, version 3.8.1
Copyright (C) 2014 John W. Eaton and others.
This is free software; see the source code for copying conditions.
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  For details, type 'warranty'.

Octave was configured for "x86_64-pc-linux-gnu".

Additional information about Octave is available at http://www.octave.org.

Please contribute if you find this software useful.
For more information, visit http://www.octave.org/get-involved.html

Read http://www.octave.org/bugs.html to learn how to submit bug reports.
For information about changes from previous versions, type 'news'.

warning: function /usr/share/octave/packages/strings-1.1.0/strjoin.m shadows a core library fu
nction
warning: function /usr/share/octave/packages/strings-1.1.0/base64encode.m shadows a core libra
ry function
warning: function /usr/share/octave/packages/strings-1.1.0/base64decode.m shadows a core libra
ry function
warning: dx does not seem to be present some functionalities will be disabled
warning: function /usr/share/octave/packages/specfun-1.1.0/ellipke.m shadows a core library fu
nction
warning: function /usr/share/octave/packages/specfun-1.1.0/erfcinv.m shadows a built-in functi
on
warning: function /usr/share/octave/packages/specfun-1.1.0/expint.m shadows a core library fun
ction
warning: function /usr/lib/x86_64-linux-gnu/octave/packages/specfun-1.1.0/x86_64-pc-linux-gnu-
api-v49+/ellipj.oct shadows a built-in function
octave:1> x = 1:10; figure, plot(x,x.^2)
+--------------------------------------------------------------------------------------------+
|                                                                                            |
|100 ++---------------+----------------+----------------+----------------+---------------++  |
|    +-----------------------------------------------------------------------------------++  |
|    |                                                                                  -||  |
|    |                                                                                -- ||  |
|    |                                                                               -   ||  |
|    |                                                                              -    ||  |
|    |                                                                             -     ||  |
|    |                                                                            +      ||  |
| 80 |+                                                                          -       |+  |
|    |                                                                         --        ||  |
|    |                                                                        -          ||  |
|    |                                                                       -           ||  |
|    |                                                                     --            ||  |
|    |                                                                    -              ||  |
|    |                                                                   +               ||  |
| 60 |+                                                                --                |+  |
|    |                                                               --                  ||  |
|    |                                                              -                    ||  |
|    |                                                            --                     ||  |
|    |                                                           +                       ||  |
|    |                                                         --                        ||  |
|    |                                                       --                          ||  |
|    |                                                     --                            ||  |
| 40 |+                                                  --                              |+  |
|    |                                                 -+                                ||  |
|    |                                               --                                  ||  |
|    |                                             --                                    ||  |
|    |                                           --                                      ||  |
|    |                                         -+                                        ||  |
|    |                                       --                                          ||  |
| 20 |+                                    --                                            |+  |
|    |                                   --                                              ||  |
|    |                                -+-                                                ||  |
|    |                             ---                                                   ||  |
|    |                           --                                                      ||  |
|    |                     ----+-                                                        ||  |
|    |              --+----                                                              ||  |
|    |          ----  +                +                +                +               |+  |
|  0 +-----------------------------------------------------------------------------------++  |
|    0                2                4                6                8               10  |
+--------------------------------------------------------------------------------------------+
octave:2>
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-07-07 04:50:03 +0100

Seen: 2,019 times

Last updated: Jul 10 '14