Note
Go to the end to download the full example code
Piechart Biomarkers
6 from __future__ import division
7
8 # Libraries.
9 import sys
10 import numpy as np
11 import pandas as pd
12 import seaborn as sns
13 import matplotlib as mpl
14 import matplotlib.cm as cm
15 import matplotlib.pyplot as plt
16
17 # Matplotlib font size configuration.
18 mpl.rcParams['font.size'] = 9.0
19
20 # ------------------------------------------------------------------------
21 # HELPER METHODS
22 # ------------------------------------------------------------------------
23 def transparency(cmap, alpha):
24 """
25 """
26 for i,rgb in enumerate(cmap):
27 cmap[i] = rgb + (alpha,)
28 return cmap
29
30 # ------------------------------------------------------------------------
31 # CONFIGURATION
32 # ------------------------------------------------------------------------
33 # Common configuration.
34 title_font_size = 30
35 labels = ['CRP','ALT','BIL','ALP','CRE','WBC'] # labels
36 labels_empty = ['', '', '', '', '', ''] # labels empty
37 colors = transparency(sns.color_palette("Set2", desat=0.75, n_colors=7), 0.5)
38 explode = (0,0,0,0,0,0) # proportion with which to offset each wedge
39 autopct = '%1.0f%%' # print values inside the wedges
40 pctdistance = 0.4 # ratio betwen center and text (default=0.6)
41 labeldistance = 0.7 # radial distance wich pie labels are drawn
42 shadow = False # shadow
43 startangle = 90 # rotate piechart (default=0)
44 radius = 1 # size of piechart (default=1)
45 counterclock = False # fractions direction.
46 center = (0,0) # center position of the chart.
47 frame = False # plot axes frame with the pie chart.
48
49 # map with arguments for the text objects.
50 textprops = {'fontsize':'x-large'}
51
52 # map with arguments for the wedge objects.
53 wedgeprops = {}
54
55 # Color manually selected
56 colors_manual = ['mediumpurple',
57 'violet',
58 'mediumaquamarine',
59 'lightskyblue',
60 'lightsalmon',
61 'indianred']
62
63
64
65
66 # -------------------------------------------------------------------------
67 # FIGURE 1
68 # -------------------------------------------------------------------------
69 # Number of pathology laboratory data for each of the selected biochemical
70 # markers (ALP, ALT, BIL, CRE, CRP, WBC) for the non-infection category
71 # during the years 2014 and 2015.
72
73 # Portions.
74 sizes = [11, 15, 16, 17, 19, 21] # Add proportions
75
76 # reset feature
77 startangle = 130
78
79 # Plot.
80 plt.figure()
81 plt.pie(sizes,
82 explode=explode,
83 labels=labels, # Use: labels / labels_empty
84 colors=colors, # Use: colors / colors_manual
85 autopct=autopct,
86 pctdistance=pctdistance,
87 labeldistance=labeldistance,
88 shadow=shadow,
89 startangle=startangle,
90 radius=radius,
91 counterclock=counterclock,
92 center=center,
93 frame=frame,
94 textprops=textprops,
95 wedgeprops={'linewidth':0.35,
96 'edgecolor':'k'})
97 # Format figure.
98 plt.axis('equal')
99 plt.tight_layout()
100 plt.title("", fontsize=title_font_size) # Add title.
101 #plt.legend(labels=labels, fontsize='xx-large') # Add legend.
102
103 # -------------------------------------------------------------------------
104 # FIGURE 2
105 # -------------------------------------------------------------------------
106 # Number of pathology laboratory data for each of the selected biochemical
107 # markers (ALP, ALT, BIL, CRE, CRP, WBC) for the infection category
108 # during the years 2014 and 2015. ['CRP','ALP','BIL','ALT','CRE','WBC']
109
110 # Portions
111 sizes = [18, 13, 14, 15, 20, 20] # Add proportions
112
113 # reset feature
114 startangle = 155
115
116 # Plot.
117 plt.figure()
118 plt.pie(sizes,
119 explode=explode,
120 labels=labels, # Use: labels / labels_empty
121 colors=colors, # Use: colors / colors_manual
122 autopct=autopct,
123 pctdistance=pctdistance,
124 labeldistance=labeldistance,
125 shadow=shadow,
126 startangle=startangle,
127 radius=radius,
128 counterclock=counterclock,
129 center=center,
130 frame=frame,
131 textprops=textprops,
132 wedgeprops={'linewidth':0.35,
133 'edgecolor':'k'})
134
135 # Format figure.
136 plt.axis('equal')
137 plt.tight_layout()
138 plt.title("", fontsize=title_font_size) # Add title
139 #plt.legend(labels=labels) # Add legend.
140
141 # Show figures
142 plt.show()
Total running time of the script: ( 0 minutes 0.134 seconds)