Skip to content

contour with (x,y) being ranges #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
PaulSoderlind opened this issue Apr 15, 2022 · 6 comments
Closed

contour with (x,y) being ranges #8

PaulSoderlind opened this issue Apr 15, 2022 · 6 comments
Labels
pythoncall upstream PythonCall issue

Comments

@PaulSoderlind
Copy link

crashes. (In contrast, several other plot types work with ranges, for instance, surf and plot.)

Example:

x = -10:10
y = range(-5,5,length=11)
z = x.^2 .+ y'.^2

figure()
  contour(x, y, z')                            #crashes
  #contour(collect(x), collect(y), z')         #works
@stevengj
Copy link
Member

It throws an exception, which is not the same as "crashing".

@stevengj
Copy link
Member

The problem seems to be y. If you replace y with collect(y) it works. PythonCall translates these as:

julia> Py(y)
Python VectorValue: <jl -5.0:1.0:5.0>

julia> Py(collect(y))
Python VectorValue: <jl [-5.0, -4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0]>

so Matplotlib doesn't mind a PythonCall VectorValue built from a Vector, but it doesn't like a VectorValue built from a StepRangeLen for some reason. Maybe there is some missing method that PythonCall should have implemented in the latter case?

@stevengj
Copy link
Member

In particular, the problem seems to be that np.asarray doesn't like StepRangeLen:

julia> np = pyimport("numpy")
Python module: <module 'numpy' from '/Users/stevenj/.julia/environments/v1.7/.CondaPkg/env/lib/python3.10/site-packages/numpy/__init__.py'>

julia> np.asarray(y, dtype=np.float64)
ERROR: Python: TypeError: ArrayValue.__array__() takes 1 positional argument but 2 were given
Python stacktrace: none
Stacktrace:
 [1] pythrow()
   @ PythonCall ~/.julia/packages/PythonCall/XgP8G/src/err.jl:94
 [2] errcheck
   @ ~/.julia/packages/PythonCall/XgP8G/src/err.jl:10 [inlined]
 [3] pycallargs
   @ ~/.julia/packages/PythonCall/XgP8G/src/abstract/object.jl:154 [inlined]
 [4] pycall(f::Py, args::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}; kwargs::Base.Pairs{Symbol, Py, Tuple{Symbol}, NamedTuple{(:dtype,), Tuple{Py}}})
   @ PythonCall ~/.julia/packages/PythonCall/XgP8G/src/abstract/object.jl:165
 [5] #_#11
   @ ~/.julia/packages/PythonCall/XgP8G/src/Py.jl:360 [inlined]
 [6] top-level scope
   @ REPL[15]:1

julia> np.asarray(collect(y), dtype=np.float64)
Python ndarray: array([-5., -4., -3., -2., -1.,  0.,  1.,  2.,  3.,  4.,  5.])

@stevengj
Copy link
Member

This works fine:

julia> np.asarray(y)
Python ndarray: array([-5., -4., -3., -2., -1.,  0.,  1.,  2.,  3.,  4.,  5.])

So the problem is the dtype argument.

@stevengj
Copy link
Member

It looks like the problem is that this __array__ method in PythonCall doesn't accept the optional dtype argument.

@stevengj
Copy link
Member

Closed by JuliaPy/PythonCall.jl@be521aa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pythoncall upstream PythonCall issue
Projects
None yet
Development

No branches or pull requests

2 participants