1

I was trying to put labes on the streamlines around a body whose symmetric profile is generated by a vortex and a uniform flow, so far I must get something like this (with labels)

vortex plus uniform flow

which I get it with the next code:

import numpy as np
import matplotlib.pyplot as plt

vortex_height = 18.0
h = vortex_height
vortex_intensity = 55.0
cv = vortex_intensity
permanent_speed = 10.0
U1 = permanent_speed

Y, X = np.mgrid[-25:25:100j, -25:25:100j]
U = 5.0 +  37.0857 * (Y - 18.326581) / (X ** 2 + (Y - 18.326581) ** 2) +- 37.0857 * (Y + 18.326581) / (X ** 2 + (Y + 18.326581) ** 2)
V = - 37.0857 * (X) / (X ** 2 + (Y - 18.326581) ** 2) + 37.0857 * (X) / (X ** 2 + (Y + 18.326581) ** 2)
speed = np.sqrt(U*U + V*V)

plt.streamplot(X, Y, U, V, color=U, linewidth=2, cmap=plt.cm.autumn)
plt.colorbar()
#CS = plt.contour(U, v, speed)
#plt.clabel(CS, inline=1, fontsize=10)
#f, (ax1, ax2) = plt.subplots(ncols=2)
#ax1.streamplot(X, Y, U, V, density=[0.5, 1])

#lw = 5*speed/speed.max()
#ax2.streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw)
plt.savefig("stream_plot5.png")
plt.show()

So I was changing the next example code (which use pylab):

from numpy import exp,arange
from pylab import meshgrid,cm,imshow,contour,clabel,colorbar,axis,title,show

# the function that I'm going to plot
def z_func(x,y):
    return (1-(x**2+y**3))*exp(-(x**2+y**2)/2)

x = arange(-3.0,3.0,0.1)
y = arange(-3.0,3.0,0.1)
X,Y = meshgrid(x, y) # grid of point
Z = z_func(X, Y) # evaluation of the function on the grid

im = imshow(Z,cmap=cm.RdBu) # drawing the function
# adding the Contour lines with labels
cset = contour(Z,arange(-1,1.5,0.2),linewidths=2,cmap=cm.Set2)
clabel(cset,inline=True,fmt='%1.1f',fontsize=10)
colorbar(im) # adding the colobar on the right
# latex fashion title
title('$z=(1-x^2+y^3) e^{-(x^2+y^2)/2}$')
show()

with this plot:

sample_plot

And finally I get it like this:

import numpy as np
from numpy import exp,arange,log
from pylab import meshgrid,cm,imshow,contour,clabel,colorbar,axis,title,show

# PSI = streamline
def streamLine(x, y, U = 5, hv = 18.326581, cv = 37.0857):
    x2 = x ** 2
    y2plus = (y + hv) ** 2
    y2minus = (y - hv) ** 2
    PSI_1 = U * y
    PSI_2 =   0.5 * cv * log(x2 + y2minus)
    PSI_3 = - 0.5 * cv * log(x2 + y2plus)
    psi = PSI_1 + PSI_2 + PSI_3
    return psi
"""
def streamLine(x, y):
    return  0.5 * 37.0857 * log(x ** 2 + (y - 18.326581) ** 2)
# (5.0 * y + 0.5 * 37.0857 * math.log(x ** 2 + (y - 18.326581) ** 2) - 0.5 * 37.0857 * math.log(x ** 2 + (y + 18.326581) ** 2))
"""
x = arange(-20.0,20.0,0.1)
y = arange(-20.0,20.0,0.1)
X,Y = meshgrid(x, y) # grid of point
#Z = z_func(X, Y) # evaluation of the function on the grid
Z= streamLine(X, Y)

im = imshow(Z,cmap=cm.RdBu) # drawing the function
# adding the Contour lines with labels
cset = contour(Z,arange(-5,6.5,0.2),linewidths=2,cmap=cm.Set2)
clabel(cset,inline=True,fmt='%1.1f',fontsize=9)
colorbar(im) # adding the colobar on the right
# latex fashion title
title('$phi= 5.0 y + (1/2)* 37.0857 log(x^2 + (y - 18.326581)^2)-(1/2)* 37.085...$')
show()
#print type(Z)
#print len(Z)

But then I get the next plot:

result_of_vortex_plus_uniform_flow

which is something that keeps me wondering what's wrong because the axis are not where they should be.

6
  • I don't really understand what the problem is. Commented Oct 28, 2013 at 4:14
  • It's about the visualization of the streamlines, I was checking that the len(Z) give 400 and I'm not sure of how to make it look simple like the example that just shows a simple and clear plot, so I was trying to achieve the first plot/image with the labels Commented Oct 28, 2013 at 4:17
  • streamline != contour Commented Oct 28, 2013 at 4:35
  • if streamline != contour , then I must ask the other question, thanks for your time Commented Oct 28, 2013 at 4:46
  • why was this tagged with r? Commented Oct 28, 2013 at 4:52

1 Answer 1

3

contour() draws contour lines of a scalar field, and streamplot() is draw of vector field, Vector fields can be constructed out of scalar fields using the gradient operator.

Here is an example:

import numpy as np
from numpy import exp,arange,log
from pylab import meshgrid,cm,imshow,contour,clabel,colorbar,axis,title,show,streamplot

# PSI = streamline
def f(x, y, U = 5, hv = 18.326581, cv = 37.0857):
    x2 = x ** 2
    y2plus = (y + hv) ** 2
    y2minus = (y - hv) ** 2
    PSI_1 = U * y
    PSI_2 =   0.5 * cv * log(x2 + y2minus)
    PSI_3 = - 0.5 * cv * log(x2 + y2plus)
    psi = PSI_1 + PSI_2 + PSI_3
    return psi

x = arange(-20.0,20.0,0.1)
y = arange(-20.0,20.0,0.1)
X,Y = meshgrid(x, y) # grid of point
#Z = z_func(X, Y) # evaluation of the function on the grid
Z= f(X, Y)

dx, dy = 1e-6, 1e-6
U = (f(X+dx, Y) - f(X, Y))/dx
V = (f(X, Y+dy) - f(X, Y))/dy
streamplot(X, Y, U, V, linewidth=1, color=(0, 0, 1, 0.3))

cset = contour(X, Y, Z,arange(-20,20,2.0),linewidths=2,cmap=cm.Set2)
clabel(cset,inline=True,fmt='%1.1f',fontsize=9)
colorbar(im) # adding the colobar on the right


# latex fashion title
title('$phi= 5.0 y + (1/2)* 37.0857 log(x^2 + (y - 18.326581)^2)-(1/2)* 37.085...$')

show()

output:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

This seems quite what I wanted, but how can I draw for a particular value, say streamline = 5.29 ? (which coincides with the stagnation point)
If you mean the contour line of 5.92: cset = contour(X, Y, Z, [5.92],linewidths=2,cmap=cm.Set2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.