pandas.DataFrame.plot.hexbin#
- DataFrame.plot.hexbin(x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs)[源代码]#
生成六边形分箱图。
生成 x 对 y 的六边形分箱图。如果 C 为 None(默认值),则这是对
(x[i], y[i])处观测值出现次数的直方图。如果指定了 C,则指定给定坐标
(x[i], y[i])的值。这些值会累积到每个六边形箱中,然后根据 reduce_C_function 进行归约,默认为 NumPy 的均值函数(numpy.mean())。(如果指定了 C,它也必须是一个与 x 和 y 长度相同的 1-D 序列,或者是一个列标签。)- 参数:
- xint or str
x 点的列标签或位置。
- yint or str
y 点的列标签或位置。
- Cint or str, optional
(x, y) 点的列标签或位置。
- reduce_C_functioncallable, default np.mean
一个参数的可调用函数,它将 bin 中的所有值归约到一个数字(例如 np.mean、np.max、np.sum、np.std)。
- gridsizeint or tuple of (int, int), default 100
x 方向上的六边形数量。y 方向上的相应六边形数量被选择,使得六边形近似为规则的。或者,gridsize 可以是一个包含两个元素的元组,分别指定 x 方向和 y 方向上的六边形数量。
- **kwargs
其他关键字参数记录在
DataFrame.plot()中。
- 返回:
- matplotlib.Axes
绘制六边形图的 matplotlib
Axes。
另请参阅
DataFrame.plot绘制 DataFrame 的图。
matplotlib.pyplot.hexbin使用 matplotlib 的六边形分箱图,matplotlib 是底层使用的函数。
示例
以下示例是使用正态分布的随机数据生成的。
>>> n = 10000 >>> df = pd.DataFrame({"x": np.random.randn(n), "y": np.random.randn(n)}) >>> ax = df.plot.hexbin(x="x", y="y", gridsize=20)
下一个示例使用了 C 和 np.sum 作为 reduce_C_function。请注意,‘observations’ 的值范围从 1 到 5,但结果图显示的值超过 25。这是因为 reduce_C_function 的作用。
>>> n = 500 >>> df = pd.DataFrame( ... { ... "coord_x": np.random.uniform(-3, 3, size=n), ... "coord_y": np.random.uniform(30, 50, size=n), ... "observations": np.random.randint(1, 5, size=n), ... } ... ) >>> ax = df.plot.hexbin( ... x="coord_x", ... y="coord_y", ... C="observations", ... reduce_C_function=np.sum, ... gridsize=10, ... cmap="viridis", ... )