scatter_hist: 创建散点直方图绘图
一个快速生成散点直方图的函数。
从mlxtend.plotting导入散点直方图
概述
参考文献
- https://matplotlib.org/gallery/lines_bars_and_markers/scatter_hist.html
示例 1 - 来自 Pandas DataFrame 的散点图和直方图
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)
sepal length [cm] | sepal width [cm] | petal length [cm] | petal width [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]"])
示例 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])
API
scatter_hist(x, y, xlabel=None, ylabel=None, figsize=(5, 5))
Scatter plot and individual feature histograms along axes.
Parameters
-
x
: 1D array-like or Pandas SeriesX-axis values.
-
y
: 1D array-like or Pandas SeriesY-axis values.
-
xlabel
: str (default:None
)Label for the X-axis values. If
x
is a pandas Series, andxlabel
isNone
, the label is inferred automatically. -
ylabel
: str (default:None
)Label for the X-axis values. If
y
is a pandas Series, andylabel
isNone
, the label is inferred automatically. -
figsize
: tuple (default:(5, 5)
)Matplotlib figure size.
Returns
plot
: Matplotlib Figure object