2012-11-08 97 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 | *** 

該解決方案是初級程序員一個很好的開始!它很簡單,乾淨,甚至縮放不應該是一個問題(只要增加終端窗口的寬度,如果酒吧變長)。