配色方案

参考:
https://zhuanlan.zhihu.com/p/593320758

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
import matplotlib.pyplot as plt
import matplotlib.patches as patches

colors = {
'plan00': [
{'name': 'red', 'rgb': [219, 49, 36]},
{'name': 'orange', 'rgb': [252, 140, 90]},
{'name': 'yellow', 'rgb': [255, 223, 146]},
{'name': 'bg-blue', 'rgb': [230, 241, 243]},
{'name': 'light-blue', 'rgb': [144, 190, 224]},
{'name': 'deep-blue', 'rgb': [75, 116, 178]},
],
'plan01': [
{'name': 'dark-green', 'rgb': [38, 70, 83]},
{'name': 'deep-green', 'rgb': [40, 114, 113]},
{'name': 'bi-green', 'rgb': [42, 157, 140]},
{'name': 'light-green', 'rgb': [138, 176, 125]},
{'name': 'bg-green', 'rgb': [236, 240, 219]},
],
'plan02': [
{'name': 'dark-red', 'rgb': [120, 0, 1]},
{'name': 'dark-blue', 'rgb': [0, 47, 73]},
{'name': 'bg-yellow', 'rgb': [254, 240, 213]},
],
'plan03': [
{'name': 'light-flesh', 'rgb': [253, 232, 213]},
{'name': 'flesh', 'rgb':[221, 190, 169]},
{'name': 'deep-flesh', 'rgb': [203, 153, 126]},
{'name': 'light-dark-green', 'rgb': [184, 183, 163]},
{'name': 'dark-green', 'rgb': [165, 165, 141]},
{'name': 'deep-dark-green', 'rgb': [107, 112, 92]},
],
'plan04': [
{'name': 'blue-green', 'rgb': [68, 117, 122]},
{'name': 'green-gray', 'rgb': [183, 181, 160]},
{'name': 'orange-red', 'rgb': [212, 76, 60]},
{'name': 'deep-purple', 'rgb': [69, 42, 61]},
{'name': 'skin', 'rgb': [238, 213, 183]},
{'name': 'light-yellow', 'rgb': [252, 231, 186]},
{'name': 'brown', 'rgb': [203, 153, 126]},
]
}

def my_colors(palettes):
"""
Args:
palettes (dict): 包含颜色方案的字典。
"""
# ... 您的函数代码保持不变 ...
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei', 'Arial']
plt.rcParams['axes.unicode_minus'] = False

max_cols = 7
num_plans = len(palettes)
fig, axes = plt.subplots(
nrows=num_plans,
ncols=max_cols,
figsize=(max_cols * 2, num_plans * 2),
squeeze=False
)

fig.suptitle('my_colors', fontsize=20, y=0.98)

for i, (plan_name, colors_list) in enumerate(palettes.items()): # 变量名改为 colors_list 避免与模块名混淆
# 在每行左侧显示方案名称
axes[i, 0].text(-0.3, 0.5, plan_name, transform=axes[i, 0].transAxes,
fontsize=14, fontweight='bold', ha='right', va='center')

for j, color_info in enumerate(colors_list):
if j >= max_cols:
print(f"警告:'{plan_name}' 中的颜色数量超过 {max_cols},多余的颜色将不会显示。")
break

ax = axes[i, j]

rgb_255 = color_info['rgb']
name = color_info['name']

rgb_norm = [c / 255.0 for c in rgb_255]

ax.set_facecolor(rgb_norm)

# 根据背景色的亮度决定使用白色还是黑色文字
luminance = (0.299 * rgb_norm[0] + 0.587 * rgb_norm[1] + 0.114 * rgb_norm[2])
text_color = 'white' if luminance < 0.5 else 'black'

# 在色块下方显示颜色名称和RGB值
ax.text(0.5, 0.5, name, ha='center', va='bottom', fontsize=11, color=text_color)
ax.text(0.5, 0.35, f'({rgb_255[0]}, {rgb_255[1]}, {rgb_255[2]})',
ha='center', va='bottom', fontsize=9, color=text_color)

# 关闭坐标轴刻度和边框
ax.set_xticks([])
ax.set_yticks([])
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

# 隐藏未使用的子图格子
for i in range(num_plans):
num_colors_in_plan = len(palettes[list(palettes.keys())[i]])
for j in range(num_colors_in_plan, max_cols):
axes[i, j].axis('off')

# 调整布局以防止重叠
plt.tight_layout(rect=[0, 0.03, 1, 0.98])

# 显示色卡
plt.show()


# --- 主程序入口 ---
if __name__ == '__main__':
print("正在直接运行 my_colors.py 以展示颜色库...")
my_colors(colors)

色卡

图片展示


颜色使用

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
import matplotlib.pyplot as plt
from colors import colors

# --- 绘图准备 ---
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei', 'Arial']
plt.rcParams['axes.unicode_minus'] = False

palette_name = 'plan00'
selected_palette = colors[palette_name]

plot_colors = [tuple(c / 255 for c in color_info['rgb']) for color_info in selected_palette]

categories = ['产品A', '产品B', '产品C', '产品D', '产品E', '产品F']
values = [250, 410, 320, 180, 500, 280]

# --- 开始绘图 ---
fig, ax = plt.subplots(figsize=(10, 6))
bars = ax.bar(categories, values, color=plot_colors)

# ... 美化代码 ...
ax.bar_label(bars, fmt='%d', padding=3, fontsize=11, color='gray')
ax.set_title(f'产品季度销售额 (使用 "{palette_name}" 配色方案)', fontsize=16, pad=20)
ax.set_xlabel('产品类别', fontsize=12, labelpad=10)
ax.set_ylabel('销售额 (万元)', fontsize=12, labelpad=10)
ax.set_ylim(0, max(values) * 1.15)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('gray')
ax.spines['bottom'].set_color('gray')
ax.tick_params(axis='x', colors='gray')
ax.tick_params(axis='y', colors='gray')
ax.yaxis.grid(True, linestyle='--', which='major', color='lightgray', alpha=0.7)
ax.set_axisbelow(True)
plt.tight_layout()
plt.show()