使用python和quickdraw绘制直方图

tom*_*th 2 python 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
Run Code Online (Sandbox Code Playgroud)

到目前为止,我有一个代码从文件中提取等级(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) 
Run Code Online (Sandbox Code Playgroud)

Chr*_*our 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])
Run Code Online (Sandbox Code Playgroud)

这是图表在运行之后的样子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()
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述

这是最好的解决方案,处理缩放并且图形看起来很棒.


示例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)
Run Code Online (Sandbox Code Playgroud)

输出:

Grade Histogram
6.5     | 
7.0     | *
7.5     | 
8.0     | 
8.5     | *
9.0     | *
9.5     | *
10.0    | ***
Run Code Online (Sandbox Code Playgroud)

对于初学者来说,这个解决方案是一个很好的开始!它简单,干净,甚至缩放都不应成为问题(如果条形变长,只需增加终端窗口的宽度).