# Julia plot on REPL and save

**URL:** <https://discourse.julialang.org/t/julia-plot-on-repl-and-save/26288>\
**Category:** New to Julia\
**Created:** [July 12, 2019, 1:24pm UTC](https://discourse.julialang.org/t/julia-plot-on-repl-and-save/26288 "2019-07-12T13:24:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [July 12, 2019, 1:24pm UTC](https://discourse.julialang.org/t/julia-plot-on-repl-and-save/26288/1 "2019-07-12T13:24:07Z")

</div>

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:

```julia
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

```

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [July 12, 2019, 2:17pm UTC](https://discourse.julialang.org/t/julia-plot-on-repl-and-save/26288/2 "2019-07-12T14:17:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [July 12, 2019, 3:14pm UTC](https://discourse.julialang.org/t/julia-plot-on-repl-and-save/26288/3 "2019-07-12T15:14:58Z")

</div>

This is how to plot in the REPL and save the figure as pdf in [Gaston](https://mbaz.github.io/Gaston.jl/v0.10.0/):

```julia
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")

```

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [July 13, 2019, 2:05pm UTC](https://discourse.julialang.org/t/julia-plot-on-repl-and-save/26288/4 "2019-07-13T14:05:40Z")

</div>

> [@BeastyBlacksmith](#):
>
> display(my\_plot)

I got:

```julia
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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 13, 2019, 3:59pm UTC](https://discourse.julialang.org/t/julia-plot-on-repl-and-save/26288/5 "2019-07-13T15:59:34Z")

</div>

> [@Luigi\_Marongiu](#):
>
> 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.
