从 pandas 数据框创建自定义热图 [英] Create custom heatmap from pandas dataframe

查看:62
本文介绍了从 pandas 数据框创建自定义热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个8行6028列的数据框.我想为第一列创建一个包含8行的热图(最终,我将创建一个动画,以便该地图更新每列的读数)

I have a dataframe with 8 rows and 6028 columns. I want to create a heatmap of the 8 rows for the first column (eventually I will create an animation so the map updates reading through each column)

这是数据框的摘要:

                       value                    
percentage_time         0.00      0.15      0.16
region                                          
Anterior Distal     0.111212  0.119385  0.116270
Anterior Proximal   0.150269  0.153613  0.168188
Lateral Distal      0.130440  0.137157  0.136494
Lateral Proximal    0.171977  0.182251  0.181090
Medial Distal       0.077468  0.082064  0.082553
Medial Proximal     0.194924  0.198803  0.199339
Posterior Distal    0.164124  0.171221  0.166328
Posterior Proximal  0.131310  0.145706  0.136094

我使用了以下代码,但它给出了一个图,其中堆积了索引,并且数据帧中的所有数据都是

I have used the following code but it gives me one plot with the indices stacked and all the data in the dataframe:

sns.heatmap(region_pressure_data)

当我尝试使用以下代码获取第一列时,我得到以下内容:

When I try to use the following code to get just the first column, I get the following:

sns.heatmap(region_pressure_data.ix[:,0:1])

理想情况下,我想要一张8个区域的地图,其中包含2行(近端和远端)和4列(前,外侧,后,内侧),显示一列的数据.

Ideally, I would like 1 map of 8 regions, with 2 rows (proximal and distal) and 4 columns (anterior, lateral, posterior, medial), displaying the data of one column.

对于使用此方法进行改进或是否有更好的方法来应对挑战的建议,我将不胜感激.

I'd appreciate any advice on progressing with this method or if there is a better way to approach the challenge.

谢谢.

推荐答案

索引中的数据必须是单元格的一部分,并且您可能希望使用数据透视表.为了说明,我创建了一些类似的数据框,但其中的列较少,以说明我在做什么.我希望这是您正在使用的结构?

The data in your indices needs to be part of the cells and you probably want a pivot. For explanation, I created some similar dataframe with less columns to illustrate what I am doing. I hope this is the structure you are using?

df = pd.DataFrame(index=["Anterior Distal", "Anterior Proximal", "Lateral Distal", "Lateral Proximal"], data={0.:[1,2,3,4], 1.:[5,6,7,8]})
print(df)                                                                     
>>>
                   0.0  1.0
region                     
Anterior Distal      1    5
Anterior Proximal    2    6
Lateral Distal       3    7
Lateral Proximal     4    8

据我了解,您想显式地引用索引的两个部分,因此您需要先拆分索引.例如,您可以通过以下方式执行此操作:首先使用pandas方法拆分字符串,然后将其转换为可切片的numpy数组

As I understand it, you want to explicitly refer to the two parts of your index, so you will need to split the index first. You can do this for example in this way which first uses a pandas method to split the strings and then transforms it to a numpy array which you can slice

index_parts = np.array(df.index.str.split().values.tolist())
index_parts[:,0]
>>> array(['Anterior', 'Anterior', 'Lateral', 'Lateral'], dtype='<U8')

现在,您可以将其添加为新列

Now, you can add those as new columns

df["antlat"] = index_parts[:,0]
df["distprox"] = index_parts[:,1]
print(df)
>>>
                   0.0  1.0    antlat  distprox
region                                         
Anterior Distal      1    5  Anterior    Distal
Anterior Proximal    2    6  Anterior  Proximal
Lateral Distal       3    7   Lateral    Distal
Lateral Proximal     4    8   Lateral  Proximal

然后,您可以为您感兴趣的值创建枢轴

Then you can create the pivot for the value you are interested in

df_pivot = df.pivot(index="antlat", columns="distprox", values=0.0)
print(df_pivot)
>>>
distprox  Distal  Proximal
antlat                    
Anterior       1         2
Lateral        3         4

并绘制它(请注意,这只是2x2,因为我没有在示例中添加Medial和Posterior)

And plot it (note that this is only 2x2, since I did not add Medial and Posterior to the example)

sns.heatmap(df_pivot)

这篇关于从 pandas 数据框创建自定义热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆