我已經看到了一些與此類似的問題,但無法使其作業,所以我想我會在這里問。
我有一個帶有 2 個疊加散點圖的 seaborn 熱圖。當它繪制顏色條時,標簽不會以顏色為中心。我想知道如何將值集中在顏色上。是否可以始終將 0 顏色設定為白色?我的代碼和情節如下
fig, ax = plt.subplots(figsize=(20, 20))
plt.gca().set_aspect(RI/SI)
cbar_kws=dict(pad=0.01,shrink=0.86, drawedges=True, ticks=range(int(maxfold) 1)) #Colorbar size and location
cmap = plt.get_cmap('jet', maxfold 1)
sns.heatmap(binspivot, cmap=cmap,linewidths=0.01, linecolor = 'black', vmin=0, vmax=maxfold, xticklabels=False, yticklabels=False,annot=True, annot_kws={"size": 20,"color":"black"},cbar_kws=cbar_kws)
ax.set(xlabel=None, ylabel=None)
ax.scatter(Sourcedfcut['Xmod'],Sourcedfcut['Ymod'], color="red", s = 400)
ax.scatter(Recdfcut['Xmod'],Recdfcut['Ymod'], color="blue", s= 400)

uj5u.com熱心網友回復:
使顏色條的刻度線位于矩形中心的最簡單方法是移動vmin一半vmax。要向現有顏色圖添加額外顏色,可以ListedColormap使用從原始顏色圖中提取的新顏色和顏色創建新顏色。要使顏色欄中的白色在白色背景下可見,可以添加一個黑色的環繞矩形。
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import seaborn as sns
import numpy as np
binspivot = (12.02 * np.random.rand(20, 20) ** .5).astype(int)
maxfold = binspivot.max()
fig, ax = plt.subplots(figsize=(20, 20))
cbar_kws = dict(pad=0.01, shrink=0.86, drawedges=True, ticks=range(int(maxfold) 1)) # Colorbar size and location
cmap1 = plt.get_cmap('turbo', maxfold 1)
colors = ['white'] list(plt.get_cmap('turbo', maxfold).colors)
cmap = ListedColormap(colors=colors)
sns.heatmap(binspivot, cmap=cmap, linewidths=0.01, linecolor='black', vmin=-0.5, vmax=maxfold 0.5,
xticklabels=False, yticklabels=False,
annot=True, annot_kws={"size": 20}, cbar_kws=cbar_kws, ax=ax)
ax.set(xlabel=None, ylabel=None)
cbar_ax = fig.axes[-1]
cbar_ax.tick_params(labelsize=20)
cbar_ax.add_patch(plt.Rectangle((0, 0), 1, 1, fc='none', ec='black', clip_on=False, transform=cbar_ax.transAxes))
plt.tight_layout()
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/428942.html
標籤:Python matplotlib 海运 彩条
