Python 可视化主要使用 Matplotlib、Seaborn、Plotly 提供的颜色库和调色板。
Matplotlib 基本颜色
1
2
3
4
5
6
7
| import matplotlib.colors as mcolors
# 基本颜色名称
mcolors.BASE_COLORS.keys() # 'r', 'g', 'b', 'c', 'm', 'y', 'k', 'w'
# CSS4 颜色(140+ 种)
mcolors.CSS4_COLORS['red'] # '#FF0000'
|
Seaborn 调色板
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| import seaborn as sns
# 分类调色板
sns.color_palette("deep") # 深色
sns.color_palette("pastel") # 柔和
sns.color_palette("Set2", 8) # Set 系列
sns.color_palette("tab10", 8) # Tableau
# 连续调色板
sns.color_palette("Blues", 10)
sns.color_palette("viridis", 10)
# 发散调色板
sns.color_palette("RdBu_r", 10)
# 可视化
sns.palplot(sns.color_palette("husl", 8))
|
Plotly 颜色
1
2
3
4
5
6
7
8
9
| import plotly.express as px
# 定性颜色
px.colors.qualitative.Set1
px.colors.qualitative.Plotly
# 顺序颜色
px.colors.sequential.Viridis
px.colors.sequential.Plasma
|
推荐选择策略
1
2
3
4
5
6
7
8
9
10
11
| def get_colors(n, palette_type='qualitative'):
if palette_type == 'qualitative':
if n <= 10:
return sns.color_palette("tab10", n)
elif n <= 20:
return sns.color_palette("tab20", n)
return sns.color_palette("husl", n)
elif palette_type == 'sequential':
return sns.color_palette("viridis", n)
elif palette_type == 'diverging':
return sns.color_palette("RdBu_r", n)
|
调色板类型选择
| 类型 | 用途 | 示例 |
|---|
| 定性 | 离散分类 | tab10, Set2, husl |
| 顺序 | 连续数值 | Blues, viridis, plasma |
| 发散 | 正负对比 | RdBu_r, coolwarm |
推荐:科研和数据分析使用 Seaborn 的 husl、Set1-3、tab10/20,视觉区分度好。
Comments