Seaborn热图将多个标签组合为xticks

时间:2019-06-16 18:31:12

标签: python pandas seaborn pandas-groupby

我试图绘制一个热图,以显示十年来数据泄露的不同原因以及受影响的个人数量。热图显示没有错误。但是,通过将年份和“受影响的个人”标签结合在一起来标记小标签。

causes_of_breaches_by_year = pd.DataFrame(
  df.groupby([
    'Breach Submission Year',
    'Type of Breach'])[['Individuals Affected']]
  .count())
  .reset_index()

causes_of_breaches_by_year['Individuals Affected'] =
    causes_of_breaches_by_year['Individuals Affected']
    .fillna(0)
    .astype(np.int64)

data_breaches_per_year_heatmap = causes_of_breaches_by_year.pivot(
    'Type of Breach',
    'Breach Submission Year'
  )

sns.heatmap(
    annot=True,
    cmap="YlGnBu",
    cbar_kws={'label': 'Individuals Affected'},
    data=data_breaches_per_year_heatmap
  )
  .set_title('Cause of Data Breach by Year')

1 个答案:

答案 0 :(得分:0)

简短答案

旋转时,必须给出3个参数。参见pandas.DataFrame.pivot

DataFrame.pivot(index=None, columns=None, values=None)[source]

或者您的情况

piv = df.pivot(index='Type of Breach',
               columns='Breach Submission Year',
               values='Individuals Affected'
               )

示例

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

sns.set()

df = pd.DataFrame({
    'Breach Submission Year': ['2018', '2018', '2018', '2019', '2019', '2019'],
    'Type of Breach': ['A', 'B', 'C', 'A', 'B', 'C'],
    'Individuals Affected': [1, 2, 6, 2, 5, 6]
})

piv = df.pivot('Type of Breach', 'Breach Submission Year',
               'Individuals Affected')
piv = df.pivot(index='Type of Breach',
               columns='Breach Submission Year',
               values='Individuals Affected'
               )

sns.heatmap(
    annot=True,
    cmap="YlGnBu",
    cbar_kws={'label': 'Individuals Affected'},
    data=piv
).set_title('Cause of Data Breach by Year')

plt.show()

enter image description here