Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用热图使桌子居中_Python_Pandas_Matplotlib_Seaborn_Heatmap - Fatal编程技术网

Python 用热图使桌子居中

Python 用热图使桌子居中,python,pandas,matplotlib,seaborn,heatmap,Python,Pandas,Matplotlib,Seaborn,Heatmap,我正在尝试在seaborn热图下添加matplotlib表。 我已经能够画出它们,但没有对齐的运气 # Main data df = pd.DataFrame({"A": [20, 10, 7, 39], "B": [1, 8, 12, 9], "C": [780, 800, 1200, 250]}) # It contains min and max v

我正在尝试在seaborn热图下添加matplotlib表。 我已经能够画出它们,但没有对齐的运气

# Main data
df = pd.DataFrame({"A": [20, 10, 7, 39], 
                   "B": [1, 8, 12, 9], 
                   "C": [780, 800, 1200, 250]})

# It contains min and max values for the df cols
df_info =  pd.DataFrame({"A": [22, 35], 
                   "B": [5, 10], 
                   "C": [850, 900]})

df_norm = (df - df.min())/(df.max() - df.min())


# Plot the heatmap
vmin = df_norm.min().min()
vmax = df_norm.max().max()

fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)
sns.heatmap(df_norm, ax=ax1, annot=df, cmap='RdBu_r', cbar=True)
当我将表格添加到
ax2
时,它以热图的所有宽度(包括颜色条)绘制。我尝试了所有可能的
loc
bbox
组合,但我无法准确地将表格居中,并使其与顶部热图的宽度(整个表格的宽度,但也包括单个单元格的宽度)相同

table = df_info
cell_text = []
for row in range(len(table)):
    cell_text.append(table.iloc[row])

ax2.axis('off')
ax2.table(cellText=cell_text,
          rowLabels=table.index,
          colLabels=None,
          loc='center')

有时我会将参数传递到热图,以打印方形单元格,结果如下:

问题:如何将桌子及其单元格连接到热图上并居中

编辑:从技术上讲,这是解决我的简单示例问题的正确方法。虽然我可能过度简化了它,并遗漏了一条重要信息。
在我的真实场景中,如果我使用以下方法创建图形:

fig, (ax1, ax2) = plt.subplots(nrows=2,
                               **{"figsize": (18, 18),
                                  "dpi": 200,
                                  "tight_layout": True})
应用上述答案,我得到如下结果:

桌子的底部远比热图宽

另外,如果我设置了
tight\u layout=False
我会得到一个宽度正确但底部仍然很远的表格:

fig, (ax1, ax2) = plt.subplots(nrows=2,
                               **{"figsize": (18, 18),
                                  "dpi": 200,
                                  "tight_layout": False})

我想在我的例子中,
“figsize”:(18,18)
紧凑的布局对我的问题有很大的影响,但我不知道为什么,也不知道如何解决它。

Short method 您可以使用移动/调整表的大小。可以根据需要调整
左侧
/
底部
/
宽度
/
高度
参数:

bbox1=ax1.get_position()
bbox2=ax2.get_position()
#根据需要修改
左=bbox1.x0
底部=bbox1.y0-(bbox2.height*0.8)
宽度=bbox1.x0+(bbox1.width*0.8)
高度=bbox2.5米高度
ax2.设置位置([左、下、宽、高])


长方法 如果简单方法不起作用,请尝试通过
plt.subplot()
中的
gridspec\u kw
设置轴
height\u ratio
。此外,我还需要设置
tight\u layout=False

使用
height\u ratio
设置
ax1:ax2
(热图:表格)的比率,并使用
*\u offset
变量根据需要调整表格的大小/位置。这些值适用于我的系统,但您可以调整您的系统:

###根据需要修改这些参数###
高度比=(20,1)#热图:桌面比(20:1)
左_偏移=0#工作台左位置调整
底部偏移=-0.025#工作台底部位置调整
宽度偏移=-0.0005#工作台宽度调整
高度偏移=0#工作台高度调整
#####################################
图kw=dict(图尺寸=(18,18),dpi=200,紧密布局=False)
gridspec_kw=dict(高度比=高度比)
图(ax1,ax2)=plt.子批次(nrows=2,gridspec\u-kw=gridspec\u-kw,**图\u-kw)
sns.heatmap(df_norm,ax=ax1,annot=df,cmap='RdBu_r',cbar=True)
ax2.table(cellText=[df_info.iloc[row]表示范围内的行(len(df_info)),
rowLabels=table.index,
colLabels=无,
loc='center')
ax2.轴(“关闭”)
bbox1=ax1.get_position()
bbox2=ax2.get_position()
左=bbox1.x0+左偏移
底部=bbox1.y0-bbox2.height+底部偏移
宽度=bbox1.width+width\u偏移
高度=bbox2.高度+高度偏移
ax2.设置位置([左、下、宽、高])

将此解决方案应用到我的简化示例中确实有效。但在我的真实情况下,它不是,我不知道为什么。我尝试过使用
set\u position
value,但是桌子就在底部,距离很远,而且很宽,它既不会移动也不会调整大小:\Hmm如果没有亲眼看到它,我想不出为什么会发生这种情况。如果你能为这种情况建立一个可复制的例子,我可以看一看。我已经更新了我的问题。@Leonardo看看更新的
较长版本
是否有用。