how to plot streamlines , when i know u and v components of velocity(numpy 2d arrays), using a plotting program in python?

前端 未结 3 1426
被撕碎了的回忆
被撕碎了的回忆 2020-12-07 22:03

i hope the title itself was quite clear , i am solving 2D lid-driven cavity(square domain) problem using fractional step method , finite difference formulation (Navier-Stoke

相关标签:
3条回答
  • 2020-12-07 22:29

    In version 1.2 of Matplotlib, there is now a streamplot function.

    0 讨论(0)
  • 2020-12-07 22:32

    Have a look at Tom Flannaghan's streamplot function. The relevant thread on the user's list is here, and there's also another similar code snippet by Ray Speth that does things slightly differently.

    If you have problems with speed, it might be more efficient to use some of scipy's integration functionality instead of the pure-numpy integration functions used in both of these examples. I haven't tried it, though, and these deliberately avoid a dependency on scipy. (scipy is a rather heavy dependency compared to numpy)

    From it's example plot:

    import matplotlib.pyplot as plt
    import numpy as np
    from streamplot import streamplot
    
    x = np.linspace(-3,3,100)
    y = np.linspace(-3,3,100)
    u = -1-x**2+y[:,np.newaxis]
    v = 1+x-y[:,np.newaxis]**2
    speed = np.sqrt(u*u + v*v)
    
    plt.figure()
    plt.subplot(121)
    streamplot(x, y, u, v, density=1, INTEGRATOR='RK4', color='b')
    plt.subplot(122)
    streamplot(x, y, u, v, density=(1,1), INTEGRATOR='RK4', color=u,
               linewidth=5*speed/speed.max())
    plt.show()
    

    enter image description here

    Another option is to use VTK. It's accelerated 3D plotting, so making a 2D plot will require setting the camera properly (which isn't too hard), and you won't be able to get vector output.

    Mayavi, tvtk, and mlab provide pythonic wrappers for VTK. It has lots of functionality along these lines.

    The easiest way to use VTK to plot streamlines from numpy arrays is to use mayavi.mlab.flow. I'll skip an example for the moment, but if you want to explore using VTK to do this, I can add one.

    0 讨论(0)
  • 2020-12-07 22:43

    Have a look at matplotlib's quiver: http://matplotlib.sourceforge.net/examples/pylab_examples/quiver_demo.html

    0 讨论(0)
提交回复
热议问题