python - 将颜色条移近热图(Seaborn)
问题描述
我的颜色条离热图的底部很远。有没有办法让它靠近一点?
我的代码是:
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()
解决方案
您可以使用例如cbar_kws={"orientation": "horizontal", "pad":0.02}
. 内边距是子图高度的一小部分,因此 0.02 是 2%。有关其他参数的更多信息,请参阅颜色条文档。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()
推荐阅读
- sql - 为什么在表中更新后行会发生变化?PostreSQL
- r - 具有约束的邻接矩阵聚类:节点权重总和的最大值和最小值
- python - 如何在 Python 中向上/向下舍入一个 int?
- r - 在反应输出中使用 setSliderColor 更改滑块颜色
- python - 如何在 Django 中创建扩展的用户对象?
- python - 是什么导致测量的 fps 与预期但可能的 fps 不同?
- reactjs - Divi order class props works wrong when I dublicate it
- apache-spark - 在explode“子查询”中有效地插入when语句
- asp.net - 在现有的 oauth2 实施中包含 Azure AD
- java - Spring Boot 2.5 升级 - 在 Spock 中执行的 SpringBootTest 中的对象似乎被覆盖