Skip to content

Commit d4e6d5b

Browse files
authored
Resolve multiple bugs
Remove space=0 warnings; redesign the logic for same_scale; resolve the bug for the issue Univariate bars are "doubling" on the left #12
1 parent 76d31e0 commit d4e6d5b

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

hammock_plot/hammock_plot.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ def __init__(self,
121121
self.color_coloumn_placeholder = "value_color_col_placeholder_VCCP"
122122
self.same_var_placeholder = "_same_var_placeholder_SVP_"
123123

124+
# to resolve same_scale and value_order alignment issue
125+
self.same_scale_placeholder = "_same_scale_placeholder_SSP_"
126+
124127
def plot(self,
125128
var: List[str] = None,
126129
value_order: Dict[str, Dict[int, str]] = None,
@@ -136,7 +139,7 @@ def plot(self,
136139
default_color="lightskyblue",
137140
# Manipulating Spacing and Layout
138141
bar_width: float = 1.,
139-
min_bar_width: float = .05,
142+
min_bar_width: float = .1,
140143
space: float = .5,
141144
label_options: Dict = None,
142145
height: float = 10.,
@@ -249,11 +252,6 @@ def plot(self,
249252
self.shape = shape
250253
self.same_scale = same_scale
251254

252-
if self.label and self.space == 0:
253-
raise ValueError(
254-
f'space must not be 0 if label = True.'
255-
)
256-
257255
if self.missing:
258256
self.data_df = self.data_df.fillna(self.missing_data_placeholder)
259257
else:
@@ -284,7 +282,7 @@ def plot(self,
284282
data_point_numbers = []
285283
for p in var_pairs:
286284
varname_p = [self._get_varname(var) for var in p]
287-
pair_dict_temp = self.data_df.groupby(varname_p).size().to_dict()
285+
pair_dict_temp = self.data_df.groupby(varname_p, observed=True).size().to_dict()
288286
pair_dict = {}
289287
for k, v in pair_dict_temp.items():
290288
if v == 0:
@@ -298,7 +296,7 @@ def plot(self,
298296
for p in var_pairs:
299297
varname_p = [self._get_varname(var) for var in p]
300298
varname_p.append(self.color_coloumn_placeholder)
301-
color_dict_temp = self.data_df.groupby(varname_p).size().to_dict()
299+
color_dict_temp = self.data_df.groupby(varname_p, observed=True).size().to_dict()
302300
color_dict = {}
303301
for k, v in color_dict_temp.items():
304302
if v == 0:
@@ -428,23 +426,22 @@ def plot(self,
428426
if k[1] == the_hi_value and self.hi_var == col_name:
429427
label_rectangle_width_temp = label_rectangle_widths[idx]
430428
label_rectangle_total_obvs_color[k] = 0
431-
# case2: for other colors
432-
elif the_hi_value:
433-
num_obv = self.data_df.groupby([self.hi_var, col_name]).size().get((the_hi_value, k[1]), 0)
429+
# case2: for other variables
430+
elif the_hi_value != None:
431+
num_obv = self.data_df.groupby([self.hi_var, col_name], observed=True).size().get((the_hi_value, k[1]), 0)
434432
label_rectangle_width_temp = bar * num_obv
435-
if self.min_bar_width and label_rectangle_width_temp <= self.min_bar_width and label_rectangle_width_temp != 0:
436-
label_rectangle_width_temp = self.min_bar_width
433+
# if self.min_bar_width and label_rectangle_width_temp <= self.min_bar_width and label_rectangle_width_temp != 0:
434+
# label_rectangle_width_temp = self.min_bar_width
437435
label_rectangle_total_obvs_color[k] -= num_obv
438436
# case3: for the last default color
439437
else:
440438
label_rectangle_width_temp = bar * label_rectangle_total_obvs_color[k]
441-
if self.min_bar_width and label_rectangle_width_temp <= self.min_bar_width and label_rectangle_width_temp != 0:
442-
label_rectangle_width_temp = self.min_bar_width
439+
# if self.min_bar_width and label_rectangle_width_temp <= self.min_bar_width and label_rectangle_width_temp != 0:
440+
# label_rectangle_width_temp = self.min_bar_width
443441
label_rectangle_widths_color.append(label_rectangle_width_temp)
444442
label_rectangle_ratio_color_centers.append((label_rectangle_width_color_total[idx] + label_rectangle_width_temp / 2) / label_rectangle_widths[idx])
445443
label_rectangle_width_color_total[idx] += label_rectangle_width_temp
446444
idx+=1
447-
448445
if not label_rectangle_vertical:
449446
xs, ys = label_rectangle_painter.get_coordinates(label_rectangle_left_center_pts, label_rectangle_right_center_pts, label_rectangle_widths)
450447
color_left_center_pts, color_right_center_pts = label_rectangle_painter.get_center_highlight(xs, ys,
@@ -535,6 +532,7 @@ def _gen_coordinate(self, start, n, edge, spacing, total_range, val_type="str"):
535532
coor_lst.append(total_range + (start - edge) - edge)
536533
return coor_lst
537534

535+
# using the variable with the largest number of values as the criteria for same_scale
538536
def _get_same_scale_minmax(self, original_unique_value):
539537
min, max = 0, 0
540538
for i, varname in enumerate(self.same_scale):
@@ -583,6 +581,8 @@ def _list_labels(self, ax, figsize_y, figsize_x, label):
583581
for v in sorted_unique_valnames_temp:
584582
if v in unique_valnames:
585583
sorted_unique_valnames.append(v)
584+
else:
585+
sorted_unique_valnames.append(self.same_scale_placeholder)
586586
if self.missing_data_placeholder in unique_valnames:
587587
unique_valnames.remove(self.missing_data_placeholder)
588588
sorted_unique_valnames = sorted(
@@ -649,6 +649,8 @@ def _list_labels(self, ax, figsize_y, figsize_x, label):
649649

650650
# plot labels
651651
for i, (val, y) in enumerate(zip(uni_val, uni_val_coordinates)):
652+
if val[1] == self.same_scale_placeholder:
653+
continue
652654
if label:
653655
if self.missing and val[1] == self.missing_data_placeholder:
654656
ax.text(x, y, "missing", ha='center', va='center')
@@ -684,6 +686,8 @@ def _parse_colors(self, color_list):
684686

685687
return parsed_colors
686688

689+
import math
690+
687691
def _compute_left_right_centers(self, midpoints_top, midpoints_bottom, lengths):
688692
"""
689693
Given the top and bottom midpoints of one or more aligned rectangles, along with their width,

0 commit comments

Comments
 (0)