Skip to content

Commit e5975fc

Browse files
BielStelajorisvandenbossche
authored andcommitted
DOC: updated the pandas.DataFrame.plot.hexbin docstring (#20121)
1 parent cad6dc7 commit e5975fc

File tree

1 file changed

+70
-11
lines changed

1 file changed

+70
-11
lines changed

pandas/plotting/_core.py

+70-11
Original file line numberDiff line numberDiff line change
@@ -3174,26 +3174,85 @@ def scatter(self, x, y, s=None, c=None, **kwds):
31743174
def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None,
31753175
**kwds):
31763176
"""
3177-
Hexbin plot
3177+
Generate a hexagonal binning plot.
3178+
3179+
Generate a hexagonal binning plot of `x` versus `y`. If `C` is `None`
3180+
(the default), this is a histogram of the number of occurrences
3181+
of the observations at ``(x[i], y[i])``.
3182+
3183+
If `C` is specified, specifies values at given coordinates
3184+
``(x[i], y[i])``. These values are accumulated for each hexagonal
3185+
bin and then reduced according to `reduce_C_function`,
3186+
having as default the NumPy's mean function (:meth:`numpy.mean`).
3187+
(If `C` is specified, it must also be a 1-D sequence
3188+
of the same length as `x` and `y`, or a column label.)
31783189
31793190
Parameters
31803191
----------
3181-
x, y : label or position, optional
3182-
Coordinates for each point.
3183-
C : label or position, optional
3184-
The value at each `(x, y)` point.
3185-
reduce_C_function : callable, optional
3192+
x : int or str
3193+
The column label or position for x points.
3194+
y : int or str
3195+
The column label or position for y points.
3196+
C : int or str, optional
3197+
The column label or position for the value of `(x, y)` point.
3198+
reduce_C_function : callable, default `np.mean`
31863199
Function of one argument that reduces all the values in a bin to
3187-
a single number (e.g. `mean`, `max`, `sum`, `std`).
3188-
gridsize : int, optional
3189-
Number of bins.
3190-
`**kwds` : optional
3200+
a single number (e.g. `np.mean`, `np.max`, `np.sum`, `np.std`).
3201+
gridsize : int or tuple of (int, int), default 100
3202+
The number of hexagons in the x-direction.
3203+
The corresponding number of hexagons in the y-direction is
3204+
chosen in a way that the hexagons are approximately regular.
3205+
Alternatively, gridsize can be a tuple with two elements
3206+
specifying the number of hexagons in the x-direction and the
3207+
y-direction.
3208+
**kwds
31913209
Additional keyword arguments are documented in
31923210
:meth:`pandas.DataFrame.plot`.
31933211
31943212
Returns
31953213
-------
3196-
axes : :class:`matplotlib.axes.Axes` or numpy.ndarray of them
3214+
matplotlib.AxesSubplot
3215+
The matplotlib ``Axes`` on which the hexbin is plotted.
3216+
3217+
See Also
3218+
--------
3219+
DataFrame.plot : Make plots of a DataFrame.
3220+
matplotlib.pyplot.hexbin : hexagonal binning plot using matplotlib,
3221+
the matplotlib function that is used under the hood.
3222+
3223+
Examples
3224+
--------
3225+
The following examples are generated with random data from
3226+
a normal distribution.
3227+
3228+
.. plot::
3229+
:context: close-figs
3230+
3231+
>>> n = 10000
3232+
>>> df = pd.DataFrame({'x': np.random.randn(n),
3233+
... 'y': np.random.randn(n)})
3234+
>>> ax = df.plot.hexbin(x='x', y='y', gridsize=20)
3235+
3236+
The next example uses `C` and `np.sum` as `reduce_C_function`.
3237+
Note that `'observations'` values ranges from 1 to 5 but the result
3238+
plot shows values up to more than 25. This is because of the
3239+
`reduce_C_function`.
3240+
3241+
.. plot::
3242+
:context: close-figs
3243+
3244+
>>> n = 500
3245+
>>> df = pd.DataFrame({
3246+
... 'coord_x': np.random.uniform(-3, 3, size=n),
3247+
... 'coord_y': np.random.uniform(30, 50, size=n),
3248+
... 'observations': np.random.randint(1,5, size=n)
3249+
... })
3250+
>>> ax = df.plot.hexbin(x='coord_x',
3251+
... y='coord_y',
3252+
... C='observations',
3253+
... reduce_C_function=np.sum,
3254+
... gridsize=10,
3255+
... cmap="viridis")
31973256
"""
31983257
if reduce_C_function is not None:
31993258
kwds['reduce_C_function'] = reduce_C_function

0 commit comments

Comments
 (0)