Skip to content
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

plot_surface and facecolors #91

Closed
mardukbp opened this issue Sep 25, 2014 · 8 comments
Closed

plot_surface and facecolors #91

mardukbp opened this issue Sep 25, 2014 · 8 comments

Comments

@mardukbp
Copy link

Apparently, the cmap keyword in plot_surface colors the surface in the Z direction. I needed to color the surface of a sphere with a scalar function. This is achieved with the facecolors keyword. In Matplotlib this is the solution:

norm = matplotlib.colors.Normalize()
ax.plot_surface(x, y, z,  rstride=1, cstride=1, facecolors=cm.jet(norm(colorfunction)))

However, in PyPlot.jl the ColorMap type does not have a method that takes an array of floats and returns the associated colors in the colormap. The workaround is

map = get_cmap("Blues")
map[:__call__](linspace(0,1,10))

Something similar happens with matplotlib.colors.Normalize. As a result, in PyPlot.jl the facecolors keyword must have the value

facecolors=map[:__call__](matplotlib.colors.Normalize()[:__call__](colorfunction))

I am new to Julia and don't have the skills (yet) to submit a pull request fixing this. Hopefully it won't be difficult to implement.

@stevengj
Copy link
Member

I agree that using :__call__ is annoying. But this will go away in Julia 0.4, when call overloading should be available (JuliaLang/julia#8008). At that point, you will be able to call Python objects in Julia naturally, without resorting to :__call__ or pycall.

@mardukbp
Copy link
Author

Good to know. Thank you.

@gozzilli
Copy link

Finding this saved my life. I spent hours trying to get the colormap to work, and couldn't figure out how to use it. Could this be added to the documentation, or to this recordnotfound page, which has the most amount of detail on colormaps, or to the README of this project? Thanks!

@stevengj
Copy link
Member

stevengj commented Feb 4, 2016

@gozzilli, in Julia 0.4 the __call__ should no longer be needed, because call overloading works.

@IlyaOrson
Copy link

Hello,
I was trying to emulate this examples with julia 0.4.5 and matplotlib 1.5.1 and I couldn't avoid the use of __call__ to get them working.

For example, I used this:

number_of_lines = 5
viri = get_cmap("viridis")
cm_subsection = linspace(0.0, 1.0, number_of_lines)
colors = [viri[:__call__](x) for x in cm_subsection]
for col in colors
       plot(rand(10), color = col)
end

because a direct call throws an error:

colors = [viri(x) for x in cm_subsection]

ERROR: MethodError: call has no method matching call(::PyPlot.ColorMap, ::Float64)
Closest candidates are:
BoundsError()
BoundsError(::Any...)
DivideError()
...
in anonymous at no file

I get a similar error for the suggestion made here.
Am I missing something?
Thanks

@stevengj
Copy link
Member

stevengj commented Jun 4, 2016

I can't reproduce:

julia> using PyPlot; c = get_cmap("RdBu")
ColorMap "RdBu"

julia> c(0.5)
(0.9657054999295402,0.9672433698878569,0.968089198364931,1.0)

julia> [c(x) for x in linspace(0,1,10)]
10-element Array{Any,1}:
 (0.40392157435417175,0.0,0.12156862765550613,1.0)               
 (0.711880063309389,0.12179931034060085,0.1816993519371631,1.0)  
 (0.8622837452327504,0.4295271150037354,0.3427143558567646,1.0)  
 (0.9686274528503418,0.7176470756530762,0.6000000238418579,1.0)  
 (0.9820069214876961,0.9061899325426888,0.861591703751508,1.0)   
 (0.8838908181470984,0.928489046938279,0.9530180727734285,1.0)   
 (0.6549019813537598,0.814379096031189,0.8941176533699036,1.0)   
 (0.3234909813778073,0.6149173624375287,0.7854671408148373,1.0)  
 (0.14248366800009035,0.4173010459133223,0.6833525755826164,1.0) 
 (0.019607843831181526,0.1882352977991104,0.3803921639919281,1.0)

@stevengj
Copy link
Member

stevengj commented Jun 4, 2016

Maybe you need a Pkg.update()?

@IlyaOrson
Copy link

Pkg.checkout("PyPlot") did the trick. Thanks!

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

No branches or pull requests

4 participants