首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python和quickdraw绘制直方图

使用python和quickdraw绘制直方图
EN

Stack Overflow用户
提问于 2012-11-09 05:46:47
回答 2查看 1.9K关注 0票数 2

我需要帮助编写一个程序,从文本文件中读取约300行,并从一个特定的作业(列A1)的成绩,然后使用该作业的成绩绘制在快速绘图直方图。

代码语言:javascript
复制
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中绘制直方图时遇到了问题?

代码语言:javascript
复制
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) 
EN

回答 2

Stack Overflow用户

发布于 2012-11-09 17:15:27

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

示例1快速绘制解决方案

代码语言:javascript
复制
#!/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后的图形,它不是很漂亮!

这个解决方案真的很糟糕。代码本质上是杂乱无章的(当然,我可以做很多事情来提高可读性和灵活性,但它证明了这个概念)。缩放不是处理,你需要处理300个学生记录,每个年级的数量将大于10,更不用说它看起来很可怕。它可以改进,例如,在每个矩形周围绘制白线将是一个小的改进,但您需要做所有的计算。

示例2 MATPLOTLIB解决方案

代码语言:javascript
复制
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版本,在你不能行走之前,不要尝试跑步。

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
Grade Histogram
6.5     | 
7.0     | *
7.5     | 
8.0     | 
8.5     | *
9.0     | *
9.5     | *
10.0    | ***

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

票数 9
EN

Stack Overflow用户

发布于 2012-11-09 06:39:49

这个可以让你继续前进吗?

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
[  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的输出,我在其中做了一些处理,以便将浮点数传递给histhist是一个matplotlib函数,分别打印直方图值和边缘。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13298375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档