【问题标题】:Seaborn Heatmap Not Placing Data on Axes ProperlySeaborn 热图未将数据正确放置在轴上
【发布时间】:2020-04-10 17:59:55
【问题描述】:

我是使用 Seaborn 的新手,通常只使用 Matplotlib.pyplot。

随着最近 COVID 的发展,一位主管要求我汇总估计学生人数和费用的变化,我们需要为受影响的学生费用提供资金(我在大学预算办公室工作)。我已经能够将我的场景分析放在一起,但现在我试图在热图中可视化这些结果。

我想做的是拥有:

 x-axis be my population change rates,
 y_axis be my expense change rates,
 cmap be my new student fees depending on the x & y axis.

我的代码目前正在做的是:

 x-axis is displaying the new student fee category (not sure how to describe this - see picture)
 y-axis is displaying the population change and expense change (population, expenses)
 cmap is displaying accurately

基本上,我的代码沿 y 轴将每个场景堆叠在其他场景之上。

这是当前正在生产的图片,这是不正确的:

我用我的代码附加了一个指向Colab Jupyter notebook 的链接,下面是给我带来问题的部分的 sn-p。

# Create Pandas DF of Scenario Analysis       
df = pd.DataFrame(list(zip(Pop, Exp, NewStud, NewTotal)), 
                  index = [i for i in range(0,len(NewStud))], 
                  columns=['Population_Change', 'Expense_Change', 'New_Student_Activity_Fee', 'New_Total_Fee'])

# Group this scenario analysis 
df = df.groupby(['Population_Change', 'Expense_Change'], sort=False).max()

# Create Figure 
fig = plt.figure(figsize=(15,8))
ax = plt.subplot(111)

# Drop New Student Activity Fee Column. Analyze Only New Total Fee
df = df.drop(['New_Student_Activity_Fee'], axis=1)

########################### Not Working As Desired
sb.heatmap(df)
########################### 

【问题讨论】:

    标签: python pandas matplotlib seaborn


    【解决方案1】:

    您的 DataFrame 的形状不适合 seaborn.heatmap()。例如,作为groupby 操作的结果,您将Population_ChangeExpense_Change 作为MultiIndex,它仅用于绘图函数的标记。

    所以不要使用groupby,而是先删除多余的列,然后执行以下操作:

    df = df.pivot(index='Expense_Change', columns='Population_Change', values='New_Total_Fee')
    

    那么seaborn.heatmap(df) 应该会按预期工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 2021-12-05
      • 2018-08-07
      • 2016-03-17
      • 2021-08-03
      • 1970-01-01
      • 2017-04-16
      相关资源
      最近更新 更多