我想繪製給定csv文件中特定列的直方圖和散點圖。我是編程新手,我從朋友那裏得到了這段代碼,它顯然可以工作,但不知何故,我得到了這個錯誤。代碼:在python中繪製csv文件中的列的直方圖
import csv
import numpy as np
import matplotlib.pyplot as plt
f = open('Data for question 13.csv')
data = csv.reader(f)
Area = []; MajorAxisLength = []; MinorAxisLength = []; Perimeter = []
MinIntensity = []; MeanIntensity = []; MaxIntensity = []
header = [Area, MajorAxisLength, MinorAxisLength,Perimeter,MinIntensity,MeanIntensity,MaxIntensity]
for row in data:
i = 1
for name in header:
name.append(row[i])
i = i + 1
plt.figure()
plt.hist(Area, bins=50) # error follows after this
錯誤:
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
plt.hist(Area, bins=50, alpha=0.5)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/pyplot.py", line 2827, in hist
stacked=stacked, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 8312, in hist
xmin = min(xmin, xi.min())
File "/Library/Python/2.7/site-packages/numpy-1.9.0-py2.7-macosx-10.9-intel.egg/numpy/core/_methods.py", line 29, in _amin
return umr_minimum(a, axis, None, out, keepdims)
TypeError: cannot perform reduce with flexible type
我無法擺脫這種錯誤的。 答案應該很簡單,但由於我是新的,我不知道如何處理它。
我不能重現此。你的數據文件是什麼樣的? – matsjoyce
有沒有一種方法可以共享或上傳數據文件以查看數據的外觀? 我不知道這是否會給你任何想法,並會有所幫助:它有9個標題欄和數值數據列,直到約400行。 –
將前幾行粘貼到問題中(僅〜10),或使用例如。 [pastebin](http://pastebin.com),並把鏈接的查詢 – hitzg