带有seaborn的subplot2grid覆盖相同的ax

时间:2016-10-12 19:12:45

标签: python matplotlib seaborn

指定我想要图表的轴的正确方法是什么?

目前我正在尝试绘制不同的热图,每个热图都在不同的轴上。但是在尝试这个时,它只是将2个图表一个在另一个上面。

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

1 个答案:

答案 0 :(得分:0)

您只需将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