Seaborn 热图未将数据正确放置在轴上

我是使用Seaborn的新手,通常只使用马特洛利布。


随着最近的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笔记本的链接,下面是给我带来问题的部分的片段。


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

########################### 


慕桂英4014372
浏览 101回答 1
1回答

凤凰求蛊

您的数据帧的形状不正确。例如,作为操作的结果,您有 一个 MultiIndex,它仅用于绘图函数的标记。seaborn.heatmap()groupbyPopulation_ChangeExpense_Change因此,首先删除多余的列,而不是 ,然后执行以下操作:groupbydf = df.pivot(index='Expense_Change', columns='Population_Change', values='New_Total_Fee')然后应该按预期工作。seaborn.heatmap(df)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python