数据可视化之matplotlib
matplotlib是python专门用于绘图的库,其官方指导网站为https://matplotlib.org/,里面涉及多种绘图的示例以及指导。这里只介绍一部分个人用到的一些方法。包括subplot,colormap。
1.contour、pcolor、plot、scatter、imshow、matshow
以上各种方法都可以绘制三维数据。用法基本相同,具体参数看官方文档。
for i in range(16):
plt.subplot(4,4,i+1)
plt.pcolor(conv2[0,:,:,i])
fig, axes = plt.subplots(4, 4, figsize=(6, 6),
subplot_kw={'xticks': [], 'yticks': []})
fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05)
for i,ax in zip(range(16), axes.flat):
ax.imshow(conv2[0,:,:,i])
plt.show()
2.subplot 控制绘图间距
方法1:subplot(nrows, ncols, plot_number)
方法2:matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
import matplotlib.pyplot as plt
import numpy as np
methods = [None, 'none', 'nearest', 'bilinear', 'bicubic', 'spline16',
'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric',
'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc', 'lanczos']
# Fixing random state for reproducibility
np.random.seed(19680801)
grid = np.random.rand(4, 4)
fig, axes = plt.subplots(3, 6, figsize=(12, 6),
subplot_kw={'xticks': [], 'yticks': []})
fig.subplots_adjust(hspace=0.3, wspace=0.05)#控制间距
#fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05)
for ax, interp_method in zip(axes.flat, methods):
ax.imshow(grid, interpolation=interp_method, cmap='viridis')
ax.set_title(interp_method)
plt.show()
3.多个figure绘图叠加
fig, ax = plt.subplots()
cmap = mpl.colors.ListedColormap(['blue','red'])
c = plt.pcolor(z,cmap = cmap,vmin = 0,vmax = 30)
plt.axis('tight')
fig.colorbar(c,ticks = [15])
plt.scatter(xid,yid,color = 'green')
for i in range(len(xid)):
plt.text(xid[i]+10,yid[i]+10,str(cid[i]),ha = 'right')
plt.show()