Gabriel
Gabriel

Reputation: 3807

subplot2grid with seaborn overwrites same ax

What is the correct way of specifying the ax where I want a chart to go?

Currently I'm trying to plot different heatmaps, each one in a different ax. But when trying this it just plots the 2 charts one on top of the other.

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})

and this is how it looks: enter image description here

Upvotes: 1

Views: 928

Answers (1)

Paul H
Paul H

Reputation: 68186

You just need to pass your Axes objects to the heatmap function:

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

Upvotes: 2

Related Questions