如何使用 plt.hist2d 函数将颜色映射到热图中的某些值?

How to map colors to certain values in a heatmap with plt.hist2d function?

我正在使用 matplotlib.pyplot.hist2d 作为 seaborn.FacetGrid 对象的 map 函数的参数绘制热图的分面网格,问题是不同图中值的颜色表示不一致. 如何将调色板值映射到所有绘图的常量值?

def hist2dgrid(x, y, **kwargs):
    palette = kwargs.pop('color')
    bins_x = np.arange(0,24+1.5,1.5)
    bins_y = np.arange(0,36+3,2)
    plt.hist2d(x, y, bins = [bins_x, bins_y], cmap = palette, cmin = 0.5)
    plt.colorbar(label='count')

g= sns.FacetGrid(data=df_clean,col='start_time_day_name',col_wrap = 3)
g.map(hist2dgrid, 'start_time_hour_decimal', 'avg_trip_speed_kmh',color='viridis_r');

输出为:

您应该传递给 matplotlib.pyplot.hist2d vminvmax 参数:

随机数据集的结果,没有上述参数:

使用上述参数在同一数据集上的结果:

plt.hist2d(x, y, bins = [bins_x, bins_y], cmap = palette, cmin = 0.5, vmin = 30, vmax = 40)