2014-09-24 15 views
5

Ich versuche, dem Konturplot im folgenden Code manuelle Beschriftungen hinzuzufügen. Die Etiketten sind etwas zufällig gedruckt. Hat jemand eine Idee, wie das zu beheben ist? Es scheint ein Fehler in Matplotlib zu sein.Etiketten manuell in Matplotlib Konturplot falsch einstellen

Grüße, David

import numpy as np 
import matplotlib.pyplot as plt 

a = 0.2 

resolution = 100 
xarray = np.linspace(0,0.25,num=resolution) 
yarray = np.linspace(0,1,num=resolution) 

A = np.empty([resolution,resolution]) 

xc = 0 
yc = 0 
for x in xarray: 
    for 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 = 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() 

enter image description here

+1

Wenn Sie versuchen, mehr als 5 Etiketten zu verwenden, wird die erste (0,1) an der oberen Kurve wiederholt. mit 'np.dstack ((np.zeros (4), np.arange (0.1,0.5,0.1))) [0]' erhalten Sie eine korrekte Zahl. es scheint mir auch ein Fehler – joaquin

Antwort

2

Dies ist das erwartete Verhalten.

Es wählt die engste Konturkurve für jede x, y Datenkoordinate, die im Parameter manual enthalten ist. Wenn dieselbe Konturkurve für viele Koordinaten gefunden wird, kann es vorkommen, dass sie beginnen zu agglomerieren, wie in Ihrem Fall.

Wenn Sie verwendet:

y_pick = [0.01, 0.025, 0.05, 0.075, 0.1, 0.15, 0.2, 0.3, 0.5] 
labelpos = ((0, i) for i in y_pick) 

Sie erhalten würden so etwas wie:

enter image description here


Aus Thema:

Sie Ihren Code zu vermeiden, die relativ langsame vektorisieren können for Schleifen:

import numpy as np 
import matplotlib.pyplot as plt 

a = 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) 
A = fwp(x, y, a) 
A[A<=0] += 1 
B = np.fliplr(A) 
AB = np.hstack((B, A)) 
+0

Danke für die Antwort und Tipp! – user338095

Verwandte Themen