簡體   English   中英

使用python和quickdraw繪制直方圖

[英]Plotting a histogram using python and quickdraw

我需要幫助編寫一個程序,該程序從文本文件中讀取大約300行並從特定作業中獲取成績(列A1),然后使用該作業中的成績來繪制快速繪制中的直方圖。

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) 

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后圖表的樣子 histogram.py | java -jar quickdraw.jar它不漂亮!

在此輸入圖像描述

這個解決方案真的太可怕了。 代碼本質上是混亂的(我當然可以做很多事情來提高可讀性和靈活性,但無論如何它都證明了這個概念)。 縮放不是處理,你將需要,因為有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()

輸出:

在此輸入圖像描述

這是最好的解決方案,處理縮放並且圖形看起來很棒。


示例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    | ***

對於初學者來說,這個解決方案是一個很好的開始! 它簡單,干凈,甚至縮放都不應成為問題(如果條形變長,只需增加終端窗口的寬度)。

怎么樣才能讓你前進?

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. ]),

在此輸入圖像描述

第一個列表是A1的輸出,其中我做了一些處理以將浮點數傳遞給hist hist是一個matplotlib函數,分別打印直方圖值和邊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM