I would like to simply plot a filled contour for given x
, y
, and z
variables (or a function z(x,y)
), and save it to eps (or svg). My goal is to make a GitHub repo of simple plotting examples that export to eps, svg, pdf, etc., that I can then use as a basis for making publication-quality figures. I believe getting such simple examples out there will help the Julia users who struggle for simple tasks like me Anyway, here is my current non-working MWE using julia v0.7 (it’s in the repo too):
using Pkg
Pkg.activate(".")
function peaks(x, y)
z = 3 * (1 - x)^2 * exp(-x^2 - (y + 1)^2)
z += -10 * (x / 5 - x^3 - y^5) * exp(-x^2 - y^2)
z += -1/3 * exp(-(x+1)^2 - y^2)
return z
end
x = range(-3, stop = 3, length = 100)
y = range(-2, stop = 2, length = 80)
levels = collect(-1:0.2:1)
using Plots
# using PyPlot
# using Plotly, ORCA
plt = contourf(x, y, peaks; levels = levels)
savefig(plt, "test.eps")
Now I can comment/uncomment the package (or backend) being used. The code above is in a file called plot_a_contour_of_peaks.jl
and lives in a project where I have added the packages Plots
, PyPlot
, Plotly
, and ORCA
.
I then run (julia version 0.7)
julia> include("plot_a_contour_of_peaks.jl")
and get the following errors. I’m looking for help to figure out how to solve these errors!
-
using
Plots
(for which the default backend isGR.jl
if I understand correctly)ERROR: LoadError: MethodError: no method matching _show(::IOStream, ::MIME{Symbol("image/eps")}, ::Plots.Plot{Plots.GRBackend}) Closest candidates are: _show(::IO, ::MIME{Symbol("text/html")}, ::Plots.Plot) at /Users/benoitpasquier/.julia/packages/Plots/ex9On/src/output.jl:171 _show(::IO, ::MIME{Symbol("text/plain")}, ::Plots.Plot) at /Users/benoitpasquier/.julia/packages/Plots/ex9On/src/output.jl:212 _show(::IO, ::MIME{Symbol("application/postscript")}, ::Plots.Plot{Plots.GRBackend}) at /Users/benoitpasquier/.julia/packages/Plots/ex9On/src/backends/gr.jl:1379 ... Stacktrace: [1] show(::IOStream, ::MIME{Symbol("image/eps")}, ::Plots.Plot{Plots.GRBackend}) at /Users/benoitpasquier/.julia/packages/Plots/ex9On/src/output.jl:206 [2] eps(::Plots.Plot{Plots.GRBackend}, ::String) at /Users/benoitpasquier/.julia/packages/Plots/ex9On/src/output.jl:42 [3] savefig(::Plots.Plot{Plots.GRBackend}, ::String) at /Users/benoitpasquier/.julia/packages/Plots/ex9On/src/output.jl:123 [4] top-level scope at none:0 [5] include at ./boot.jl:317 [inlined] [6] include_relative(::Module, ::String) at ./loading.jl:1038 [7] include(::Module, ::String) at ./sysimg.jl:29 [8] include(::String) at ./client.jl:398 [9] top-level scope at none:0 in expression starting at /Users/benoitpasquier/Projects/JuliaPlotting/plot_a_contour_of_peaks.jl:16
-
using
PyPlot.jl
ERROR: LoadError: PyError ($(Expr(:escape, :(ccall(#= /Users/benoitpasquier/.julia/packages/PyCall/rUul9/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 'PyCall.jlwrap'") File "/Users/benoitpasquier/.julia/packages/Conda/m7vem/deps/usr/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2938, in contourf ret = ax.contourf(*args, **kwargs) File "/Users/benoitpasquier/.julia/packages/Conda/m7vem/deps/usr/lib/python3.7/site-packages/matplotlib/__init__.py", line 1867, in inner return func(ax, *args, **kwargs) File "/Users/benoitpasquier/.julia/packages/Conda/m7vem/deps/usr/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 6290, in contourf contours = mcontour.QuadContourSet(self, *args, **kwargs) File "/Users/benoitpasquier/.julia/packages/Conda/m7vem/deps/usr/lib/python3.7/site-packages/matplotlib/contour.py", line 890, in __init__ kwargs = self._process_args(*args, **kwargs) File "/Users/benoitpasquier/.julia/packages/Conda/m7vem/deps/usr/lib/python3.7/site-packages/matplotlib/contour.py", line 1476, in _process_args x, y, z = self._contour_args(args, kwargs) File "/Users/benoitpasquier/.julia/packages/Conda/m7vem/deps/usr/lib/python3.7/site-packages/matplotlib/contour.py", line 1534, in _contour_args x, y, z = self._check_xyz(args[:3], kwargs) File "/Users/benoitpasquier/.julia/packages/Conda/m7vem/deps/usr/lib/python3.7/site-packages/matplotlib/contour.py", line 1565, in _check_xyz z = ma.asarray(args[2], dtype=np.float64) File "/Users/benoitpasquier/.julia/packages/Conda/m7vem/deps/usr/lib/python3.7/site-packages/numpy/ma/core.py", line 7810, in asarray subok=False, order=order) File "/Users/benoitpasquier/.julia/packages/Conda/m7vem/deps/usr/lib/python3.7/site-packages/numpy/ma/core.py", line 2785, in __new__ order=order, subok=True, ndmin=ndmin) Stacktrace: [1] pyerr_check at /Users/benoitpasquier/.julia/packages/PyCall/rUul9/src/exception.jl:60 [inlined] [2] pyerr_check at /Users/benoitpasquier/.julia/packages/PyCall/rUul9/src/exception.jl:64 [inlined] [3] macro expansion at /Users/benoitpasquier/.julia/packages/PyCall/rUul9/src/exception.jl:84 [inlined] [4] __pycall!(::PyCall.PyObject, ::Ptr{PyCall.PyObject_struct}, ::PyCall.PyObject, ::PyCall.PyObject) at /Users/benoitpasquier/.julia/packages/PyCall/rUul9/src/pyfncall.jl:44 [5] _pycall!(::PyCall.PyObject, ::PyCall.PyObject, ::Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},typeof(peaks)}, ::Int64, ::PyCall.PyObject) at /Users/benoitpasquier/.julia/packages/PyCall/rUul9/src/pyfncall.jl:22 [6] _pycall!(::PyCall.PyObject, ::PyCall.PyObject, ::Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},typeof(peaks)}, ::Base.Iterators.Pairs{Symbol,Array{Float64,1},Tuple{Symbol},NamedTuple{(:levels,),Tuple{Array{Float64,1}}}}) at /Users/benoitpasquier/.julia/packages/PyCall/rUul9/src/pyfncall.jl:11 [7] #pycall#88(::Base.Iterators.Pairs{Symbol,Array{Float64,1},Tuple{Symbol},NamedTuple{(:levels,),Tuple{Array{Float64,1}}}}, ::Function, ::PyCall.PyObject, ::Type{PyCall.PyAny}, ::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, ::Vararg{Any,N} where N) at /Users/benoitpasquier/.julia/packages/PyCall/rUul9/src/pyfncall.jl:86 [8] (::getfield(PyCall, Symbol("#kw##pycall")))(::NamedTuple{(:levels,),Tuple{Array{Float64,1}}}, ::typeof(PyCall.pycall), ::PyCall.PyObject, ::Type{PyCall.PyAny}, ::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, ::Vararg{Any,N} where N) at ./none:0 [9] #contourf#34(::Base.Iterators.Pairs{Symbol,Array{Float64,1},Tuple{Symbol},NamedTuple{(:levels,),Tuple{Array{Float64,1}}}}, ::Function, ::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, ::Vararg{Any,N} where N) at /Users/benoitpasquier/.julia/packages/PyPlot/fZuOQ/src/PyPlot.jl:179 [10] (::getfield(PyPlot, Symbol("#kw##contourf")))(::NamedTuple{(:levels,),Tuple{Array{Float64,1}}}, ::typeof(contourf), ::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, ::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, ::Function) at ./none:0 [11] top-level scope at none:0 [12] include at ./boot.jl:317 [inlined] [13] include_relative(::Module, ::String) at ./loading.jl:1038 [14] include(::Module, ::String) at ./sysimg.jl:29 [15] include(::String) at ./client.jl:398 [16] top-level scope at none:0 in expression starting at /Users/benoitpasquier/Projects/JuliaPlotting/plot_a_contour_of_peaks.jl:18
-
using
Plotly
andORCA
:
ERROR: LoadError: UndefVarError: contourf not defined
Stacktrace:
[1] top-level scope at none:0
[2] include at ./boot.jl:317 [inlined]
[3] include_relative(::Module, ::String) at ./loading.jl:1038
[4] include(::Module, ::String) at ./sysimg.jl:29
[5] include(::String) at ./client.jl:398
[6] top-level scope at none:0
in expression starting at /Users/benoitpasquier/Projects/JuliaPlotting/plot_a_contour_of_peaks.jl:18