9

Does anyone know how to plot a 3D surface with Julia's Pyplot with e.g the norm of the surface-gradient as the face color?

Similar to this topic for Python: Color matplotlib plot_surface command with surface gradient

Community
  • 1
  • 1
René Hiemstra
  • 117
  • 1
  • 4

1 Answers1

10

I saw this and I just HAD to make it work. It was almost supported out of the box with Plots.jl, using the PyPlot backend. I just had to swap out for a custom matplotlib shader to apply a different z-matrix.

You'll notice that I'm accessing numpy's gradient function (imported through PyCall), and I'm wrapping the gradient matrix G so that it doesn't get sliced into columns. All in all... much simpler than the python example!

    using Plots; pyplot();
    x = y = LinRange(-5.0, 5.0, 30)
    z = sin(sqrt(Float64[xi^2+yi^2 for xi = x, yi = y]))
    surface(x, y, z, alpha = 0.7)

enter image description here

    using PyCall
    Gx, Gy = Plots.pynb.pymember(:gradient)(z)
    surface(x, y, z, alpha = 0.8, zcolor = wrap(G))

enter image description here

PatrickT
  • 10,037
  • 9
  • 76
  • 111
Tom Breloff
  • 1,782
  • 9
  • 15
  • I like the concise syntax you have in Plots.jl. stevengj suggested the following in PyPlot https://github.com/JuliaLang/IJulia.jl/issues/418#issuecomment-215609485 – René Hiemstra Apr 30 '16 at 20:10
  • 2
    How would you do this in Plots.jl using Julia v1.5? – René Hiemstra Oct 05 '20 at 14:29
  • @Tom Breloff: would be grateful if you would update the syntax to `Julia 1.6`, as it doesn't run anymore. This is a top-ranked question/answer for the topic. Many thanks! – PatrickT Jun 10 '21 at 07:18
  • I can make the first part run with `surface(x, y, abs.(z), alpha = 0.7)`, getting rid of the complex part. – PatrickT Jun 10 '21 at 07:21