用python绘制Matplotlib直方图

2024-05-23 19:03:30 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图用python绘制直方图时,遇到了一个错误。 你能帮我解决这个错误吗? 我认为这不是什么大问题,但我能找到解决办法。:(

代码

import matplotlib.pyplot as plt
import csv
import sys

def analyze():
#   datafile = 'test.csv'
    datafile = sys.argv[1]
    pieces = []
    with open(datafile, 'rt') as f:
        data = csv.reader(f,delimiter = '\t')
        for d in data:
            pieces.append(d)

    x = [op for op, response, interval in pieces]
    y1 = [interval for op, response, interval in pieces]


    plt.figure()
    plt.hist(y1)
    plt.show()

if __name__ == '__main__':
    analyze()

错误消息:

^{pr2}$

数据文件格式:

653070                232.93               104981.00
653071                277.94               104981.00
653072                232.93                12695.00
653073                232.93                25878.00
653074                232.93                32714.00
653075                232.93                19532.00
653076                232.93                19532.00
653077                232.93                32715.00
653078                232.93                32715.00
653079                232.93                45899.00
653080                232.93                65430.00
653081                232.93                65430.00
Continued .......
 ..........

Tags: csvinimportfordataresponseas错误
1条回答
网友
1楼 · 发布于 2024-05-23 19:03:30

试着调试你的代码。您将发现y1是一个字符串列表,因此plt.hist(y1)将引发

TypeError: len() of unsized object

TypeError raised when an operation or function is applied to an object of inappropriate type.

这意味着您应该使用floatint,因此请尝试运行以下命令:

^{pr2}$

相关问题 更多 >