首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Colorbar移至更靠近热图的位置(Seaborn)

将Colorbar移至更靠近热图的位置(Seaborn)
EN

Stack Overflow用户
提问于 2021-10-01 16:21:09
回答 1查看 169关注 0票数 0

我的颜色栏离我的热图底部很远。有没有办法让它更近一点呢?

我的代码是:

代码语言:javascript
复制
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()

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-01 17:01:53

你可以使用例如cbar_kws={"orientation": "horizontal", "pad":0.02}。填充是子图高度的一部分,因此0.02是2%。有关pad和其他参数的详细信息,请参阅colorbar docs

代码语言:javascript
复制
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()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69409099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档