先上结果:
基础语法:
Axes.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 float或 array 维度为(n, )
s float 或 array,维度为(n, ), 非必需
**c **array或list,映射颜色
可能的类型:
**marker **MarkerStyle
, 默认: rcParams["scatter.marker"]
(default: 'o'
) 是标记的形状,具体可看matplotlib官方文档
cmap str or Colormap
, default: rcParams["image.cmap"]
(default: 'viridis'
) 颜色条,可以用colormap,参考官方文档
normNormalize
, default: None
colors.Normalize
.缩放颜色的选项,一般不用
vmin, vmaxfloat, default: None
vmin 和 vmax 与默认规范一起使用,以将颜色数组 c 映射到颜色图 cmap。 如果没有,则使用颜色数组的相应最小值和最大值。
不能与norm同用。
alpha 透明度
0 (完全透明) and 1 (不透明).
linewidths float or array-like, default: rcParams["lines.linewidth"]
(default: 1.5
)线宽默认为1.5
edgecolors{‘face’, ‘none’, None} or color or sequence of color, default: rcParams["scatter.edgecolors"]
(default: 'face'
)
plotnonfinitebool, default: False
是否使用非有限 c 绘制点(即 inf
、-inf
或 nan
)。 如果为“真”,则使用 bad 颜色图颜色绘制点
# libraries
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
%matplotlib inline
# create data
x = np.random.rand(15) #x是15个0-1均匀分布
y = x+np.random.rand(15)
z = x+np.random.rand(15)
z=z*z
# Change color with c and transparency with alpha.
# I map the color to the X axis value.
plt.scatter(x, y, s=z*2000, c=x, cmap="Blues", alpha=0.4, edgecolors="grey", linewidth=2) # s是气泡随机的大小, c是不同类的颜色
# Add titles (main and on axis)
plt.xlabel("the X axis")
plt.ylabel("the Y axis")
plt.title("A colored bubble plot")
# Show the graph
plt.show()
# Load a numpy record array from yahoo csv data with fields date, open, close,
# volume, adj_close from the mpl-data/example directory. The record array
# stores the date as an np.datetime64 with a day unit ('D') in the date column.
price_data = (cbook.get_sample_data('goog.npz', np_load=True)['price_data']
.view(np.recarray))
price_data = price_data[-250:] # get the most recent 250 trading days
delta1 = np.diff(price_data.adj_close) / price_data.adj_close[:-1]
# Marker size in units of points^2
volume = (15 * price_data.volume[:-2] / price_data.volume[0])**2
close = 0.003 * price_data.close[:-2] / 0.003 * price_data.open[:-2]
# 数据准备,生成了delta1, close, volume三个列向量
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
fig, ax = plt.subplots()
ax.scatter(delta1[:-1], delta1[1:], c=close, s=volume, alpha=0.5)
ax.set_xlabel(r'$\Delta_i$', fontsize=15)
ax.set_ylabel(r'$\Delta_{i+1}$', fontsize=15)
ax.set_title('Volume and percent change')
ax.grid(True)
fig.tight_layout()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(19680801)
# Compute areas and colors
N = 150
r = 2 * np.random.rand(N)
theta = 2 * np.pi * np.random.rand(N)
area = 200 * r**2
colors = theta
fig = plt.figure()
ax = fig.add_subplot(projection='polar')
c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)
ig = plt.figure()
ax = fig.add_subplot(projection=‘polar’)
c = ax.scatter(theta, r, c=colors, s=area, cmap=‘hsv’, alpha=0.75)