输入数据以在Quickdraw中创建直方图

时间:2012-11-08 20:09:10

标签: python animation histogram quickdraw

我被困在一个问题上。如果我在列表中有一组数据(比如说学生数量及其对应的等级)我如何编写代码以在快速绘制中创建直方图?

1 个答案:

答案 0 :(得分:1)

Quickdraw开箱即用支持图表。一切都需要自己绘制和映射,这是一个例子:

#!/bin/python 

import random

#create 40 random grades 
grades = [(float(int(float(20*random.random())))/2)+0.5 for i in xrange(40)]

#count the occurrence of each grade
histogram = []
for i in sorted(set(grades)): histogram.append([int(i*50),grades.count(i)])

#some grid information
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 0 140 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并运行python histogram.py | java -jar quickdraw.jar,请注意它不是很漂亮!更好的方法是使用python库matplotlib

enter image description here