2012-11-08 83 views
2

我需要帮助编写一个程序,该程序从文本文件中读取大约300行,并从特定分配(A1列)获取分数,然后使用该分配中的分数绘制直方图快挂。使用python和quickdraw绘制直方图

ID , Last, First, Lecture, Tutorial, A1, A2, A3, A4, A5 
8959079, Moore, Maria, L01, T03, 9.0, 8.5, 8.5, 10.0, 8.5 
4295498, Taylor, John, L00, T04, 10.0, 6.5, 8.5, 9.5, 7.0 
9326386, Taylor, David, L00, T00, 9.5, 8.0, 8.0, 9.0, 10.0 
7223234, Taylor, James, L01, T03, 8.5, 5.5, 10.0, 0.0, 0.5 
7547838, Miller, Robert, L01, T09, 7.0, 8.0, 8.5, 10.0, 0.5 
0313453, Lee, James, L01, T01, 10.0, 0.5, 8.0, 7.0, 5.0 
3544072, Lee, Helen, L00, T03, 10.0, 9.0, 7.0, 9.0, 8.5 

到目前为止,我有从文件(A1)中提取的成绩,并把它变成一个列表,然后创建另一个计数一定品位的许多事件如何发生的代码。我现在有麻烦了,现在使用这个列表并输入到quickdraw绘制直方图?

def file(): 
    file = open('sample_input_1.txt', 'r') 
    col = [] data = file.readlines() 
    for i in range(1,len(data)-1): 
    col.append(int(float(data[i].split(',')[5]))) 
    return col 

def hist(col): 
    grades = [] 
    for i in range(11): 
    grades.append(0) 
    for i in (col): 
    grades[i] += 1 
    return grades 

col = file() 
grades = hist(col) 
print(col) 
print(grades) 
+1

您需要发布一些代码,还有一个更具体的问题 - 是否有错误?你看到什么与你的期望不符? – thegrinner

+0

DEF文件(): \t文件=打开( 'sample_input_1.txt', 'R') \t COL = [] \t数据= file.readlines() \t对于i在范围(1,LEN(数据) -1): \t \t col.append(INT(浮动(数据[I] .split( '')[5]))) \t返回COL DEF HIST(COL):\t \t \t 等级= [] \t对于i在范围(11): \t \t grades.append(0) \t对于i在(COL): \t \t等级[I] + = 1个\t \t返回等级 \t COL =文件() 等级= HIST(COL) 打印(COL) 打印(等级) –

+0

这就是到目前为止我的代码的广告即时知道如何从等级列表中的数据DISPLY到了柱状图,快挂 –

回答

0

这是怎么回事?

s = """ID , Last, First, Lecture, Tutorial, A1, A2, A3, A4, A5 
8959079, Moore, Maria, L01, T03, 9.0, 8.5, 8.5, 10.0, 8.5 
4295498, Taylor, John, L00, T04, 10.0, 6.5, 8.5, 9.5, 7.0 
9326386, Taylor, David, L00, T00, 9.5, 8.0, 8.0, 9.0, 10.0 
7223234, Taylor, James, L01, T03, 8.5, 5.5, 10.0, 0.0, 0.5 
7547838, Miller, Robert, L01, T09, 7.0, 8.0, 8.5, 10.0, 0.5 
0313453, Lee, James, L01, T01, 10.0, 0.5, 8.0, 7.0, 5.0 
3544072, Lee, Helen, L00, T03, 10.0, 9.0, 7.0, 9.0, 8.5""" 

from StringIO import StringIO 

c = StringIO(s) 
a = loadtxt(c, delimiter=',', dtype='S8') 
A1 = a[1:, 5].astype('float32') 
print A1 
hist(A1, bins=10) 

输出:

[ 9. 10. 9.5 8.5 7. 10. 10. ] 
Out[81]: 
(array([1, 0, 0, 0, 0, 1, 1, 0, 1, 3]), 
array([ 7. , 7.3, 7.6, 7.9, 8.2, 8.5, 8.8, 9.1, 9.4, 
     9.7, 10. ]), 

enter image description here

的第一列表是A1的输出,其中我做了位处理的对浮子传递给histhist是一个matplotlib函数,分别打印直方图值和边缘。

+0

这一个任务是在课堂上这样不允许使用matplotlib的分配,我们还没有做过阵列又是那么不知道如何工作的 –

+0

哦,对不起。我错过了quickdraw的部分。 –

+0

其他建议 –

9

Quickdraw不支持绘制图开箱即用,所有矩形,网格,文本都必须自己映射。更好的方法是使用已存在的python库。不要试图重新发明轮子。

例1 QuickDraw的解决方案

#!/bin/python 

# Quickdraw histogram: 
# Assume max grade is 10 

A1 = [9.0,10.0,9.5,8.5,7.0,10.0,10.0] 

histogram = [] 
for i in sorted(set(A1)): histogram.append([int(i*50),A1.count(i)]) 

gridsize = 500 
griddiv = 20 
topleft = 50 

#graph title 
print 'text', '"','Histogram of Grades','"', 220, 25 

#x axis title 
for i in range(1,21): 
    print 'text', '"',float(i)/2,'"', (i+1)*25, 570 

#y axix title 
for i in range(0,11): 
    print 'text', '"',i,'"', 25, 600-(i+1)*50 

#grid 
print 'grid', topleft, topleft, gridsize, gridsize, griddiv, griddiv 

#chart rectangles 
print 'color 140 0 0' 
for i in histogram: 
    print 'fillrect',i[0]-25+topleft, gridsize-(50*i[1])+topleft,gridsize/griddiv,50*i[1],'b'+str(i[0]) 
    print 'fillrect', 'color','b'+str(i[0]) 

这里是图的样子运行histogram.py | java -jar quickdraw.jar它不漂亮了!

enter image description here

该解决方案确实是可怕的。代码本质上是混乱的(我可以做很多事情来提高可读性和灵活性,但它无论如何都证明了这个概念)。缩放不是句柄,你需要300个学生的记录,每个年级的记数会大于10.更不用说它看起来很可怕。它可以得到改进,例如通过在每个矩形周围绘制白线会有小的改进,但您需要进行所有计算。


实施例2 MATPLOTLIB解

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.mlab as mlab 

# You already have A1 from the file in a list like this: 
A1 = [9.0,10.0,9.5,8.5,7.0,10.0,10.0] 

#Set up infomation about histogram and plot using A1 
fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.hist(A1, 12,facecolor='red') 
ax.set_title('Grade Histogram') 
ax.set_xlabel('Grade') 
ax.set_ylabel('Count') 
ax.set_xlim(min(A1)-0.5, max(A1)+0.5) 
ax.set_ylim(0, max([A1.count(i) for i in sorted(set(A1))])+0.5) 
ax.grid(True) 
plt.show() 

输出:

enter image description here

这是最好的解决方案,该缩放处理,图形看起来优异。


例3简单的CLI

我甚至会退后一步,做一个简单的CLI版本,不要试图和运行你不能走路了。

A1 = [9.0,10.0,9.5,8.5,7.0,10.0,10.0] 

upper =2*int(max(A1))+1 
lower =2*int(min(A1))-1 

for i in [x * 0.5 for x in range(lower,upper)]: 
    print i,'\t|' ,'*'*A1.count(i) 

输出:

Grade Histogram 
6.5  | 
7.0  | * 
7.5  | 
8.0  | 
8.5  | * 
9.0  | * 
9.5  | * 
10.0 | *** 

该解决方案是初级程序员一个很好的开始!它很简单,干净,甚至缩放不应该是一个问题(只要增加终端窗口的宽度,如果酒吧变长)。