如何使用数据框中的散点图数据制作热图?

时间:2019-08-07 18:06:53

标签: python matplotlib plot heatmap histogram2d

我有一个数据框,其中包含我的x和y数据(以后转换为列表),并且对于每个x,y,我都有三个属性,它们是百分比(全部加起来为100%),例如,对于x ,y第一个属性是0.05,第二个属性是0.45,第三个属性是0.5。

         pc1       pc2      %_h        %_s      %_c  
0    -2.319093 -4.058990  0.718839  0.074559  0.206602  
1     1.514446 -2.324842  0.552632  0.157895  0.289474  
2    -2.431196 -1.938358  0.440313  0.071755  0.487932  
3    -2.642250 -1.001307  0.707883  0.058733  0.233385 
4    -1.486477 -2.537368  0.617834  0.151956  0.230209  
5    -1.990138 -3.457012  0.326633  0.088358  0.585008 
6    -0.844124 -3.081770  0.550000  0.113636  0.336364  
7    -2.376568 -1.471469  0.663071  0.196066  0.140863  
8    -3.139226  0.451762  0.696914  0.056173  0.246914  
    :

我的目标是制作某种热图,以绘制每个属性的x(pc1),y(pc2)和范围,因此较暗的部分将意味着特定属性的密度较高。 下面是使用的热图:

plt.figure(figsize = (16,16))
plt.hist2d(pc1, pc2, bins=50, cmap=plt.cm.jet)
plt.show()

plot 但这是基于x,y点的密度,但是我希望它基于我的3个属性,即红色区域用于%h/%s/%c值高的区域

我的研究表明,这与将数据网格化以及计算每个像元的频率以获得某种z维度有关。 我已经尝试过:Plotting a 2D heatmap with Matplotlib 和:Generate a heatmap in MatPlotLib using a scatter data set此仅基于2个维度) 而且没有运气。

我还想使用类似sns.jointmap功能的东西为每个x.y绘制三个属性的密度的折线图

编辑 我认为该方法将是绘制一个热图,该热图表示第一个属性,然后在另一个属性上依次层叠。但是我不确定如何设置热图代表属性的范围,而不是x,y的密度计数?

编辑 这是在尝试建议的代码之后。

plot2

1 个答案:

答案 0 :(得分:0)

您尝试过pcolormesh吗? 示例:https://matplotlib.org/3.1.1/gallery/images_contours_and_fields/pcolormesh_levels.html

fig, ax = plt.subplots() 
data = np.array([df['%_h'],df['%_s'],df['%_c']]).reshape(3,len(df))
cax =ax.pcolormesh(df.index, np.arange(0,3), data, shading = 'gouraud', cmap = 'jet')
fig.colorbar(cax)