简体   繁体   English

从熊猫数据帧创建自定义热图

[英]Create custom heatmap from pandas dataframe

I have a dataframe with 8 rows and 6028 columns.我有一个 8 行 6028 列的数据框。 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)我想为第一列创建一个 8 行的热图(最终我将创建一个动画,以便地图更新读取每列)

This is a snippet of the dataframe:这是数据框的片段:

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

在此处输入图片说明

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.理想情况下,我想要一张 8 个区域的地图,2 行(近端和远端)和 4 列(前、侧、后、内侧),显示一列的数据。

I'd appreciate any advice on progressing with this method or if there is a better way to approach the challenge.如果有任何关于使用这种方法取得进展的建议,或者是否有更好的方法来应对挑战,我将不胜感激。

Thanks in advance.提前致谢。

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

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例如,您可以通过这种方式执行此操作,首先使用 Pandas 方法拆分字符串,然后将其转换为可以切片的 numpy 数组

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

And plot it (note that this is only 2x2, since I did not add Medial and Posterior to the example)并绘制它(请注意,这只是 2x2,因为我没有在示例中添加内侧和后侧)

sns.heatmap(df_pivot)

在此处输入图片说明

Why not using directly matplotlib ?为什么不直接使用matplotlib :D :D

import matplotlib.pyplot as plt
plt.imshow(df.reset_index(drop=True).values[:,1:].astype("float"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM