# plot_vector_field3d in spherical coordinates

Hello,

How can I draw a vector field in spherical coordinates?

For example

E=EuclideanSpace(3)
c_cart.<x,y,z>=E.cartesian_coordinates()
c_spher.<r,ph,th>=E.spherical_coordinates()

f_spher=E.frames() # frame in spherical coordinates
E.set_default_chart(c_spher)
E.set_default_frame(f_spher)

vf=E.vectorfield((r,0,0), frame=f_spher,cart=c_spher,name="vf");show(vf.display())


This beutifully gives me a vector field as r e_r

Now, I would like to plot this field in spherical coordinates

plot_vector_field3d([c.expr() for c in vf[:]], (r,2,10),(th,0,pi),(ph,0,2*pi))


I also tried out the transformation keyword as it is in plot3d function, but it still plots r as x. How can I plot the vector field in spherical coordinates? I was expecting to see outgoing arrows in all directions from r=2 to 10?

edit retag close merge delete

Sort by » oldest newest most voted

Instead of plot_vector_field3d, use the method plot of vector fields:

vf.plot(chart=c_cart, chart_domain=c_spher, ranges={r: (2, 10)}, scale=0.2, width=2)


The output is See the documentation of VectorField.plot() for explanations and more examples.

Side note: there are some typos in your example (E.vectorfield --> E.vector_field, cart --> chart); it should read

vf = E.vector_field((r,0,0), frame=f_spher, chart=c_spher, name="vf")


Besides, instead of f_spher = E.frames(), you can use f_spher = E.spherical_frame().

more

Thank you so much.

Yep, I made typos. Sorry for that. The code was on another computer, I was typing it it.

Is there a way to increase density of the vectors? It is also very slow. Is there a way to speed it up?

To increase the density of vectors, use the optional argument number_values, for instance number_values=9; see the documentation of VectorField.plot(). Yes the plot is slow and there is room for improvement. This is on my todo list...