Julia plot on REPL and save

Hello,

is there a simple way to plot on REPL and save the plot? There different methods on the Internet, but none worked with me.
Is there a native Julia plot function or do I need packages like pyplot and gadfly?
I tried with:

using PyPlot
julia> x
0.0:0.05:0.3
julia> y
7-element Array{Float64,1}:
 500000.0
     -1.93144663704e10
     -2.8821735719767118e19
     -6.417929867928171e37
     -3.1823257859879353e74
     -7.824272717547299e147
     -4.7297987573339595e294
julia> myPlot = plot(x, y, color="red", linewidth=2.0, linestyle="--")
1-element Array{PyCall.PyObject,1}:
 PyObject <matplotlib.lines.Line2D object at 0x7f691d3004e0>
julia> show(myPlot)
PyCall.PyObject[PyObject <matplotlib.lines.Line2D object at 0x7f691d3004e0>]
julia> draw(myPlot)
ERROR: PyError ($(Expr(:escape, :(ccall(#= /home/gigiux/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:44 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) <class 'TypeError'>
TypeError('draw() takes 0 positional arguments but 1 was given',)
Stacktrace:
 [1] pyerr_check at /home/gigiux/.julia/packages/PyCall/ttONZ/src/exception.jl:60 [inlined]
 [2] pyerr_check at /home/gigiux/.julia/packages/PyCall/ttONZ/src/exception.jl:64 [inlined]
 [3] macro expansion at /home/gigiux/.julia/packages/PyCall/ttONZ/src/exception.jl:84 [inlined]
 [4] __pycall!(::PyCall.PyObject, ::Ptr{PyCall.PyObject_struct}, ::PyCall.PyObject, ::Ptr{Nothing}) at /home/gigiux/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:44
 [5] _pycall!(::PyCall.PyObject, ::PyCall.PyObject, ::Tuple{Array{PyCall.PyObject,1}}, ::Int64, ::Ptr{Nothing}) at/home/gigiux/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:29
 [6] _pycall! at /home/gigiux/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:11 [inlined]
 [7] #pycall#110 at /home/gigiux/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:86 [inlined]
 [8] pycall at /home/gigiux/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:86 [inlined]
 [9] #draw#40(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::Array{PyCall.PyObject,1}) at /home/gigiux/.julia/packages/PyPlot/cdCMF/src/PyPlot.jl:177
 [10] draw(::Array{PyCall.PyObject,1}) at /home/gigiux/.julia/packages/PyPlot/cdCMF/src/PyPlot.jl:174
 [11] top-level scope at none:0
julia> plot.show(myPlot)
ERROR: type #plot has no field show
Stacktrace:
 [1] getproperty(::Any, ::Symbol) at ./sysimg.jl:18
 [2] top-level scope at none:0
julia> plot(myPlot)
ERROR: PyError ($(Expr(:escape, :(ccall(#= /home/gigiux/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:44 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) <class 'TypeError'>
TypeError("float() argument must be a string or a number, not 'Line2D'",)
  File "/home/gigiux/.local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 2813, in plot
    is not None else {}), **kwargs)
  File "/home/gigiux/.local/lib/python3.6/site-packages/matplotlib/__init__.py", line 1810, in inner
    return func(ax, *args, **kwargs)
  File "/home/gigiux/.local/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 1612, in plot
    self.add_line(line)
  File "/home/gigiux/.local/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 1895, in add_line
    self._update_line_limits(line)
  File "/home/gigiux/.local/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 1917, in _update_line_limits
    path = line.get_path()
  File "/home/gigiux/.local/lib/python3.6/site-packages/matplotlib/lines.py", line 945, in get_path
    self.recache()
  File "/home/gigiux/.local/lib/python3.6/site-packages/matplotlib/lines.py", line 645, in recache
    y = _to_unmasked_float_array(yconv).ravel()
  File "/home/gigiux/.local/lib/python3.6/site-packages/matplotlib/cbook/__init__.py", line 1365, in _to_unmasked_float_array
    return np.asarray(x, float)
  File "/home/gigiux/.local/lib/python3.6/site-packages/numpy/core/numeric.py", line 538, in asarray
    return array(a, dtype, copy=False, order=order)
Stacktrace:
 [1] pyerr_check at /home/gigiux/.julia/packages/PyCall/ttONZ/src/exception.jl:60 [inlined]
 [2] pyerr_check at /home/gigiux/.julia/packages/PyCall/ttONZ/src/exception.jl:64 [inlined]
 [3] macro expansion at /home/gigiux/.julia/packages/PyCall/ttONZ/src/exception.jl:84 [inlined]
 [4] __pycall!(::PyCall.PyObject, ::Ptr{PyCall.PyObject_struct}, ::PyCall.PyObject, ::Ptr{Nothing}) at /home/gigiux/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:44
 [5] _pycall!(::PyCall.PyObject, ::PyCall.PyObject, ::Tuple{Array{PyCall.PyObject,1}}, ::Int64, ::Ptr{Nothing}) at/home/gigiux/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:29
 [6] _pycall! at /home/gigiux/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:11 [inlined]
 [7] #pycall#110 at /home/gigiux/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:86 [inlined]
 [8] pycall at /home/gigiux/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:86 [inlined]
 [9] #plot#85(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::Array{PyCall.PyObject,1}) at /home/gigiux/.julia/packages/PyPlot/cdCMF/src/PyPlot.jl:177
 [10] plot(::Array{PyCall.PyObject,1}) at /home/gigiux/.julia/packages/PyPlot/cdCMF/src/PyPlot.jl:174
 [11] top-level scope at none:0
julia> png("sim.png", 30, 20)
ERROR: UndefVarError: png not defined
Stacktrace:
 [1] top-level scope at none:0
1 Like

If the plot didn’t show up right after the call to plot, you need display(my_plot).

To save the plot to the harddrive you can use savefig( my_plot, "path/to/figure" ).

There is no built in plotting, so you have to choose one of the plenty visualization packages.

If you want to have plots inside the REPL you can use UnicodePlots.

This is how to plot in the REPL and save the figure as pdf in Gaston:

julia> using Gaston
julia> set(terminal="dumb")
julia> plot(rand(10))
                                                                               
      +--------------------------------------------------------------------+   
    1 |-++      +      *      +      +      +      +      +      +      *+-|   
      |                **                                              *   |   
      |               *  *                                             *   |   
      |               *  *                                            *    |   
  0.8 |-+*           *    *                                          *   +-|   
      |   *          *     *                                         *     |   
      |   *          *      *       *********                       *      |   
      |    *        *       *     **         *                      *      |   
  0.6 |-+  *        *        *  **           *                     *     +-|   
      |     *       *         **              *                   *        |   
      |     *      *                           *                  *        |   
      |      *     *                           *          ********         |   
  0.4 |-+    *    *                             *        *               +-|   
      |       *   *                             *       *                  |   
      |       *   *                              *    **                   |   
      |        * *                                *  *                     |   
  0.2 |-+      * *                                * *                    +-|   
      |         *                                  *                       |   
      |  +      *      +      +      +      +      +      +      +      +  |   
      +--------------------------------------------------------------------+   
         1      2      3      4      5      6      7      8      9      10     
                                                                               

julia> printfigure(outputfile="plotfromrepl.pdf")
2 Likes

I got:

julia> display(myPlot)
1-element Array{PyCall.PyObject,1}:
 PyObject <matplotlib.lines.Line2D object at 0x7f9b6bc19208>

But no figure. But savefig("fig.png") worked. However, savefig has a parameter for the dpi and one for the format (only png, pdf, ps, eps and svg) and one for the orientation; is it possible to give the actual dimension of the figure? Thank you.

When you create the figure, you can use figure(figsize=...).

Since PyPlot is just a thin wrapper around Matplotlib, you can easily google things like “figure size matplotlib” and find dozens of answers; you just need to translate the syntax from Python.

1 Like