科学计算少不了用matplotlib画图, 这里写一点心得笔记:
import matplotlib.pyplot as plt
fig = plt.figure()
# 2行1列
ax1 = plt.subplot(211) # 第一个子图
plt.imshow(np.random.random((100, 100)), cmap=plt.cm.BuPu_r)
ax2 = plt.subplot(212) # 第二个子图
plt.imshow(np.random.random((100, 100)), cmap=plt.cm.BuPu_r)
将数据显示为图像,即二维显示(W,H)。参数:
matplotlib.pyplot.imshow(
X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=None,
vmin=None, vmax=None, origin=None, extent=None, *, filternorm=True, filterrad=4.0,
resample=None, url=None, data=None, **kwargs)
X: array-like or PIL image
(M, N): an image with scalar data.
The values are mapped to colors using normalization and a colormap.
See parameters norm, cmap, vmin, vmax.
(M, N, 3): an image with RGB values (0-1 float or 0-255 int).
(M, N, 4): an image with RGBA values (0-1 float or 0-255 int), i.e. including transparency.
extent:floats (left, right, bottom, top)
The bounding box in data coordinates that the image will fill.
The image is stretched individually along x and y to fill the box.
即该图的坐标刻度(x1,x2,y1,y2)
origin{'upper', 'lower'}, default: rcParams["image.origin"] (default: 'upper')
图像的坐标起点,默认y轴起点在上方(从上到下),适用于图像。
如果在下方,x,y的起点在同一位置。
散点图
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)
c: array-like or list of colors or color
点的颜色, 如:'w'代表白色(white)
marker: MarkerStyle, default: rcParams["scatter.marker"] (default: 'o')
点的形状
edgecolors{'face', 'none', None} or color or sequence of color
点的边缘颜色,默认和点的颜色一致(face),或者黑边'k'
fig = plt.figure()
ax1 = fig.add_axes([0.1,0.1,0.8,0.8]) # 起点不是 (0,0), 为了左下角预留一定的边宽 (margin), 长度不是(1,1),为了给边缘预留0.2的margin.
ax2 = fig.add_axes([0.5,0.5,0.25,0.25]) # 起点是画布中心,长宽度为画布长宽的0.25