scatter_hist: 创建散点直方图

一个快速生成散点直方图的函数。

from mlxtend.plotting import scatter_hist

概述

参考资料

  • https://matplotlib.net.cn/gallery/lines_bars_and_markers/scatter_hist.html

示例 1 - 从 Pandas DataFrames 创建散点图和直方图

from mlxtend.data import iris_data
from mlxtend.plotting import scatter_hist
import pandas as pd


X, y = iris_data()
df = pd.DataFrame(X)
df.columns = ['sepal length [cm]', 'sepal width [cm]', 'petal length [cm]', 'petal width [cm]']
df.head(5)
萼片长度 [cm] 萼片宽度 [cm] 花瓣长度 [cm] 花瓣宽度 [cm]
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2
import matplotlib.pyplot as plt
from mlxtend.plotting import scatter_hist


fig = scatter_hist(df["sepal length [cm]"], df["sepal width [cm]"])

png

示例 2 - 从 NumPy 数组创建类别散点图

from mlxtend.data import iris_data
from mlxtend.plotting import scatter_hist
import pandas as pd


X, y = iris_data()
X[:5]
array([[5.1, 3.5, 1.4, 0.2],
       [4.9, 3. , 1.4, 0.2],
       [4.7, 3.2, 1.3, 0.2],
       [4.6, 3.1, 1.5, 0.2],
       [5. , 3.6, 1.4, 0.2]])
fig = scatter_hist(X[:, 0], X[:, 1])

png

API

scatter_hist(x, y, xlabel=None, ylabel=None, figsize=(5, 5))

沿坐标轴的散点图和单个特征直方图。

参数

  • x : 1D 数组类对象或 Pandas Series

    X 轴值。

  • y : 1D 数组类对象或 Pandas Series

    Y 轴值。

  • xlabel : str (默认值: None)

    X 轴值的标签。如果 x 是 pandas Series,且 xlabelNone,则标签会自动推断。

  • ylabel : str (默认值: None)

    X 轴值的标签。如果 y 是 pandas Series,且 ylabelNone,则标签会自动推断。

  • figsize : tuple (默认值: (5, 5))

    Matplotlib 图形大小。

返回值

  • plot : Matplotlib Figure 对象