Skip to content
Advertisement

Get information from plt.hexbin

I have been using hexbing to try to improve the SPCF method, so for that I would need to get information from the plot to modify the data and then replot it with the new values.

The most important data I need to modify to replot is the number of points per cell and the C value of that grid cell.

Any way I can get such data?

Advertisement

Answer

matplotlib.pyplot.hexbin returns a PolyCollection object with methods get_offsets and get_array which provide exactly what you’re looking for. From the hexbin docs:

Returns: PolyCollection
A PolyCollection defining the hexagonal bins.

If marginals is True, horizontal bar and vertical bar (both PolyCollections) will be attached to the return collection as attributes hbar and vbar.

Example

As an example, I’ll create a simple hexbin plot and grab the PolyCollection object returned by plt.hexbin:

In [2]: x = np.linspace(1, 20, 500)

In [3]: y = np.log(x) + np.random.random(size=500) - 0.5

In [4]: coll = plt.hexbin(x, y, gridsize=20)

hexbin plot

You can explore the collection object and grab the arrays returned by get_offsets() and get_array():

In [23]: type(coll)
Out[23]: matplotlib.collections.PolyCollection

In [24]: offsets = coll.get_offsets()

In [25]: offsets
Out[25]:
array([[ 9.99999981e-01, -3.48964611e-01],
       [ 9.99999981e-01, -3.91102264e-03],
       [ 9.99999981e-01,  3.41142565e-01],
       ...
       [ 1.95250000e+01,  2.58399089e+00],
       [ 1.95250000e+01,  2.92904448e+00],
       [ 1.95250000e+01,  3.27409806e+00]])

In [26]: arr = coll.get_array()

In [27]: arr
Out[27]:
masked_array(data=[ 2.,  4.,  0.,  ...,  7.,  2.,  4.],
             mask=False,
       fill_value=1e+20)

In [28]: offsets.shape
Out[28]: (472, 2)

In [29]: arr.shape
Out[29]: (472,)

You could manipulate these arrays however you want, and then plot them again using the x, y, and C count argument to hexbin. If you don’t change the values of offsets and arr, this plots the exact same image:

In [30]: plt.hexbin(offsets[:, 0], offsets[:, 1], C=arr, gridsize=20)
Advertisement