python - Setting labels manually in matplotlib contour-plot wrong -
i trying add manual labels contourplot in code below. labels printed randomly. have idea how fix this? seems bug in matplotlib.
regards, david
import numpy np import matplotlib.pyplot plt = 0.2 resolution = 100 xarray = np.linspace(0,0.25,num=resolution) yarray = np.linspace(0,1,num=resolution) = np.empty([resolution,resolution]) xc = 0 yc = 0 x in xarray: y in yarray: #print xc,yc wp = 1./np.pi*np.arctan(np.sin(np.pi*y)*np.sinh(np.pi*a/2.)/ (np.cosh(np.pi*x)-np.cos(np.pi*y)*np.cosh(np.pi*a/2.))) if wp <= 0: wp = wp+1 a[xc, yc] = wp else: a[xc, yc] = wp yc += 1 yc=0 xc += 1 = a.transpose() b = np.fliplr(a) ab = np.hstack((b,a)) fullx = np.hstack((-xarray[::-1],xarray)) #plot fig = plt.figure() fig.suptitle("weighting potential") ax = plt.subplot(1,1,1) cs = plt.contour(fullx,yarray,ab,10, colors='k') labelpos = np.dstack((np.zeros(9),np.arange(0.1,1,0.1)))[0] plt.clabel(cs,inline=true, fmt='%1.1f',fontsize=9, manual=labelpos) plt.show()
this expected behavior.
it picks closest contour curve each x, y
data coordinate contained in manual
parameter. when same contour curve found many coordinates, may happen start agglomerate, in case.
if used:
y_pick = [0.01, 0.025, 0.05, 0.075, 0.1, 0.15, 0.2, 0.3, 0.5] labelpos = ((0, i) in y_pick)
you like:
out of topic:
you can vectorize code avoiding relatively slow for
loops:
import numpy np import matplotlib.pyplot plt = 0.2 def fwp(x, y, a): return (1./np.pi*np.arctan(np.sin(np.pi*y)*np.sinh(np.pi*a/2.)/ (np.cosh(np.pi*x)-np.cos(np.pi*y)*np.cosh(np.pi*a/2.)))) resolution = 100 xarray = np.linspace(0, 0.25, num=resolution) yarray = np.linspace(0, 1, num=resolution) x, y = np.meshgrid(xarray, yarray, copy=false) = fwp(x, y, a) a[a<=0] += 1 b = np.fliplr(a) ab = np.hstack((b, a))
Comments
Post a Comment