Matplotlib:颜色栏中的中心颜色,具有使用索引颜色值的发散颜色图

Tho*_*ger 4 python matplotlib

这部分是两个问题:

  • 如何将(发散的)颜色图围绕某个给定值居中?
  • 如何做到这一点,同时将数据中的索引映射到颜色图中的值?(下面进一步解释)

某些类型的数据(例如 BMI 分数)具有自然的中点。在 matplotlib 中,有几个不同的颜色图。我希望颜色图的中心,即光谱的“中间”位于“理想”BMI 分数上,与绘制的 BMI 分数分布无关。

BMI 类别阈值是:bmi_threshold = [16, 17, 18.5, 25, 30, 35].

在下面的代码中,我绘制了 300 个随机 BMI 值的散点图,其中 x 轴为体重,y 轴为身高,如下图所示。

在第一张图片中,我使用了- 调用的 - 参数np.digitize(bmi, bmi_threshold),但随后颜色条中的每个值也变成了,而我希望颜色条刻度位于 BMI 分数中(大约 15-40)。(是对应于和的 300 个随机 BMI 分数的数组)cax.scatter()range(7)bmixy

BMI 阈值分布不均匀,因此如果我仅更改颜色条中的刻度标签,则将无法正确表示与数字化类别索引(例如2和之间)的距离。3

在与如下所示的代码一起使用的第二张图像中,似乎没有正确居中于“理想”BMI 分数 22。我尝试使用“使散点颜色条仅显示vmin/vmax ”来调整颜色栏中的颜色范围,但它似乎没有按(我)预期的方式工作。

low此外,我认为我可以通过设置和high到[0, 1] 之外的值(例如 [-0.5,1.5] )来“挤压”颜色来强调“中心”又名“理想”分数cmap(np.linspace(low, high, 7)),但随后我遇到了更多麻烦使颜色条居中。

我做错了什么,我该如何实现这一目标?

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import matplotlib as mpl

np.random.seed(4242)

# Define BMI class thresholds
bmi_thresholds = np.array([16, 17, 18.5, 25, 30, 35])

# Range to sample BMIs from
max_bmi = max(bmi_thresholds)*0.9
min_bmi = min(bmi_thresholds)*0.3

# Convert meters into centimeters along x-axis
@mpl.ticker.FuncFormatter
def m_to_cm(m, pos):
    return f'{int(m*100)}'

# Number of samples
n = 300

# Heights in range 0.50 to 2.20 meters
x = np.linspace(0.5, 2.2, n) 
# Random BMI values in range [min_bmi, max_bmi]
bmi = np.random.rand(n)*(max_bmi-min_bmi) + min_bmi  
# Compute corresponding weights
y = bmi * x**2      

# Prepare plot with labels, etc.
fig, ax = plt.subplots(figsize=(10,6))
ax.set_title(f'Random BMI values. $n={n}$')
ax.set_ylabel('Weight in kg')
ax.set_xlabel('Height in cm')
ax.xaxis.set_major_formatter(m_to_cm)
ax.set_ylim(min(y)*0.95, max(y)*1.05)
ax.set_xlim(min(x), max(x))

# plot bmi class regions (i.e. the "background")
for i in range(len(bmi_thresholds)+1):
    area_min = bmi_thresholds[i-1] if i > 0 else 0
    area_max = bmi_thresholds[i] if i < len(bmi_thresholds) else 10000#np.inf
    area_color = 'g' if i == 3 else 'y' if i in [2,4] else 'orange' if i in [1,5] else 'r'
    ax.fill_between(x, area_min * x**2, area_max * x**2, color=area_color, alpha=0.2, interpolate=True)

# Plot lines to emphasize regions, and additional bmi score lines (i.e. 10 and 40)    
common_plot_kwargs = dict(alpha=0.8, linewidth=0.5)
for t in (t for t in np.concatenate((bmi_thresholds, [10, 40]))):
    style = 'g-' if t in [18.5, 25] else 'r-' if t in [10,40] else 'k-' 
    ax.plot(x, t * x**2, style, **common_plot_kwargs)

# Compute offset from target_center to median of data range 
target_center = 22
mid_bmi = np.median(bmi)
s = max(bmi) - min(bmi)
d = target_center - mid_bmi
# Use offset to normalize offset as to the range [0, 1]
high = 1 if d < 0 else (s-d)/s
low = 0 if d >= 0 else -d/s


# Use normalized offset to create custom cmap to centered around ideal BMI?
cmap = plt.get_cmap('PuOr')
colors = cmap(np.linspace(low, high, 7))
cmap = mpl.colors.LinearSegmentedColormap.from_list('my cmap', colors)

# plot random BMIs
c = np.digitize(bmi, bmi_thresholds)
sax = ax.scatter(x, y, s=15, marker='.', c=bmi, cmap=cmap)

cbar = fig.colorbar(sax, ticks=np.concatenate((bmi_thresholds, [22, 10, 40])))
plt.tight_layout()
Run Code Online (Sandbox Code Playgroud)

eme*_*mem 5

您可以使用matplotlib执行相同操作的内置函数:

matplotlib.colors.TwoSlopeNorm
Run Code Online (Sandbox Code Playgroud)

请参阅:https ://matplotlib.org/3.2.2/gallery/userdemo/colormap_normalizations_diverging.html

  • 这正是我当时所寻找的。谢谢,我很感谢你的意见,尽管这个问题已经很老了。 (2认同)