Python: Как построить несколько тепловых карт морского происхождения рядом? - PullRequest
0 голосов
/ 02 июля 2018

У меня есть датафрейм Pandas с более чем 20 функциями. Я хотел бы видеть их матрицы корреляции. Я создаю тепловые карты с кодом, подобным приведенному ниже, с subset1, subset2 и т. Д.:

import seaborn as sns
cmap = sns.diverging_palette( 220 , 10 , as_cmap = True )
sb1 = sns.heatmap(
    subset1.corr(), 
    cmap = cmap,
    square=True, 
    cbar_kws={ 'shrink' : .9 }, 
    annot = True, 
    annot_kws = { 'fontsize' : 12 })

Я хотел бы иметь возможность отображать несколько тепловых карт, сгенерированных приведенным выше кодом, бок о бок, например:

display_side_by_side(sb1, sb2, sb3, . . .)

Я не уверен, как это сделать, потому что первый фрагмент кода выше не только сохраняет результаты в sb1, но и отображает тепловую карту. Кроме того, не уверен, как написать функцию, display_side_by_side(). Я использую следующие данные для фреймов данных Pandas:

# create a helper function that takes pd.dataframes as input and outputs pretty, compact EDA results
from IPython.display import display_html
def display_side_by_side(*args):
    html_str = ''
    for df in args:
        html_str = html_str + df.to_html()
    display_html(html_str.replace('table','table style="display:inline"'),raw=True)

Основываясь на первом ответе Симаса Йонелюнаса, приведенном ниже, я нашел следующее рабочее решение:

import matplotlib.pyplot as plt
import seaborn as sns

# Here we create a figure instance, and two subplots
fig = plt.figure(figsize = (20,20)) # width x height
ax1 = fig.add_subplot(3, 3, 1) # row, column, position
ax2 = fig.add_subplot(3, 3, 2)
ax3 = fig.add_subplot(3, 3, 3)
ax4 = fig.add_subplot(3, 3, 4)
ax5 = fig.add_subplot(3, 3, 5)

# We use ax parameter to tell seaborn which subplot to use for this plot
sns.heatmap(data=subset1.corr(), ax=ax1, cmap = cmap, square=True, cbar_kws={'shrink': .3}, annot=True, annot_kws={'fontsize': 12})
sns.heatmap(data=subset2.corr(), ax=ax2, cmap = cmap, square=True, cbar_kws={'shrink': .3}, annot=True, annot_kws={'fontsize': 12})
sns.heatmap(data=subset3.corr(), ax=ax3, cmap = cmap, square=True, cbar_kws={'shrink': .3}, annot=True, annot_kws={'fontsize': 12})
sns.heatmap(data=subset4.corr(), ax=ax4, cmap = cmap, square=True, cbar_kws={'shrink': .3}, annot=True, annot_kws={'fontsize': 12})
sns.heatmap(data=subset5.corr(), ax=ax5, cmap = cmap, square=True, cbar_kws={'shrink': .3}, annot=True, annot_kws={'fontsize': 12})

1 Ответ

0 голосов
/ 02 июля 2018

Вы должны посмотреть на matplotlib.add_subplot:

# Here we create a figure instance, and two subplots
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)

# We use ax parameter to tell seaborn which subplot to use for this plot
sns.pointplot(x="x", y="y", data=data, ax=ax1)
...