-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
72 lines (60 loc) · 3.23 KB
/
Copy pathutils.py
File metadata and controls
72 lines (60 loc) · 3.23 KB
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
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.cm as cm, matplotlib.font_manager as fm
import matplotlib.ticker as mtick
from matplotlib import rc
title_font = fm.FontProperties(family='Bitstream Vera Sans', style='normal', size=15, weight='normal', stretch='normal')
label_font = fm.FontProperties(family='Bitstream Vera Sans', style='normal', size=12, weight='normal', stretch='normal')
ticks_font = fm.FontProperties(family='Bitstream Vera Sans', style='normal', size=10, weight='normal', stretch='normal')
annotation_font = fm.FontProperties(family='Bitstream Vera Sans', style='normal', size=10, weight='normal', stretch='normal')
axis_bgcolor = '#f0f0f0'
# function to produce more beautiful pie charts with matplotlib
def gbplot_pie(fractions, #values for the wedges
labels, #labels for the wedges
title = '', #title of the pie chart
cm_name = 'Pastel1', #name of the matplotlib colormap to use
autopct = '%1.1f%%', #format the value text on each pie wedge
labeldistance = 1.1, #where to place wedge labels in relation to pie wedges
shadow = True, #shadow around the pie
startangle = 90, #rotate 90 degrees to start the top of the data set on the top of the pie
edgecolor = 'w', #color of pie wedge edges
width = 8, #width of the figure in inches
height = 10, #height of the figure in inches
grouping_threshold = None, #group all wedges below this value into one 'all others' wedge
grouping_label = None): #what the label the grouped wedge
# if the user passed a threshold value, group all fractions lower than it into one 'misc' pie wedge
if not grouping_threshold==None:
# if user didn't pass a label, apply a default text
if grouping_label == None:
grouping_label = 'Others'
# select the rows greater than the cutoff value
row_mask = fractions > grouping_threshold
meets_threshold = fractions[row_mask]
# group all other rows below the cutoff value
all_others = pd.Series(fractions[~row_mask].sum())
all_others.index = [grouping_label]
# append the grouped row to the bottom of the rows to display
fractions = meets_threshold.append(all_others)
labels = fractions.index
# get the color map then pull 1 color from it for each pie wedge we'll draw
color_map = cm.get_cmap(cm_name)
num_of_colors = len(fractions)
colors = color_map([x/float(num_of_colors) for x in range(num_of_colors)])
# create the figure and an axis to plot on
fig, ax = plt.subplots(figsize=[width, height])
# plot the pie
wedges = ax.pie(fractions,
labels = labels,
labeldistance = labeldistance,
autopct = autopct,
colors = colors,
shadow = shadow,
startangle = startangle)
# change the edgecolor for each wedge
for wedge in wedges[0]:
wedge.set_edgecolor(edgecolor)
# set the title and show the plot
ax.set_title(title, fontproperties=title_font)
ax.axis('equal')
plt.tight_layout()
plt.show()