Get corresponding hex bin centers for each point of a hexbin in python

Viewed 96

I am looking for a way to find the corresponding bin center for every point in my hexbin plot. For example, here is my plot:

hex = plt.hexbin(
    df['loc_x'], df['loc_y'],
    extent=(-250, 250, 422.5, -47.5), 
    cmap=plt.cm.Blues, gridsize=15,mincnt=0
    )

enter image description here

Here is the data used for it:

{'loc_x': {0: 165,
  1: 133,
  2: -151,
  3: -18,
  4: 0,
  5: -214,
  6: 0,
  7: -25,
  8: -222,
  9: 0,
  10: -1,
  11: -54,
  12: -170,
  13: -3,
  14: 193,
  15: 33,
  16: 100,
  17: -69,
  18: 0,
  19: 0,
  20: -23,
  21: -70,
  22: 0,
  23: -228,
  24: -147,
  25: 24,
  26: 78,
  27: 84,
  28: -105,
  29: -41,
  30: -143,
  31: 0,
  32: 13,
  33: 0,
  34: -181,
  35: -143,
  36: -122,
  37: 89,
  38: 0,
  39: 21,
  40: 10,
  41: -15,
  42: -149,
  43: -151,
  44: -219,
  45: 0,
  46: 0,
  47: 135,
  48: 9,
  49: 0,
  50: 10,
  51: 5,
  52: 132,
  53: 0,
  54: -40,
  55: 51,
  56: 176,
  57: 72,
  58: 241,
  59: -130,
  60: 124,
  61: 0,
  62: 192,
  63: 72,
  64: 0,
  65: 171,
  66: 17,
  67: -26,
  68: -164,
  69: 0,
  70: 40,
  71: 2,
  72: -201,
  73: -15,
  74: 53,
  75: 48,
  76: 113,
  77: -157,
  78: 88,
  79: 13,
  80: -5,
  81: -90,
  82: -216,
  83: 0,
  84: -202,
  85: -65,
  86: 0,
  87: 0,
  88: 231,
  89: 0,
  90: 0,
  91: 41,
  92: 170,
  93: 66,
  94: 7,
  95: -149,
  96: 149,
  97: 0,
  98: 29,
  99: 68},
 'loc_y': {0: 138,
  1: 105,
  2: 38,
  3: 0,
  4: 0,
  5: 2,
  6: 94,
  7: 103,
  8: 15,
  9: 0,
  10: 54,
  11: 230,
  12: 210,
  13: 86,
  14: 3,
  15: 135,
  16: 66,
  17: 56,
  18: 0,
  19: 0,
  20: 7,
  21: 160,
  22: 0,
  23: 7,
  24: 6,
  25: 91,
  26: 235,
  27: 49,
  28: 125,
  29: 148,
  30: 156,
  31: 0,
  32: 172,
  33: 0,
  34: 124,
  35: 191,
  36: -19,
  37: 99,
  38: 0,
  39: 7,
  40: 3,
  41: 7,
  42: 85,
  43: 53,
  44: 97,
  45: 0,
  46: 0,
  47: 100,
  48: 93,
  49: 0,
  50: 11,
  51: 212,
  52: 115,
  53: 0,
  54: 169,
  55: 3,
  56: 146,
  57: 25,
  58: -17,
  59: 194,
  60: 222,
  61: 0,
  62: 89,
  63: 56,
  64: 0,
  65: 36,
  66: 7,
  67: 22,
  68: 23,
  69: 0,
  70: 84,
  71: 1,
  72: 163,
  73: 9,
  74: 23,
  75: 29,
  76: 101,
  77: 132,
  78: 30,
  79: 213,
  80: 155,
  81: 81,
  82: 17,
  83: 0,
  84: 104,
  85: 196,
  86: 0,
  87: 0,
  88: 37,
  89: 102,
  90: 0,
  91: 205,
  92: 168,
  93: 51,
  94: -5,
  95: 160,
  96: 113,
  97: 0,
  98: 3,
  99: 148}}

I would like to create two columns, perhaps called center_x and center_y, that gets the center of each point's corresponding hexbin. I know I have hex.get_offsets() at my disposal, but that just returns each hexbin center. I want to get each center for each point. hex.get_array() just gets the count of points in each bin, so I don't think that'll help here.

An example of my desired output would be

    x  y  center_x   center_y
0 -54 75   -50         70
...

As those are the coordinates for the center of the hexbin for which observation 0 lies. I would imagine there has to be a way to do this, but I can't seem to find anything in the docs.

0 Answers
Related