-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplot_fdt_cat_default.py
More file actions
255 lines (214 loc) · 8.4 KB
/
plot_fdt_cat_default.py
File metadata and controls
255 lines (214 loc) · 8.4 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import matplotlib.pyplot as plt
import numpy as np
def plot_fdt_cat(x,
plot_type='fb',
v=False,
v_round=2,
v_pos=3,
xlab=None,
xlas=0,
ylab=None,
y2lab=None,
y2cfp=np.arange(0, 101, 25),
col='0.4',
xlim=None,
ylim=None,
main=None,
box=False):
"""
Plot a frequency distribution table (FDT) for categorical data.
Parameters:
x (DataFrame): Input DataFrame with columns including category labels,
frequencies, relative frequencies, and cumulative frequencies.
plot_type (str): Type of plot to generate. Options include:
'fb' - bar plot with frequencies,
'fp' - polygon plot with frequencies,
'fd' - dot chart with frequencies,
'pa' - Pareto plot with cumulative frequencies,
'rfb' - bar plot with relative frequencies,
'rfp' - polygon plot with relative frequencies,
'rfd' - dot chart with relative frequencies,
'rfpb' - bar plot with relative frequencies in %,
'rfpp' - polygon plot with relative frequencies in %,
'rfpd' - dot chart with relative frequencies in %,
'cfb' - bar plot with cumulative frequencies,
'cfp' - polygon plot with cumulative frequencies,
'cfd' - dot chart with cumulative frequencies,
'cfpb' - bar plot with cumulative frequencies in %,
'cfpp' - polygon plot with cumulative frequencies in %,
'cfpd' - dot chart with cumulative frequencies in %.
v (bool): If True, display values on the plot.
v_round (int): Decimal places for values displayed on the plot.
v_pos (int): Vertical position for value labels.
xlab (str): Label for the x-axis.
xlas (int): Rotation angle for x-axis labels. Defaults to 0.
ylab (str): Label for the y-axis.
y2lab (str): Label for the secondary y-axis (used in Pareto plot).
y2cfp (array): Percentage ticks for cumulative frequency y-axis in Pareto plot.
col (str): Color for plot elements. Default is '0.4' (gray).
xlim (tuple): Limits for the x-axis.
ylim (tuple): Limits for the y-axis.
main (str): Title for the plot.
box (bool): If True, display a box around the plot.
"""
# Helper function for bar plot
def plot_b(y, categories):
fig, ax = plt.subplots()
bar_positions = np.arange(len(categories))
ax.bar(bar_positions, y, color=col, edgecolor='black')
ax.set_xticks(bar_positions)
ax.set_xticklabels(categories, rotation=xlas * 90)
if xlab:
ax.set_xlabel(xlab)
if ylab:
ax.set_ylabel(ylab)
if main:
ax.set_title(main)
if box:
ax.spines['top'].set_visible(True)
ax.spines['right'].set_visible(True)
if v:
for i, val in enumerate(y):
ax.text(i, val, f"{round(val, v_round)}", ha='center', va='bottom')
plt.show()
# Helper function for polygon plot
def plot_p(y, categories):
fig, ax = plt.subplots()
ax.plot(range(len(categories)), y, 'o-', color=col, markersize=5)
ax.set_xticks(range(len(categories)))
ax.set_xticklabels(categories, rotation=xlas * 90)
if xlab:
ax.set_xlabel(xlab)
if ylab:
ax.set_ylabel(ylab)
if main:
ax.set_title(main)
if v:
for i, val in enumerate(y):
ax.text(i, val, f"{round(val, v_round)}", ha='center', va='bottom')
plt.show()
# Helper function for dotchart
def plot_d(y, categories):
fig, ax = plt.subplots()
ax.plot(y, range(len(categories)), 'o', color=col)
ax.set_yticks(range(len(categories)))
ax.set_yticklabels(categories)
if xlab:
ax.set_xlabel(xlab)
if ylab:
ax.set_ylabel(ylab)
if main:
ax.set_title(main)
if v:
for i, val in enumerate(y):
ax.text(val, i, f"{round(val, v_round)}", ha='right')
plt.show()
# Helper function for pareto plot
def plot_pa(y, cf, cfp, categories):
fig, ax1 = plt.subplots()
bar_positions = np.arange(len(categories))
# Bar plot
ax1.bar(bar_positions, y, color=col, edgecolor='black')
ax1.set_xticks(bar_positions)
ax1.set_xticklabels(categories, rotation=xlas * 90)
if xlab:
ax1.set_xlabel(xlab)
if ylab:
ax1.set_ylabel(ylab)
if main:
ax1.set_title(main)
# Set y-axis limit based on cumulative frequency
ax1.set_ylim(0, max(cf) * 1.1)
# Cumulative frequency plot
ax2 = ax1.twinx()
ax2.plot(bar_positions, cf, color='blue', marker='o', linestyle='-', markersize=5)
ax2.set_ylabel(y2lab)
ax2.set_ylim(0, max(cf) * 1.1) # Ensure y-axis limit for cumulative frequency
plt.show()
# Call appropriate plot type based on `plot_type` argument
categories = x['Category']
if plot_type == 'fb':
y = x.iloc[:, 1]
xlab = xlab or 'Category'
ylab = ylab or 'Frequency'
ylim = ylim or (0, max(y) * 1.3)
plot_b(y, categories)
elif plot_type == 'fp':
y = x.iloc[:, 1]
xlab = xlab or 'Category'
ylab = ylab or 'Frequency'
ylim = ylim or (0, max(y) * 1.2)
plot_p(y, categories)
elif plot_type == 'fd':
y = x.iloc[:, 1]
xlab = xlab or 'Frequency'
plot_d(y, categories)
elif plot_type == 'pa':
y = x.iloc[:, 1]
cf = x.iloc[:, 4] # Cumulative frequency
cfp = x.iloc[:, 5] # Cumulative frequency percentage
xlab = xlab or 'Category'
ylab = ylab or 'Frequency'
y2lab = y2lab or 'Cumulative frequency, (%)'
ylim = ylim or (0, sum(y) * 1.1)
plot_pa(y, cf, cfp, categories)
elif plot_type == 'rfb':
y = x.iloc[:, 2]
xlab = xlab or 'Category'
ylab = ylab or 'Relative frequency'
plot_b(y, categories)
elif plot_type == 'rfp':
y = x.iloc[:, 2]
xlab = xlab or 'Category'
ylab = ylab or 'Relative frequency'
ylim = ylim or (0, max(y) * 1.2)
plot_p(y, categories)
elif plot_type == 'rfd':
y = x.iloc[:, 2]
xlab = xlab or 'Relative frequency'
plot_d(y, categories)
elif plot_type == 'rfpb':
y = x.iloc[:, 3]
xlab = xlab or 'Category'
ylab = ylab or 'Relative frequency (%)'
plot_b(y, categories)
elif plot_type == 'rfpp':
y = x.iloc[:, 3]
xlab = xlab or 'Category'
ylab = ylab or 'Relative frequency (%)'
ylim = ylim or (0, max(y) * 1.2)
plot_p(y, categories)
elif plot_type == 'rfpd':
y = x.iloc[:, 3]
xlab = xlab or 'Relative frequency (%)'
plot_d(y, categories)
elif plot_type == 'cfb':
y = x.iloc[:, 4]
xlab = xlab or 'Category'
ylab = ylab or 'Cumulative frequency'
plot_b(y, categories)
elif plot_type == 'cfp':
y = x.iloc[:, 4]
xlab = xlab or 'Category'
ylab = ylab or 'Cumulative frequency'
ylim = ylim or (0, max(y) * 1.2)
plot_p(y, categories)
elif plot_type == 'cfd':
y = x.iloc[:, 4]
xlab = xlab or 'Cumulative frequency'
plot_d(y, categories)
elif plot_type == 'cfpb':
y = x.iloc[:, 5]
xlab = xlab or 'Category'
ylab = ylab or 'Cumulative frequency (%)'
plot_b(y, categories)
elif plot_type == 'cfpp':
y = x.iloc[:, 5]
xlab = xlab or 'Category'
ylab = ylab or 'Cumulative frequency (%)'
ylim = ylim or (0, max(y) * 1.2)
plot_p(y, categories)
elif plot_type == 'cfpd':
y = x.iloc[:, 5]
xlab = xlab or 'Cumulative frequency (%)'
plot_d(y, categories)