3

I would like to count the number of points in each cell. I have tried https://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram2d.html but the out isn't appropriate. Does anybody know how I can get the output properly?

XA_ = [269142.68863031495,
 269304.695760036,
 269466.0264386978,
 392942.21775866585,
 392942.1996439248,
 392942.18152918364,
 254311.33475757603,
 254755.19249382117,
 255185.41421253807,
 492148.2779438756,
 492148.25401910784,
 492149.2251416461,
 553489.7091844846,
 550809.3067433595,
 548052.5477973885,
 519079.7628172271,
 519079.76481877314,
 519079.7654325806,
 490406.2223452815,
 490405.9016914209,
 490405.80859746644,
 494940.3091149014,
 494940.5800286697,
 494940.8509424379,
 546707.1663319349,
 546707.1306325911,
 546707.0949332472,
 772902.3863664756,
 770096.23941861,
 767309.362644247,
 435988.6088680159,
 435987.50805015286,
 435987.8836906329,
 380576.07212457457,
 380576.07212457457,
 380576.07212457457,
 562355.5408083306,
 562082.6254618462,
 561634.2589078951,
 519593.2248320134,
 520163.14482233016,
 520488.3862171857,
 280709.43500114995,
 282667.54256107233,
 284629.027003666,
 470095.25384248036,
 470097.9496341577,
 470100.0199537931,
 498019.63516434917,
 498019.636166123,
 498019.71342578344,
 545429.26374282,
 545429.4701223552,
 545429.6765018903,
 560256.948519165,
 560256.9606162632,
 560256.9727133614,
 558056.8350144841,
 558056.6628962351,
 558056.4907779862,
 467082.92585511395,
 466337.26696158113,
 466122.5451118541,
 500312.4931377756,
 500551.29173690686,
 500822.4915701299,
 488320.8881096475,
 488320.8881096475,
 488320.8881096475,
 234959.4216230074,
 236845.74740121586,
 238718.82347044855,
 363174.86611545534,
 364935.9064735326,
 366706.2106342295,
 497033.57866991305,
 497034.0968566288,
 497034.61504334444,
 293350.87277344253,
 295267.91084818705,
 297205.4022341429,
 281289.02387468005,
 283835.4214070084,
 286407.3507544949,
 280238.8325805849,
 281953.0134381577,
 283686.984369215,
 557866.4127702332,
 557767.2853572157,
 557455.4201240427,
 544276.0818856795,
 544277.1213996302,
 544278.160913581,
 560196.4346866631,
 559906.2555994007,
 559172.798667107,
 553310.5854939698,
 553312.2779971804,
 553311.6820767054,
 551348.8430010471,
 551349.4377773844,
 551348.8386734851,
 541249.4470653739,
 541900.7794716425,
 541224.3467305955,
 554234.8757064611,
 554234.8483127754,
 554234.8209190895,
 545745.6891595804,
 545745.5111079012,
 545745.3330562222,
 484873.02436431707,
 484003.1661958206,
 483398.14173198934,
 493168.4175602927,
 493389.0097356766,
 493077.69151527993,
 465367.8596565175,
 467844.8061345208,
 470322.080322923,
 542718.2282091405,
 542719.3318877186,
 542720.4355662968,
 501776.6308987725,
 500612.92108499294,
 498837.2422073845,
 774472.767904973,
 772926.2774970894,
 771366.855492034,
 537081.1411321913,
 537081.1003055439,
 537081.0594788966,
 375647.9072125684,
 373962.44197150605,
 372269.5267818782,
 339410.01580095495,
 339722.0521972096,
 340034.08859346434,
 528523.2346622795,
 529138.3610987444,
 529858.6496138738,
 528863.0461216161,
 529027.9678429372,
 529772.1385178919,
 544459.4872846468,
 544459.1089965965,
 544458.7307085461,
 280433.304815039,
 282248.8618037564,
 284050.50736230274]

YA_ = [5132177.351959863,
 5132454.958881595,
 5132745.370623719,
 4799027.16964639,
 4799027.176172647,
 4799027.182698905,
 4715865.972909568,
 4713622.766967746,
 4711410.429097172,
 5457995.491064864,
 5457995.141018024,
 5457993.652179124,
 4186724.748168416,
 4192055.050270996,
 4196836.825863356,
 5055792.355154564,
 5055791.7003405765,
 5055791.499530954,
 5457761.110190328,
 5457760.810745233,
 5457760.905925446,
 5425090.107417448,
 5425089.945452439,
 5425089.783487429,
 5278376.892017033,
 5278376.926682054,
 5278376.961347076,
 3794053.371397486,
 3794709.5665947185,
 3795368.8242837745,
 5115530.463440255,
 5115531.452352677,
 5115530.8992273975,
 5358357.1023906255,
 5358357.1023906255,
 5358357.1023906255,
 4183098.247108328,
 4182948.827814135,
 4182885.9698557155,
 5407586.030581906,
 5406415.191339511,
 5405865.425063687,
 5375278.898674618,
 5375121.025397821,
 5374989.029128767,
 5116484.487521737,
 5116484.474359201,
 5116484.327957711,
 5460928.317758323,
 5460931.097035308,
 5460932.16597176,
 5275208.30780482,
 5275208.618292654,
 5275208.928780487,
 4191410.6479157065,
 4191410.6712179165,
 4191410.694520126,
 5316862.846383741,
 5316862.315750707,
 5316861.785117673,
 5330678.414959482,
 5330671.996565787,
 5330418.313207476,
 4163275.3309627674,
 4163226.6567090345,
 4163777.7841287856,
 5384017.101647103,
 5384017.101647103,
 5384017.101647103,
 4090311.256141706,
 4090693.785906165,
 4091058.6390411435,
 5370953.649420215,
 5370851.631358324,
 5370724.235093637,
 5460877.6502608415,
 5460877.121433894,
 5460876.592606948,
 5104032.844699361,
 5104258.580699895,
 5104481.703445772,
 5377703.767245022,
 5377314.848272017,
 5376930.082459292,
 5373566.998644971,
 5373499.640390588,
 5373424.392858532,
 5316718.793442256,
 5315880.204122022,
 5315034.313263214,
 5280653.029302435,
 5280653.093216645,
 5280653.157130855,
 4191298.0409879684,
 4191017.0872896235,
 4190645.2342315284,
 4183569.0872102487,
 4183569.1835233,
 4183569.649855316,
 4184723.109379307,
 4184722.3671754412,
 4184723.8510904764,
 5249410.077029127,
 5248611.795298904,
 5247698.280675454,
 4195846.819949482,
 4195846.808210647,
 4195846.796471813,
 5279541.718556855,
 5279541.628946132,
 5279541.539335408,
 5378985.391374318,
 5381918.20070081,
 5384888.530195987,
 5461865.454342501,
 5462165.871095877,
 5461872.279347949,
 3608063.7852862985,
 3607970.9720990215,
 3607876.5264585214,
 5233854.779060345,
 5233851.871695311,
 5233848.9643302765,
 5459987.914939486,
 5460436.0631904565,
 5460259.83089087,
 3793294.915596035,
 3793721.698014937,
 3794172.4687402523,
 5399204.188631518,
 5399204.188361312,
 5399204.188091108,
 5119433.976897289,
 5119442.723172148,
 5119502.80378085,
 5547685.238786915,
 5546913.531515202,
 5546141.824243488,
 5267654.5443007015,
 5267958.675113996,
 5268557.912623625,
 5373282.781331167,
 5373224.423177257,
 5373304.47750142,
 5281026.651830151,
 5281026.488362015,
 5281026.32489388,
 5373438.891298835,
 5373382.566426716,
 5373347.855447044]



gridx = np.linspace(300000, 800000, 5)
gridy = np.linspace(3700000, 5500000, 5)
grid, _, _ = np.histogram2d(XA_, YA_, bins=[gridx, gridy])
plt.figure()
plt.plot(XA_, YA_, 'ro')
plt.grid(True)
plt.figure()
plt.pcolormesh(gridx, gridy, grid)
plt.plot(XA_, YA_, 'ro')
plt.colorbar()
plt.show()

My output My output

Danial
  • 81
  • 1
  • 2
  • 4

1 Answers1

4

If it is a geospatial problem (points), use the geospatial python modules

1) With shapely and GeoPandas for example

The points

import geopandas as gpd
from shapely.geometry import Point
points = gpd.GeoDataFrame({"x":XA_,"y":YA_})
points['geometry'] = points.apply(lambda p: Point(p.x, p.y), axis=1)
print(points.head(2))
          x             y                            geometry
0  269142.68863  5132177.35196  POINT (269142.688630315 5132177.351959863)
1  269304.69576  5132454.95888  POINT (269304.695760036 5132454.958881595)

The grid (polygons -> look at numpy meshgrid to Shapely polygons)

from shapely.ops import polygonize
hlines = [((x1, yi), (x2, yi)) for x1, x2 in list(zip(gridx[:-1], gridx[1:])) for yi in gridy]
vlines = [((xi, y1), (xi, y2)) for y1, y2 in zip(gridy[:-1], gridy[1:]) for xi in gridx]
polys = list(polygonize(MultiLineString(hlines + vlines)))
id = [i for i in range(len(grids))]
grid = gpd.GeoDataFrame({"id":id,"geometry":polys})
print(grid.head(2))
   id                                           geometry
0   0  POLYGON ((425000 3700000, 300000 3700000, 300000 4150000, 425000 4150000, 425000 3700000))
1   1  POLYGON ((425000 4150000, 300000 4150000, 300000 4600000, 425000 4600000, 425000 4150000))

(you need to specify plt.axis('equal') or plt.axes().set_aspect('equal') to plot correctly your data)

enter image description here

Number of points in polygons (look at More Efficient Spatial join in Python without QGIS, ArcGIS, PostGIS, etc)

from geopandas.tools import sjoin
pointInPolys = sjoin(points, grid, how='left')
print(pointInPolys.groupby(['id']).size().reset_index(name='count'))
   id  count
0   2     3
1   3     9
2   5     4
3   7    72
4   9    20
5   11    6
6   12    6

2) You can also use Quadrat Based Statistical Method for Planar Point Patterns with pointpats: Point Pattern Analysis in PySAL for that.

gene
  • 54,868
  • 3
  • 110
  • 187