断言错误:尺寸不兼容:参数“高度"必须是长度 2 或标量(Matplotlib、Python 2.7、绘制图表) [英] AssertionError: incompatible sizes: argument 'height' must be length 2 or scalar (Matplotlib, Python 2.7, drawing charts)
问题描述
新的一天给 Python 带来了新问题,不幸的是 :/
我有一个用Java编写的其他应用程序生成的文件.这个应用程序用一些数据生成文件,这是一种随机的东西,因为我不能说每个文件有多少行.示例文件如下所示:
3 Sat Jan 21 00:00:00 20122012年3月11日,星期日7:002010年1月5日星期五10:00:002011 年 2 月 5 日星期六 00:00:008周日2010年4月11日00:00:002011 年 8 月 24 日星期三 00:00:002010 年 2 月 20 日星期六 00:00:002011 年 10 月 3 日星期四 00:00:002010 年 12 月 17 日星期五 00:00:002010 年 7 月 20 日星期二 00:00:002011 年 12 月 2 日星期五 00:00:002010年5月31日星期一00:00:005 2011 年 5 月 16 日星期一 00:00:002012年4月30日星期一00:00:003 2010年10月28日星期四00:00:002012 年 6 月 19 日星期二 00:00:002010年9月8日星期三00:00:00
我想使用此数据绘制图表.在 X 轴上,我想要格式化日期,并在我的文件第一列的 Y 轴上编号.这是我可爱的 python 代码:
# -*- 编码:utf-8 -*-#!/usr/bin/env python进口 wx导入 matplotlibmatplotlib.use("TkAgg")将 matplotlib.pyplot 导入为 pl导入日期时间def monthNum(month):如果month =="Jan":返回1elif 月 == "二月" :返回 2elif 月 == "三月" :返回3elif month =="Apr":返回 4elif 月 == "五月" :返回5Elif month =="Jun":返回6elif 月 == "七月" :返回7elif month =="Aug":返回8elif month =="Sep":返回9elif month =="Oct":返回 10elif month =="Nov":返回11elif 月份 == "Dec" :返回12定义转换日期(日期时间):行= dateTime.split('')日期=(str(line [2])+-" + str(monthNum(line [1]))+-" + str(line [4]))归期def readFile(文件名):值= []日期 = []尝试 :使用open(filename,"r")作为openFile:对于openFile中的行:line = line.strip()数据 = line.split("\t")values.append(int(data [0]))newDate = convertDate(data [1])date.append(datetime.datetime.strptime(newDate,%d-%m-%Y").date())除了 IOError :打印("IOERROR")除了ValueError:打印("VALUE ERROR")如果 len(values) != 0 和 len(dates) != 0 :drawChart(值,日期,文件名)def drawChart(值,日期,文件名):fig = pl.figure(dpi=60,figsize=(18, 10))斧= fig.add_subplot(1,1,1)fig.subplots_adjust(左= 0.1,右= 0.95,顶部= 0.95,底部= 0.2)ax.bar(范围(len(日期)),值,facecolor ='#777777',align ='center',width = 0.5,ecolor ='black')pl.axis('紧')ax.set_xticks(range(len(dates)))pl.yticks(值)ax.set_xticklabels(日期,旋转= 90)pl.savefig(文件名+".png")pl.show()pl.close()读取文件(文件.txt")
一切都很好,如果 file.txt
仅包含一行.当它有更多行时,python代码给我一个错误:
值错误回溯(最近一次调用最后一次):< module>中的文件"test.py",第71行读取文件(文件.txt")文件test.py",第 56 行,在 readFile 中drawChart(值,日期,文件名)在drawChart中的文件"test.py",第62行ax.bar(range(len(dates)), values, facecolor='#777777', align='center', width=0.5, ecolor='black')文件"/usr/lib/pymodules/python2.7/matplotlib/axes.py",第4733行,在栏中nbars)断言错误:大小不兼容:参数高度"必须是长度 2 或标量
我真的不知道如何解决它.很好,如果 file.txt
有一行,但正如我之前写的:我不能说 file.txt
有多少行(这取决于我的Java应用程序.)
有人吗?我在 Kubuntu 12.04 上使用 Python 2.7 和 Matplotlib.
这是因为日期只有2个值.日期的长度和值的长度必须相同,matplotlib 才能知道要做什么.如果值是标量,则所有条形图的高度都相同
A new day brought new problem with Python, unfortunately :/
I have a file generated by my other app written in Java. This app generates files with some data, it's kind a random stuff 'cause I'm not able to say how many rows each file would have. Example file looks like this:
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
And I want to draw a chart with use of this data. On X axis I would like to have formatted dates, and on Y axis numbers from the first column of my file. Heres my lovely python code:
# -*- 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")
Everything is fine, if file.txt
had a one, single row. When it has more rows, python code gives me an error:
VALUE ERROR Traceback (most recent call last): File "test.py", line 71, in <module> readFile("file.txt") File "test.py", line 56, in readFile drawChart(values, dates, filename) File "test.py", line 62, in drawChart ax.bar(range(len(dates)), values, facecolor='#777777', align='center', width=0.5, ecolor='black') File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 4733, in bar nbars) AssertionError: incompatible sizes: argument 'height' must be length 2 or scalar
And I don't really know how to fix it. Its fine, if file.txt
had one row but as I wrote earlier: I'm not able to say how many rows would file.txt
have (it depends on my Java app).
Anybody? I use Python 2.7 and Matplotlib on Kubuntu 12.04.
It's because dates only has 2 values. The length of dates and the length of values must be the same for matplotlib to know what to do. If values was a scalar then all the bars would have the same height
这篇关于断言错误:尺寸不兼容:参数“高度"必须是长度 2 或标量(Matplotlib、Python 2.7、绘制图表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!