断言错误:大小不兼容:参数';高度';长度必须为2或标量(Matplotlib、Python 2.7、绘图图表)

断言错误:大小不兼容:参数';高度';长度必须为2或标量(Matplotlib、Python 2.7、绘图图表),python,matplotlib,Python,Matplotlib,不幸的是,新的一天给Python带来了新的问题:/ 我有一个用Java编写的另一个应用程序生成的文件。这个应用程序生成带有一些数据的文件,这是一种随机的东西,因为我不能说每个文件有多少行。示例文件如下所示: 3 Sat Jan 21 00:00:00 2012 7 Sun Mar 11 00:00:00 2012 5 Fri Jan 1 00:00:00 2010 4 Sat Feb 5 00:00:00 2011 8 Sun Apr 11 00:00:00 2010 4

不幸的是,新的一天给Python带来了新的问题:/

我有一个用Java编写的另一个应用程序生成的文件。这个应用程序生成带有一些数据的文件,这是一种随机的东西,因为我不能说每个文件有多少行。示例文件如下所示:

3   Sat Jan 21 00:00:00 2012
7   Sun Mar 11 00:00:00 2012
5   Fri Jan  1 00:00:00 2010
4   Sat Feb  5 00:00:00 2011
8   Sun Apr 11 00:00:00 2010
4   Wed Aug 24 00:00:00 2011
8   Sat Feb 20 00:00:00 2010
3   Thu Oct 13 00:00:00 2011
9   Fri Dec 17 00:00:00 2010
4   Tue Jul 20 00:00:00 2010
8   Fri Dec  2 00:00:00 2011
6   Mon May 31 00:00:00 2010
5   Mon May 16 00:00:00 2011
8   Mon Apr 30 00:00:00 2012
3   Thu Oct 28 00:00:00 2010
1   Tue Jun 19 00:00:00 2012
7   Wed Sep  8 00:00:00 2010
def readFile(filename) :
    values = []
    dates = []
    openedFile = open(filename, "r")
    content = openedFile.readlines()
    openedFile.close()
    for line in content :
        line = line.strip()
        data = line.split("\t")
        values.append(int(data[0]))
        newDate = self.convertDate(data[1])
        dates.append(newDate)
    print(values)
    print(dates)
我想用这些数据画一张图表。在X轴上,我希望格式化日期,在Y轴上,我的文件第一列的数字。下面是我可爱的python代码:

# -*- coding: utf-8 -*-
#!/usr/bin/env python
import wx
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as pl
import datetime

def monthNum(month) :
        if month == "Jan" :
            return 1
        elif month == "Feb" :
            return 2
        elif month == "Mar" :
            return 3
        elif month == "Apr" :
            return 4
        elif month == "May" :
            return 5
        elif month == "Jun" :
            return 6
        elif month == "Jul" :
            return 7
        elif month == "Aug" :
            return 8
        elif month == "Sep" :
            return 9
        elif month == "Oct" :
            return 10
        elif month == "Nov" :
            return 11
        elif month == "Dec" :
            return 12

def convertDate(dateTime) :
        line = dateTime.split(' ')
        date = (str(line[2]) + "-" + str(monthNum(line[1])) + "-" + str(line[4]))
        return date

def readFile(filename) :
        values = []
        dates = []
        try :
                with open(filename, "r") as openedFile:
                        for line in openedFile :
                                line = line.strip()
                                data = line.split("\t")
                                values.append(int(data[0]))
                                newDate = convertDate(data[1])
                                dates.append(datetime.datetime.strptime(newDate, "%d-%m-%Y").date())
        except IOError :
                print("IOERROR")
        except ValueError :
                print("VALUE ERROR")
        if len(values) != 0 and len(dates) != 0 :
                drawChart(values, dates, filename)

def drawChart(values, dates, filename):
        fig = pl.figure(dpi=60,figsize=(18, 10))
        ax = fig.add_subplot(1,1,1)
        fig.subplots_adjust(left=0.1, right=0.95, top=0.95, bottom=0.2)
        ax.bar(range(len(dates)), values, facecolor='#777777', align='center', width=0.5, ecolor='black')
        pl.axis('tight')
        ax.set_xticks(range(len(dates)))
        pl.yticks(values)
        ax.set_xticklabels(dates, rotation = 90)
        pl.savefig(filename + ".png")
        pl.show()
        pl.close()

readFile("file.txt")
如果
file.txt
只有一行,那么一切都很好。当它有更多行时,python代码会给我一个错误:

值错误
回溯(最近一次呼叫最后一次):
文件“test.py”,第71行,在
readFile(“file.txt”)
readFile中第56行的文件“test.py”
绘图图(值、日期、文件名)
drawChart中第62行的文件“test.py”
最大条(范围(长度(日期)),值,面颜色='#777777',对齐=中心,宽度=0.5,颜色=黑色')
文件“/usr/lib/pymodules/python2.7/matplotlib/axes.py”,第4733行,条形图
(nbars)
AssertionError:大小不兼容:参数“高度”必须为长度2或标量
我真的不知道如何修复它。如果
file.txt
有一行就可以了,但正如我前面所写的:我无法说出
file.txt
有多少行(这取决于我的Java应用程序)


有人吗?我在Kubuntu 12.04上使用Python 2.7和Matplotlib。

这是因为日期只有2个值。日期的长度和值的长度必须相同,matplotlib才能知道该做什么。如果值是一个标量,那么所有的条都将具有相同的高度

谢谢,我想我已经解决了-问题在于
readFile(arg)
函数,它应该是这样的:

3   Sat Jan 21 00:00:00 2012
7   Sun Mar 11 00:00:00 2012
5   Fri Jan  1 00:00:00 2010
4   Sat Feb  5 00:00:00 2011
8   Sun Apr 11 00:00:00 2010
4   Wed Aug 24 00:00:00 2011
8   Sat Feb 20 00:00:00 2010
3   Thu Oct 13 00:00:00 2011
9   Fri Dec 17 00:00:00 2010
4   Tue Jul 20 00:00:00 2010
8   Fri Dec  2 00:00:00 2011
6   Mon May 31 00:00:00 2010
5   Mon May 16 00:00:00 2011
8   Mon Apr 30 00:00:00 2012
3   Thu Oct 28 00:00:00 2010
1   Tue Jun 19 00:00:00 2012
7   Wed Sep  8 00:00:00 2010
def readFile(filename) :
    values = []
    dates = []
    openedFile = open(filename, "r")
    content = openedFile.readlines()
    openedFile.close()
    for line in content :
        line = line.strip()
        data = line.split("\t")
        values.append(int(data[0]))
        newDate = self.convertDate(data[1])
        dates.append(newDate)
    print(values)
    print(dates)

变量“values”包含什么?@Sheena:它包含我文件第一列中的数字列表