Skip to content

Instantly share code, notes, and snippets.

@yishenggudou
Created April 26, 2013 10:27
Show Gist options
  • Save yishenggudou/5466336 to your computer and use it in GitHub Desktop.
Save yishenggudou/5466336 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2011 timger
# +Author timger
# +Gtalk&Email [email protected]
# +Msn [email protected]
# +Weibo @timger http://t.sina.com/zhanghaibo
# +twitter @yishenggudou http://twitter.com/yishenggudou
# Licensed under the MIT License, Version 2.0 (the "License");
from matplotlib import pyplot
from numpy import arange
import bisect
def scatterplot(x,y):
pyplot.plot(x,y,'b.')
pyplot.xlim(min(x)-1,max(x)+1)
pyplot.ylim(min(y)-1,max(y)+1)
pyplot.show()
def barplot(labels,data):
pos=arange(len(data))
pyplot.xticks(pos+1.4,labels)
pyplot.bar(pos,data)
pyplot.show()
def histplot(data,bins=None,nbins=5):
if not bins:
minx,maxx=min(data),max(data)
space=(maxx-minx)/float(nbins)
bins=arange(minx,maxx,space)
binned=[bisect.bisect(bins,x) for x in data]
l=['%.1f'%x for x in list(bins)+[maxx]] if space<1 else [str(int(x)) for x in list(bins)+[maxx]]
displab=[x+'-'+y for x,y in zip(l[:-1],l[1:])]
barplot(displab,[binned.count(x) for x in range(len(bins)+1)])
def barchart(x,y,numbins=5):
datarange=max(x)-min(x)
bin_width=datarange/numbins
pos=min(x)
bins=[0 for i in range(numbins+1)]
for i in range(numbins):
bins[i]=pos
pos+=bin_width
bins[numbins]=max(x)
binsum=[0 for i in range(numbins)]
bincount=[0 for i in range(numbins)]
binaverage=[0 for i in range(numbins)]
for i in range(numbins):
for j in range(len(x)):
if x[j]>=bins[i] and x[j]<bins[i+1]:
bincount[i]+=1
binsum[i]+=y[j]
for i in range(numbins):
binaverage[i]=binsum[i]/bincount[i]
barplot(range(numbins),binaverage)
def piechart(labels,data):
fig=pyplot.figure(figsize=(7,7))
pyplot.pie(data,labels=labels,autopct='%1.2f%%')
pyplot.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment