Ask Your Question
1

plot in Octave

asked 10 years ago

Hans gravatar image

updated 10 years ago

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.

Preview: (hide)

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 ( 10 years ago )

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

Hans gravatar imageHans ( 10 years ago )

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 ( 10 years ago )

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 ( 10 years ago )

2 Answers

Sort by » oldest newest most voted
2

answered 10 years ago

calc314 gravatar image

updated 10 years ago

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")
Preview: (hide)
link

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 ( 10 years ago )

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

calc314 gravatar imagecalc314 ( 10 years ago )

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 ( 10 years ago )

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 ( 10 years ago )

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

calc314 gravatar imagecalc314 ( 10 years ago )
0

answered 10 years ago

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>
Preview: (hide)
link

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: 10 years ago

Seen: 2,224 times

Last updated: Jul 10 '14