matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, *, edgecolors=None, plotnonfinite=False, data=None, **kwargs)
屬性 | 參數 | 意義 |
---|---|---|
座標 | x,y | 輸入點列的數組,長度都是size |
點大小 | s | 點的直徑數組,默認直徑20,長度最大size |
點顏色 | c | 點的顏色,默認藍色 'b',也可以是個 RGB 或 RGBA 二維行數組。 |
點形狀 | marker | MarkerStyle 點的樣式,默認小圓圈 'o'。 |
調色板 | cmap | Colormap,默認 None,標量或者是一個 colormap 的名字,只有 c 是一個浮點數數組時才使用。如果沒有申明就是 image.cmap。 |
亮度(1) | norm | Normalize,默認 None,數據亮度在 0-1 之間,只有 c 是一個浮點數的數組的時才使用。 |
亮度(2) | vmin,vmax | 亮度設置,在 norm 參數存在時會忽略。 |
透明度 | alpha | 透明度設置,0-1 之間,默認 None,即不透明 |
線 | linewidths | 標記點的長度 |
顏色 | edgecolors | 顏色或顏色序列,默認爲 'face',可選值有 'face', 'none', None。 |
plotnonfinite | 布爾值,設置是否使用非限定的 c ( inf, -inf 或 nan) 繪製點。 | |
**kwargs | 其他參數。 |
MarkerStyle
marker | description | 描述 |
---|---|---|
"." |
point | 點 |
"," |
pixel | 像素 |
"o" |
circle | 圓 |
"v" |
triangle_down | 倒三角 |
"^" |
triangle_up | 正三角 |
"<" |
triangle_left | 左三角 |
">" |
triangle_right | 右三角 |
"1" |
tri_down | |
"2" |
tri_up | |
"3" |
tri_left | |
"4" |
tri_right | |
"8" |
octagon | 八角形 |
"s" |
square | 正方形 |
"p" |
pentagon | 五角 |
"P" |
plus (filled) | |
"*" |
star | 星星 |
"h" |
hexagon1 | |
"H" |
hexagon2 | |
"+" |
plus | +號 |
"x" |
x | X 號 |
"X" |
x (filled) | |
"D" |
diamond | |
"d" |
thin_diamond | |
``" | "`` | vline |
"_" |
hline | |
0 (TICKLEFT ) |
tickleft | |
1 (TICKRIGHT ) |
tickright | |
2 (TICKUP ) |
tickup | |
3 (TICKDOWN ) |
tickdown | |
4 (CARETLEFT ) |
caretleft | |
5 (CARETRIGHT ) |
caretright | |
6 (CARETUP ) |
caretup | |
7 (CARETDOWN ) |
caretdown | |
8 (CARETLEFTBASE ) |
caretleft (centered at base) | |
9 (CARETRIGHTBASE ) |
caretright (centered at base) | |
10 (CARETUPBASE ) |
caretup (centered at base) | |
11 (CARETDOWNBASE ) |
caretdown (centered at base) | |
"None" , " " or "" |
nothing | |
'$...$' |
Render the string using mathtext. E.g "$f$" for marker showing theletter f . |
|
verts |
A list of (x, y) pairs used for Path vertices. The center of the marker is located at (0, 0) and the size is normalized, such that the created path is encapsulated inside the unit cell. |
|
path | A ~matplotlib.path.Path instance. |
|
(numsides, 0, angle) |
A regular polygon with numsides |
|
sides, rotated by angle . |
||
(numsides, 1, angle) |
A star-like symbol with numsides |
|
sides, rotated by angle . |
||
(numsides, 2, angle) |
An asterisk with numsides sides, |
|
rotated by angle . |
示例
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(19680801)
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N) # 顏色可以隨機
area = (30 * np.random.rand(N)) ** 2 # 隨機大小
# x,y,s,c 的 size 需要一致
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()
多元高斯的情況
# 設置畫布大小
fig = plt.figure(figsize=(8, 6))
# Generating a Gaussion dataset:
# creating random vectors from the multivariate normal distribution
# given mean and covariance
mu_vec1 = np.array([0, 0])
cov_mat1 = np.array([[1, 0], [0, 1]])
X = np.random.multivariate_normal(mu_vec1, cov_mat1, 500)
R = X ** 2
R_sum = R.sum(axis=1)
plt.scatter(X[:, 0], X[:, 1], c='green', marker='o', s=32. * R_sum, edgecolor='black', alpha=0.5)
plt.show()
make_blobs
import numpy as np
from sklearn.datasets import make_blobs # 爲了快速方便的創建數據集,此處採用 scikit-learn 裏的 make_blobs
import matplotlib.pyplot as plt
# 創建一個數據集,X有兩個特徵,y={-1,1}
X, y = make_blobs(n_samples=500, centers=2, random_state=6)
y[y == 0] = -1
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap=plt.cm.Paired)
plt.xlabel("feature_1")
plt.ylabel("feature_2")
plt.show()
源碼地址:https://gitee.com/VipSoft/VipPython/matplotlib/pyplot_scatter.py
https://matplotlib.org/stable/gallery/index