官网
plt.plot([x], y, [fmt], *, data=None, **kwargs)
plt.plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
x, y: array-like or scalar(标量)
x可选,默认是
range(len(y))
;y可以是二维数组,此时会在同一个坐标系里出现多条折线;
fmt:str,optional
控制线条格式的参数,比如线条的颜色、形状等。
fmt = '[marker][line][color]'
常见的marker参数:
详见 matplotlib.markers
character description
'.' point marker
',' pixel marker
'o' circle marker
'v' triangle_down marker
'^' triangle_up marker
'<' triangle_left marker
'>' triangle_right marker
'1' tri_down marker
'2' tri_up marker
'3' tri_left marker
'4' tri_right marker
'8' octagon marker
's' square marker
'p' pentagon marker
'P' plus (filled) marker
'*' star marker
'h' hexagon1 marker
'H' hexagon2 marker
'+' plus marker
'x' x marker
'X' x (filled) marker
'D' diamond marker
'd' thin_diamond marker
'|' vline marker
'_' hline marker
常见的line参数:
character description
'-' solid line style
'--' dashed line style
'-.' dash-dot line style
':' dotted line style
常见的Colors参数:
character color
'b' blue
'g' green
'r' red
'c' cyan
'm' magenta
'y' yellow
'k' black
'w' white
**kwargsLine2D: properties, optional
也能通过这类参数来控制线条。
比如marker
控制形状,c
控制颜色
不传入x参数:
plt.plot([1, 4, 9, 16], marker='o', c='r')
plt.show()
x1 = [1,2,3]
y1=[1,3,5]
x2=[1,2,3]
y2=[2,4,6]
plt.plot(x1, y1, 'g^', x2, y2, 'g-') # 此时不能通过marker,c来控制线条的形状和颜色
官网
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: float or array-like, shape (n, )
# 数据点的位置
s: float or array-like, shape (n, ), optional
# 数据点的大小
c: array-like or list of colors or color, optional
# 数据点的颜色
marker: MarkerStyle, default: rcParams["scatter.marker"] (default: 'o')
# 数据点的形状
alpha: float, default: None
# 数据点的透明程度,0表示完全透明,1表示不透明
linewidths: float or array-like, default: rcParams["lines.linewidth"] (default: 1.5)
# 数据点的线宽
edgecolors: {'face', 'none', None} or color or sequence of color, default: rcParams["scatter.edgecolors"] (default: 'face')
# 数据点的线的颜色
设置大小、颜色和透明度
# 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 # 0 to 15 point radii
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()
x = [1, 2, 3]
y = [1, 4, 9]
plt.scatter(x, y, c='r', alpha=0.5, linewidths=15, edgecolors=None)