Matplotlib:使用索引的颜色值在颜色栏中使用不同的色图使颜色居中

时间:2018-09-19 17:45:07

标签: python matplotlib

这部分是两个问题:

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

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

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

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

在第一张图片中,我使用np.digitize(bmi, bmi_threshold)作为c调用的ax.scatter()参数,但是随后颜色栏中的每个值也都变成了range(7),而我希望色标显示在BMI分数中(大约15-40)。 (bmi是对应xy的300个随机bmi分数的数组)

BMI阈值分布不均,因此距数字化类别索引的距离例如如果仅更改颜色栏中的刻度标签,将无法正确表示23之间的位置。

在下面的代码中使用的第二个图像中,似乎没有正确地将BMI理想值定在22的中心。我尝试使用从“ Make a scatter colorbar display only a subset of the vmin/vmax”到调整颜色栏中的颜色范围,但它似乎没有达到(I)预期的效果。

此外,我认为我可以通过将low中的highcmap(np.linspace(low, high, 7))设置为[0]以外的值来“压缩”颜色,从而强调“中心”或“理想”分数1],例如[-0.5,1.5],但我在将颜色条居中时遇到了更多麻烦。

我在做什么错,我该怎么办?

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()

2 个答案:

答案 0 :(得分:2)

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

matplotlib.colors.TwoSlopeNorm

见:https://matplotlib.org/3.2.2/gallery/userdemo/colormap_normalizations_diverging.html

答案 1 :(得分:1)

我在这里找到了一个不错的解决方案:

http://chris35wills.github.io/matplotlib_diverging_colorbar/

他们使用以下代码创建了一个标准化类:

class MidpointNormalize(colors.Normalize):
    def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
        self.midpoint = midpoint
        colors.Normalize.__init__(self, vmin, vmax, clip)

    def __call__(self, value, clip=None):
        # I'm ignoring masked values and all kinds of edge cases to make a
        # simple example...
        x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1]
        return np.ma.masked_array(np.interp(value, x, y), np.isnan(value))

通过执行以下操作来使用该类:

elev_max=3000; mid_val=0;

plt.imshow(ras, cmap=cmap, clim=(elev_min, elev_max), norm=MidpointNormalize(midpoint=mid_val,vmin=elev_min, vmax=elev_max))
plt.colorbar()
plt.show()