Skip to content

Commit 15d36bf

Browse files
committed
feat: added color setting to boxplot
1 parent a32485b commit 15d36bf

File tree

2 files changed

+74
-8
lines changed

2 files changed

+74
-8
lines changed

hydrodiy/plot/boxplot.py

+44-7
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,8 @@ def draw(self, ax=None, logscale=False, xoffset=0.):
594594
# Draw whiskers
595595
item = self.whiskers
596596
if item.show_line:
597-
for cc in [[wqq1txt, bqq1txt], [wqq2txt, bqq2txt]]:
597+
wnames = [[wqq1txt, bqq1txt], [wqq2txt, bqq2txt]]
598+
for icc, cc in enumerate(wnames):
598599
# Get y data
599600
q1 = stats.loc[cc[0], colname]
600601
q2 = stats.loc[cc[1], colname]
@@ -606,8 +607,8 @@ def draw(self, ax=None, logscale=False, xoffset=0.):
606607
ax.plot(x, y, lw=item.linewidth,
607608
color=item.linecolor, \
608609
alpha=item.alpha)
609-
element["bottom-whiskers"] = ax.get_lines()[-2]
610-
element["top-whiskers"] = ax.get_lines()[-1]
610+
element[f"bottom-whiskers{icc+1}"] = ax.get_lines()[-2]
611+
element[f"top-whiskers{icc+1}"] = ax.get_lines()[-1]
611612
else:
612613
# Draw box
613614
bbox = FancyBboxPatch([i-ww/2+xoffset, q1], \
@@ -619,24 +620,24 @@ def draw(self, ax=None, logscale=False, xoffset=0.):
619620
alpha=item.alpha)
620621

621622
ax.add_patch(bbox)
622-
element["whiskers"] = ax.patches[-1]
623+
element[f"whiskers{icc+1}"] = ax.patches[-1]
623624

624625
# Cap width
625626
cw = capswidths[i]
626627

627628
# Draw caps
628629
item = self.caps
629630
if item.show_line and cw>0:
630-
for qq in [wqq1txt, wqq2txt]:
631+
for iqq, qq in enumerate([wqq1txt, wqq2txt]):
631632
q1 = stats.loc[qq, colname]
632633
x = [i-cw/5+xoffset, i+cw/5+xoffset]
633634
y = [q1]*2
634635
ax.plot(x, y, lw=item.linewidth,
635636
color=item.linecolor, \
636637
alpha=item.alpha)
637638

638-
element["bottom-cap"] = ax.get_lines()[-2]
639-
element["top-cap"] = ax.get_lines()[-1]
639+
element[f"bottom-cap{iqq+1}"] = ax.get_lines()[-2]
640+
element[f"top-cap{iqq+1}"] = ax.get_lines()[-1]
640641

641642
# Box (quartile) values
642643
item = self.box
@@ -793,3 +794,39 @@ def set_ylim(self, ylim, hide_offlimit_text=True):
793794

794795
# Set axis limits
795796
self.ax.set_ylim(ylim)
797+
798+
799+
def set_color(self, name_pattern, color, alpha=0.5):
800+
""" Set color for selected boxes identied by
801+
name_pattern.
802+
803+
Parameters
804+
-----------
805+
name_pattern : str
806+
Pattern selection for box selection.
807+
color : str
808+
Color to set
809+
alpha : float
810+
Transparency level.
811+
"""
812+
props = ["color", "linecolor", \
813+
"edgecolor", "markeredgecolor", \
814+
"facecolor"]
815+
816+
for ename, elems in self.elements.items():
817+
if not re.search(name_pattern, ename):
818+
continue
819+
820+
for on, obj in elems.items():
821+
for prop in props:
822+
# Set color to element.
823+
# Try all properties with try
824+
try:
825+
fun = getattr(obj, f"set_{prop}")
826+
fun(color)
827+
except:
828+
pass
829+
830+
if re.search("box", on):
831+
obj.set_alpha(alpha)
832+

hydrodiy/plot/tests/test_hyplot_boxplot.py

+30-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ def test_width_by_count():
136136

137137

138138
def test_coverage():
139+
plt.close("all")
139140
fig, axs = plt.subplots(ncols=2)
140141

141142
bx1 = Boxplot(data=DATA)
@@ -150,6 +151,7 @@ def test_coverage():
150151

151152

152153
def test_coverage_by():
154+
plt.close("all")
153155
fig, ax = plt.subplots()
154156
cat = DATA["cat"].copy()
155157
cat.loc[cat<3] = 0
@@ -160,6 +162,7 @@ def test_coverage_by():
160162

161163

162164
def test_item_change():
165+
plt.close("all")
163166
fig, ax = plt.subplots()
164167
bx = Boxplot(data=DATA)
165168
bx.median.textformat = "%0.4f"
@@ -169,6 +172,7 @@ def test_item_change():
169172

170173

171174
def test_center():
175+
plt.close("all")
172176
fig, ax = plt.subplots()
173177
bx = Boxplot(data=DATA)
174178
bx.median.va = "bottom"
@@ -197,6 +201,7 @@ def test_narrow():
197201
df = pd.DataFrame(np.random.normal(size=(nval, nvar)), \
198202
columns = ["data{0}".format(i) for i in range(nvar)])
199203

204+
plt.close("all")
200205
fig, ax = plt.subplots()
201206
bx = Boxplot(style="narrow", data=df)
202207
bx.draw(ax=ax)
@@ -209,6 +214,7 @@ def test_showtext():
209214
df = pd.DataFrame(np.random.normal(size=(nval, nvar)), \
210215
columns = ["data{0}".format(i) for i in range(nvar)])
211216

217+
plt.close("all")
212218
fig, axs = plt.subplots(ncols=2)
213219
bx = Boxplot(data=df)
214220
bx.draw(ax=axs[0])
@@ -225,6 +231,7 @@ def test_center_text():
225231
df = pd.DataFrame(np.random.normal(size=(nval, nvar)), \
226232
columns = ["data{0}".format(i) for i in range(nvar)])
227233

234+
plt.close("all")
228235
fig, axs = plt.subplots(ncols=2)
229236
bx = Boxplot(data=df)
230237
bx.draw(ax=axs[0])
@@ -241,13 +248,15 @@ def test_number_format():
241248
df = pd.DataFrame(np.random.normal(size=(nval, nvar)), \
242249
columns = ["data{0}".format(i) for i in range(nvar)])
243250

251+
plt.close("all")
244252
fig, ax = plt.subplots()
245253
bx = Boxplot(data=df, number_format="3.3e")
246254
bx.draw(ax=ax)
247255
fig.savefig(FIMG /"bx18_number_format.png")
248256

249257

250258
def test_change_elements():
259+
plt.close("all")
251260
fig, ax = plt.subplots()
252261
bx = Boxplot(data=DATA, show_text=True)
253262
bx.draw(ax=ax)
@@ -257,7 +266,7 @@ def test_change_elements():
257266
line.set_solid_capstyle("round")
258267
line.set_linewidth(8)
259268

260-
line = bx.elements["data1"]["top-cap"]
269+
line = bx.elements["data1"]["top-cap1"]
261270
line.set_color("green")
262271
line.set_solid_capstyle("round")
263272
line.set_linewidth(6)
@@ -270,6 +279,7 @@ def test_change_elements():
270279

271280

272281
def test_set_ylim():
282+
plt.close("all")
273283
fig, axs = plt.subplots(ncols=2)
274284
ax = axs[0]
275285
bx = Boxplot(data=DATA)
@@ -282,3 +292,22 @@ def test_set_ylim():
282292
bx.set_ylim((1, y1), False)
283293

284294
fig.savefig(FIMG /"bx20_set_ylim.png")
295+
296+
297+
def test_set_color():
298+
plt.close("all")
299+
fig, ax = plt.subplots()
300+
bx = Boxplot(data=DATA)
301+
bx.draw(ax=ax)
302+
bx.set_color(".*a2$", "tab:red")
303+
304+
fig.savefig(FIMG /"bx21_set_color.png")
305+
306+
plt.close("all")
307+
fig, ax = plt.subplots()
308+
bx = Boxplot(data=DATA, style="narrow")
309+
bx.draw(ax=ax)
310+
bx.set_color(".*a2$", "tab:red")
311+
312+
fig.savefig(FIMG /"bx22_set_color_narrow.png")
313+

0 commit comments

Comments
 (0)