2014-07-17 5 views
10

Nun, ich denke matplotlib heruntergeladen wurde, aber mit meinem neuen Skript bekomme ich diesen Fehler:Wie ein Histogramm aus einer Liste von Daten machen

/usr/lib64/python2.6/site-packages/matplotlib/backends/backend_gtk.py:621:  DeprecationWarning: Use the new widget gtk.Tooltip 
    self.tooltips = gtk.Tooltips() 
Traceback (most recent call last): 
    File "vector_final", line 42, in <module> 
plt.hist(data, num_bins) 
    File "/usr/lib64/python2.6/site-packages/matplotlib/pyplot.py", line 2008, in hist 
ret = ax.hist(x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, **kwargs) 
    File "/usr/lib64/python2.6/site-packages/matplotlib/axes.py", line 7098, in hist 
w = [None]*len(x) 
TypeError: len() of unsized object 

Und mein Code: #/usr/bin/python

l=[] 
with open("testdata") as f: 
    line = f.next() 
    f.next()# skip headers 
    nat = int(line.split()[0]) 
    print nat 

    for line in f: 
     if line.strip(): 
      if line.strip(): 
      l.append(map(float,line.split()[1:])) 


    b = 0 
    a = 1 
for b in range(53): 
    for a in range(b+1,54): 
     import operator 
     import matplotlib.pyplot as plt 
     import numpy as np 

     vector1 = (l[b][0],l[b][1],l[b][2]) 
     vector2 = (l[a][0],l[a][1],l[a][2]) 

      x = vector1 
      y = vector2 
      vector3 = list(np.array(x) - np.array(y)) 
      dotProduct = reduce(operator.add, map(operator.mul, vector3, vector3)) 


     dp = dotProduct**.5 
     print dp 

     data = dp 
     num_bins = 200 # <- number of bins for the histogram 
     plt.hist(data, num_bins) 
     plt.show() 

aber der Code, das ist mir der Fehler immer ist der neue Zusatz, dass ich hinzugefügt, die der letzte Teil ist, im folgenden wiedergegeben:

   data = dp 
       num_bins = 200 # <- number of bins for the histogram 
       plt.hist(data, num_bins) 
       plt.show() 
+1

setzen Sie Ihre 'import' Aussagen bei der Anfang Ihrer Datei, nicht in Schleifen – MattDMo

+0

Okay, ich habe es getan, es hat nichts repariert, aber ich vermute, das ist nur eine Effizienzsache? –

+0

Effizienz (teilweise) und Stil auch. – MattDMo

Antwort

20

do you have any idea how to make 200 evenly spaced out bins, and have your program store the data in the appropriate bins?

Sie können zum Beispiel NumPys arange für eine feste Bin-Größe (oder Pythons Standard-Range-Objekt) und NumPys linspace für gleichmäßig beabstandete Bin verwenden. Hier sind zwei einfache Beispiele aus meiner matplotlib gallery

Fest Binabmessung

import numpy as np 
import random 
from matplotlib import pyplot as plt 

data = np.random.normal(0, 20, 1000) 

# fixed bin size 
bins = np.arange(-100, 100, 5) # fixed bin size 

plt.xlim([min(data)-5, max(data)+5]) 

plt.hist(data, bins=bins, alpha=0.5) 
plt.title('Random Gaussian data (fixed bin size)') 
plt.xlabel('variable X (bin size = 5)') 
plt.ylabel('count') 

plt.show() 

enter image description here

Feste Anzahl von Bins

import numpy as np 
import math 
from matplotlib import pyplot as plt 

data = np.random.normal(0, 20, 1000) 

bins = np.linspace(math.ceil(min(data)), 
        math.floor(max(data)), 
        20) # fixed number of bins 

plt.xlim([min(data)-5, max(data)+5]) 

plt.hist(data, bins=bins, alpha=0.5) 
plt.title('Random Gaussian data (fixed number of bins)') 
plt.xlabel('variable X (20 evenly spaced bins)') 
plt.ylabel('count') 

plt.show() 

enter image description here

+1

Ich fand das sehr hilfreich. Ich habe die Zeilen "zufällig importieren" gelöscht, ohne irgendwelche negativen Auswirkungen zu entdecken. Wird es hier wirklich gebraucht? Ich nehme an, dass wir eine Funktion namens random.normal nennen, aber wenn ich das Skript richtig verstehe, ist diese Funktion Teil des numpigen Moduls. –

+0

Ich bin froh, dass dies hilfreich war! Guter Punkt, die Zeile 'import random' sah aus wie ein veralteter Import und wurde in diesem Code-Snippet nicht verwendet. Bearbeitet in der Antwort. Vielen Dank! – Sebastian

Verwandte Themen