python - 将颜色条移近热图(Seaborn)

标签 python matplotlib seaborn data-visualization heatmap

我的颜色条离热​​图底部很远。有没有办法把它移近一点?

我的代码是:


import seaborn as sns

Granger2 = Granger
Granger2.columns = Granger_colnames
Granger2.index = Granger_rownames

fig, ax = plt.subplots(figsize=(6,25)) 
sns.heatmap(Granger2, cmap=rvb, cbar=True, ax=ax,linewidths=.5,cbar_kws={"orientation": "horizontal"})
ax.xaxis.tick_top() # x axis on top
ax.xaxis.set_label_position('top')

#Remove ticks
ax.tick_params(axis='both', which='both', length=0)

# Drawing the frame
ax.axhline(y = 0, color='k',linewidth = 1)
ax.axhline(y = Granger2.shape[0], color = 'k',linewidth = 1)  
ax.axvline(x = 0, color = 'k', linewidth = 1)
ax.axvline(x = Granger2.shape[1], color = 'k', linewidth = 1)

plt.show()

enter image description here

最佳答案

您可以使用例如cbar_kws={"orientation": "horizo​​ntal", "pad":0.02}.填充是子图高度的一小部分,因此 0.02 是 2%。查看colorbar docs有关 pad 和其他参数的更多信息。

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

sns.set_style('whitegrid')
flights = sns.load_dataset('flights')
flights = flights.pivot('year', 'month').droplevel(0, axis=1)

fig, ax = plt.subplots(figsize=(6, 20))
sns.heatmap(flights, cmap='Greens', cbar=True, ax=ax, linewidths=.5,
            cbar_kws={"orientation": "horizontal", "pad": 0.02})
ax.xaxis.tick_top()  # x axis on top
ax.xaxis.set_label_position('top')

# Remove ticks
ax.tick_params(axis='both', which='both', length=0)

# Drawing the frame
ax.patch.set_edgecolor('0.15')
ax.patch.set_linewidth(2)

plt.tight_layout()
plt.show()

changing the colorbar padding for sns.heatmap

关于python - 将颜色条移近热图(Seaborn),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69409099/

相关文章:

python - Pandas : Sum multiple columns and get results in multiple columns

python - matplotlib:轴的比例不正确

python - ValueError : Axes instance argument was not found in a figure, 同名问题没有答案

python - QT中创建 float 框UI的方法

python - 使用 train_test_split 后分类器准确率达到 100%

python - Python 中的 HTML 标签云

python - 在 matplotlib 中使用\mathbb{}

python - Matplotlib 中的 X 轴打印随机数而不是年份

python - 使用 Seaborn FacetGrid 从数据框中绘制错误条

python - Seaborn distplot 不支持范围吗?