2012-01-30 16 views
7

ile bir çizgi üzerinde etiketleme noktaları (x, y) pyplot kullanarak 2d arsa çalışıyorum. Her biri 1 ile 10 arasında 100 değer içeren birkaç sütun içeren bir dosyada okuyorum. Sütun 5'i sütun 6'ya göre çiziyorum, bu da iyi.Matplotlib - bir değer z

ne de yapmak isteyen kolon 0 bir tam sayı olduğu konumlarda (x, y), Böylece satır 11 puan olacak Sütun 0 olan bir tamsayıdır değerleri ile elde edilen çizgi etikettir. Ayrıca bu noktaların o tamsayı ile işaretlenmesini istiyorum.

Gerçekten onun beni deli bu konuda yardıma takdir ediyorum!

+0

Şimdiye kadar denedim ne var? Başka bir deyişle, neyin işe yarayıp neyin işe yaramadığını gösteren bir örnek kodunuz var mı (ya da nerede kaybettiğiniz). Sorunuzu düzenlerken kodu biçimlendirebileceğinizi unutmayın ... – Yann

+2

Ne denediniz? Şu ana kadar bir kod pasajı verebilir misiniz? vals [vals% 1 == 0] 'tamsayıları bulur ve [' matplotlib.pyplot.text'] 'i kullanabilirsiniz (http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.text) metin çizmek için. Tüm x, y 'çiftleri için' plt.text (x, y, str (y)) '. –

cevap

26

Sorunuzdan, tam olarak ne yapmak istediğinizi tam olarak% 100 değilim.

Her tepe noktasını bir satırda etiketlemek mi istiyorsunuz? Yoksa sadece tamsayı olan köşeleri etiketlemek mi istiyorsunuz? Veya tamsayı "geçişlerin" satır boyunca hizalanacağı ve bunları etiketleyeceği yerlere mi geçmek istiyorsunuz? Zaten değilseniz

Öncelikle, metin dosyasının yüklenmesi için, numpy.loadtxt içine bakmak. En basit seçenek hızlı bir örnek (her köşe etiketleme) gibi herhangi bir oranda

z, x, y = np.loadtxt('data.txt', usecols=[0, 5, 6]).T 

: sizin özel durumda, böyle bir şey yapabileceğini

import numpy as np 
import matplotlib.pyplot as plt 

x = np.arange(10) 
y = 2 * x 
z = x ** 2 

fig, ax = plt.subplots() 
ax.plot(x, y, 'bo-') 

for X, Y, Z in zip(x, y, z): 
    # Annotate the points 5 _points_ above and to the left of the vertex 
    ax.annotate('{}'.format(Z), xy=(X,Y), xytext=(-5, 5), ha='right', 
       textcoords='offset points') 

plt.show() 

Şimdi enter image description here

, ikinci seçenek için, bunun gibi daha fazla bir şeye sahip olabiliriz (@ mathematical.coffee önerisine benzer):

import numpy as np 
import matplotlib.pyplot as plt 

x = np.arange(-0.6, 5.6, 0.2) 
y = 2 * x 
z = x**2 

fig, ax = plt.subplots() 
ax.plot(x, y, 'bo-') 

# Note the threshold... I'm assuming you want 1.000001 to be considered an int. 
# Otherwise, you'd use "z % 1 == 0", but beware exact float comparisons!! 
integers = z % 1 < 1e-6 
for (X, Y, Z) in zip(x[integers], y[integers], z[integers]): 
    ax.annotate('{:.0f}'.format(Z), xy=(X,Y), xytext=(-10, 10), ha='right', 
       textcoords='offset points', 
       arrowprops=dict(arrowstyle='->', shrinkA=0)) 

plt.show() 

enter image description here

+0

Burada bir hata alıyorum: 'TypeError: __init 1 gerekli konumsal bağımsız değişkeni eksik __(): 's'' bu çizgide' arrowprops = dict (arrowstyle =' ->', shrinkA = 0) ' – saitam