Skip to content

Commit 80f7ded

Browse files
committed
feat(bar): support color transparency adjustment via color_alpha argument
1 parent 209b580 commit 80f7ded

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

src/plotfig/bar.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import numpy.typing as npt
77
from matplotlib.axes import Axes
88
from matplotlib.ticker import FuncFormatter, ScalarFormatter
9-
from matplotlib.colors import LinearSegmentedColormap
9+
from matplotlib.colors import LinearSegmentedColormap, to_rgba
1010
from matplotlib.patches import Polygon
1111
from scipy import stats
1212

@@ -225,6 +225,7 @@ def plot_one_group_bar_figure(
225225
labels_name: list[str] | None = None,
226226
width: Num = 0.5,
227227
colors: list[str] | None = None,
228+
color_alpha: Num = 1,
228229
edgecolor: str | None = None,
229230
gradient_color: bool = False,
230231
colors_start=None,
@@ -254,6 +255,7 @@ def plot_one_group_bar_figure(
254255
labels_name (list[str] | None, optional): 每个数据集对应的标签。默认为 None,使用索引作为标签。
255256
width (Num, optional): 柱子的宽度。默认为 0.5。
256257
colors (list[str] | None, optional): 每个柱子的颜色列表。若为 None,使用默认灰色。
258+
color_alpha (Num, optional): 颜色透明度,取值范围为 0(完全透明)到 1(完全不透明)。使用 gradient_color 时该参数无效。默认为 1。
257259
edgecolor (str | None, optional): 柱子的边缘颜色。默认为 None,即不特别设置。
258260
gradient_color (bool, optional): 是否为柱子启用渐变色填充。默认为 False。
259261
colors_start (list[str] | None, optional): 渐变色的起始颜色列表。用于 gradient_color=True。
@@ -317,7 +319,7 @@ def plot_one_group_bar_figure(
317319
ax.imshow(gradient, aspect="auto", cmap=cmap, extent=extent, zorder=0)
318320
else:
319321
ax.bar(
320-
x_positions, means, width=width, color=colors, alpha=1, edgecolor=edgecolor
322+
x_positions, means, width=width, color=colors, alpha=color_alpha, edgecolor=edgecolor
321323
)
322324

323325
ax.errorbar(
@@ -385,6 +387,7 @@ def plot_one_group_violin_figure(
385387
ax: Axes | None = None,
386388
width: Num = 0.8,
387389
colors: list[str] | None = None,
390+
color_alpha: Num = 1,
388391
gradient_color: bool = False,
389392
colors_start: list[str] | None = None,
390393
colors_end: list[str] | None = None,
@@ -411,6 +414,7 @@ def plot_one_group_violin_figure(
411414
ax (Axes | None, optional): matplotlib 的 Axes 对象,用于绘图。默认为 None,使用当前 Axes。
412415
width (Num, optional): 小提琴图的总宽度。默认为 0.8。
413416
colors (list[str] | None, optional): 每个小提琴的颜色。若为 None,使用默认灰色。
417+
color_alpha (Num, optional): 颜色透明度,取值范围为 0(完全透明)到 1(完全不透明)。使用 gradient_color 时该参数无效。默认为 1。
414418
gradient_color (bool, optional): 是否启用渐变色填充。默认为 False。
415419
colors_start (list[str] | None, optional): 渐变起始颜色列表,对应每组数据。
416420
colors_end (list[str] | None, optional): 渐变结束颜色列表,对应每组数据。
@@ -438,7 +442,7 @@ def plot_one_group_violin_figure(
438442
labels_name = labels_name or [str(i) for i in range(len(data))]
439443
colors = colors or ["gray"] * len(data)
440444

441-
def draw_gradient_violin(ax, data, pos, width=width, c1="red", c2="blue"):
445+
def _draw_gradient_violin(ax, data, pos, width=width, c1="red", c2="blue", color_alpha=1):
442446
# KDE估计
443447
kde = stats.gaussian_kde(data)
444448
buffer = (max(data) - min(data)) / 5
@@ -459,10 +463,12 @@ def draw_gradient_violin(ax, data, pos, width=width, c1="red", c2="blue"):
459463
grad_height = 300
460464
gradient = np.linspace(0, 1, grad_width)
461465
if c1 == c2:
462-
cmap = LinearSegmentedColormap.from_list("cmap", [c1, c2])
466+
rgba = to_rgba(c1, alpha=color_alpha)
467+
cmap = LinearSegmentedColormap.from_list("cmap", [rgba, rgba])
468+
gradient_rgb = plt.get_cmap(cmap)(gradient)
463469
else:
464470
cmap = LinearSegmentedColormap.from_list("cmap", [c1, "white", c2])
465-
gradient_rgb = plt.get_cmap(cmap)(gradient)[..., :3]
471+
gradient_rgb = plt.get_cmap(cmap)(gradient)[..., :3]
466472
gradient_img = np.tile(gradient_rgb, (grad_height, 1, 1))
467473
# 显示图像并裁剪成violin形状
468474
im = ax.imshow(
@@ -508,7 +514,7 @@ def draw_gradient_violin(ax, data, pos, width=width, c1="red", c2="blue"):
508514
c2 = colors_end[i]
509515
else:
510516
c1 = c2 = colors[i]
511-
ymax, ymin = draw_gradient_violin(ax, d, pos=i, c1=c1, c2=c2)
517+
ymax, ymin = _draw_gradient_violin(ax, d, pos=i, c1=c1, c2=c2, color_alpha=color_alpha)
512518
ymax_lst.append(ymax)
513519
ymin_lst.append(ymin)
514520
ymax = max(ymax_lst)

0 commit comments

Comments
 (0)