带有seaborn的子blot2grid覆盖相同的ax

2024-06-16 10:04:06 发布

您现在位置:Python中文网/ 问答频道 /正文

在我希望图表指向的位置指定ax的正确方法是什么?在

目前我正在尝试绘制不同的热图,每一张都在不同的斧头上。但当尝试这个时,它只是把两个图表一个接一个地画出来。在

import seaborn as sns 
import matplotlib.pyplot as plt

fig3 = plt.figure(figsize=(12,10))

ax1     = plt.subplot2grid((11,2),(0,0), rowspan=3, colspan=1)
ax2     = plt.subplot2grid((11,2),(4,0), rowspan=3, colspan=1)

ax1 = sns.heatmap(dict_pivots['df_pivot_10_win_2_thres'], square=False, cmap="RdYlBu", 
                    linewidths=0.1, annot=True, annot_kws={"size":12})

ax2 = sns.heatmap(dict_pivots['df_pivot_5_win_2_thres'], square=False, cmap="RdYlBu", 
                    linewidths=0.1, annot=True, annot_kws={"size":12})

它看起来是这样的: enter image description here


Tags: importdfas图表pltdictsnsheatmap
1条回答
网友
1楼 · 发布于 2024-06-16 10:04:06

只需将Axes对象传递给heatmap函数:

import seaborn as sns 
import matplotlib.pyplot as plt

fig3 = plt.figure(figsize=(12,10))

ax1 = plt.subplot2grid((11,2),(0,0), rowspan=3, colspan=1)
ax2 = plt.subplot2grid((11,2),(4,0), rowspan=3, colspan=1)

ax1 = sns.heatmap(dict_pivots['df_pivot_10_win_2_thres'],
                  square=False,  cmap="RdYlBu",
                  linewidths=0.1, annot=True,
                  annot_kws={"size":12},
                  ax=ax1)  # <  here

ax2 = sns.heatmap(dict_pivots['df_pivot_5_win_2_thres'],
                  square=False,  cmap="RdYlBu",
                  linewidths=0.1, annot=True,
                  annot_kws={"size":12},
                  ax=ax2)  # <  and here

相关问题 更多 >