-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdraw_pic.py
101 lines (88 loc) · 2.61 KB
/
draw_pic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 26 09:42:46 2017
@author: Hank Kuang
@title: 统计图形
"""
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import matplotlib as mpl
import numpy as np
def drawPie(s, labels=None, dropna=True):
"""
Pie Plot for s
-------------------------------------
Params
s: pandas Series
lalels:labels of each unique value in s
dropna:bool obj
-------------------------------------
Return
show the plt object
"""
counts = s.value_counts(dropna=dropna)
if labels is None:
labels = counts.index
fig1, ax1 = plt.subplots()
ax1.pie(counts, labels=labels, autopct='%1.2f%%', shadow=True, startangle=90)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.show()
def drawBar(s, x_ticks=None, pct=False, dropna=False, horizontal=False):
"""
bar plot for s
-------------------------------------------
Params
s: pandas Series
x_ticks: list, ticks in X axis
pct: bool, True means trans data to odds
dropna: bool obj,True means drop nan
horizontal: bool, True means draw horizontal plot
-------------------------------------------
Return
show the plt object
"""
counts = s.value_counts(dropna=dropna)
if pct == True:
counts = counts / s.shape[0]
ind = np.arange(counts.shape[0])
if x_ticks is None:
x_ticks = counts.index
if horizontal == False:
p = plt.bar(ind, counts)
plt.ylabel('frequecy')
plt.xticks(ind, tuple(counts.index))
else:
p = plt.barh(ind, counts)
plt.xlabel('frequecy')
plt.yticks(ind, tuple(counts.index))
plt.title('Bar plot for %s' % s.name)
plt.show()
def drawHistogram(s, num_bins=20, save=False, filename='myHist'):
"""
plot histogram for s
---------------------------------------------
Params
s: pandas series
num_bins: number of bins
save: bool, is save?
filename��png name
---------------------------------------------
Return
show the plt object
"""
fig, ax = plt.subplots()
mu = s.mean()
sigma = s.std()
# the histogram of the data
n, bins, patches = ax.hist(s, num_bins, normed=1)
# add a 'best fit' line
y = mlab.normpdf(bins, mu, sigma)
ax.plot(bins, y, '--')
ax.set_xlabel(s.name)
ax.set_ylabel('Probability density')
ax.set_title(r'Histogram of %s: $\mu=%.2f$, $\sigma=%.2f$' % (s.name, mu, sigma))
# Tweak spacing to prevent clipping of ylabel
fig.tight_layout()
if save:
plt.savefig(filename + '.png')
plt.show()