如何将两个坐标之间的区域划分为块并为这些块分配一些值?

时间:2019-05-22 01:24:41

标签: python-3.x matplotlib

基本上,我必须创建一个区域内人群的热图。 我有两个坐标。 X从0开始,最大值为119994。Y从-14,000到+27,000。我必须根据需要将这些坐标划分为任意多个块,计算每个块中的人数并创建整个区域的热图。 基本上显示出该区域的拥挤程度。

state = {
  currentId: 0,
  pause: true,
  count: 0,
  storiesDone: 0
}
componentDidMount() {
  //Assign localStorage.get("currentId") to a variable
  const localCurrentId = localStorage.get("currentId");
  //Check if localCurrentId is not null or undefined
  if (localCurrentId) {
    this.setState(prevState => ({
      //Update state's currentId if the current value didn't match with localCurrentId
      currentId: prevState.currentId !== localCurrentId ? localCurrentId : prevState.currentId
    }));
  }
}

我尝试将两个坐标最大值都除以100(以生成100个块),并尝试找到块坐标,但这非常复杂。

当我必须制作热图时,我必须准备块形式的值矩阵。每个数据块都会有一定数量的人,我可以计算并从我的数据中找出来,但是问题是如何制作这些坐标块?

关于散点图,我还有另一个问题: 我的数据是:-

I have data in the below format:-
Employee_ID X_coord Y_coord_start Y_coord_end
    23       1333      0           6000
    45       3999    7000          17000

当我使用以下代码绘制它时:-

Batch_ID         Pieces_Productivity
181031008780     4.578886
181031008781     2.578886

它没有给我适当的阴谋。但是,当我使用Batch_ID的小整数(0-1000)进行绘制时,会得到良好的图形。如何处理大整数以进行绘图?

1 个答案:

答案 0 :(得分:0)

我既不知道Y_coord_行中的哪一行都应该给出实际的Y坐标,也不知道您的绘图是否应该在严格的“网格”上评估数据,或者应该比较平滑出来;因此,我在以下代码中同时使用了imshow()sns.kdeplot()

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

### generate some data
np.random.seed(0)
data = np.random.multivariate_normal([0, 0], [(1, .6), (.6, 1)], 100)

## this would e.g. be X,Y=df['X_coord'], df['Y_coord_start'] :
X,Y=data[:,0],data[:,1] 


fig,ax=plt.subplots(nrows=1,ncols=3,figsize=(10,5))
ax[0].scatter(X,Y)
sns.kdeplot(X,Y, shade=True, ax=ax[1],cmap="viridis")
## the X,Y points are binned into 10x10 bins here, you will need 
#  to adjust the amount of bins so that it looks "nice" for you
heatmap, xedges, yedges = np.histogram2d(X, Y, bins=(10,10))
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
im=ax[2].imshow(heatmap.T, extent=extent, 
            origin="lower",aspect="auto",
            interpolation="nearest") ## also play with different interpolations

## Loop over heatmap dimensions and create text annotations:
#  note that we need to "push" the text from the lower left corner of each pixel
#     into the center of each pixel 
## also try to choose a text color which is readable on all pixels, 
#     or e.g. use vmin=… vmax=  to adjust the colormap such that the colors
#     don't clash with e.g. white text

pixel_center_x=(xedges[1]-xedges[0])/2.
pixel_center_y=(yedges[1]-yedges[0])/2.
for i in range(np.shape(heatmap)[1]):
    for j in range(np.shape(heatmap)[0]):
        text = ax[2].text(pixel_center_x+xedges[j], pixel_center_y+yedges[i],'{0:0.0f}'.format(heatmap[j, i]),
                       ha="center", va="center", color="w",fontsize=6)

plt.colorbar(im)
plt.show()

产量:

Updated plot result