Created
May 18, 2015 14:11
-
-
Save roger35972134/597151d4d2a06ffe71f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from matplotlib import pyplot | |
from collections import namedtuple | |
from pprint import pprint as pp | |
from math import floor | |
#由於有點摸不著頭緒,所以參考了一下同學的程式碼,我下次會更多自己的想法 | |
Stem = namedtuple('Stem', 'data, leafdigits') | |
data0 = Stem((4285,564,1278,205,3920, | |
2066,604,209,602,1379, | |
2584,14,349,3770,99, | |
1009,4152,478,726,510, | |
318,737,3032,3894,582, | |
1429,852,1461,2662,308, | |
981,1560,701,497,3367, | |
1402,1786,1406,35,99, | |
1137,520,261,2778,373, | |
414,396,83,1379,454) | |
,3.0) | |
def stemplot(stem): | |
d = [] | |
interval = int(10**int(stem.leafdigits)) | |
for data in sorted(stem.data): | |
data = int(floor(data)) | |
stm, lf = divmod(data,interval) | |
d.append( (int(stm), int(lf)) ) | |
stems, leafs = list(zip(*d)) | |
stemwidth = max(len(str(x)) for x in stems) | |
leafwidth = max(len(str(x)) for x in leafs) | |
laststem, out = min(stems) - 1, [] | |
for s,l in d: | |
while laststem < s: | |
laststem += 1 | |
out.append('\n%*i |' % ( stemwidth, laststem)) | |
out.append(' %0*i' % (leafwidth, l)) | |
out.append('\n\nKey:\n Stem multiplier: %i\n X | Y => %i*X+Y\n' | |
% (interval, interval)) | |
return ''.join(out) | |
def 長條圖(data): | |
pyplot.subplot(2,1,1).hist(data) | |
def 箱型圖(data): | |
pyplot.subplot(2,1,2).boxplot(data) | |
pyplot.show() | |
if __name__ == '__main__': | |
data = (4285,564,1278,205,3920, | |
2066,604,209,602,1379, | |
2584,14,349,3770,99, | |
1009,4152,478,726,510, | |
318,737,3032,3894,582, | |
1429,852,1461,2662,308, | |
981,1560,701,497,3367, | |
1402,1786,1406,35,99, | |
1137,520,261,2778,373, | |
414,396,83,1379,454) | |
print( stemplot(data0) ) | |
長條圖(data) | |
箱型圖(data) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment