1、散点图
n = 1024
X = np.random.normal(0,1,n)
Y = np.random.normal(0,1,n)
# 颜色
T = np.arctan2(Y,X)
# c = T,颜色;对应的colormap,T中某一个值对应到colormap中某一个颜色,采用默认值,透明度0.5
plt.scatter(X,Y, s = 75, c = T,alpha = 0.5)
plt.xlim((-1,1))
plt.ylim((-1,1))
# 隐藏所有的ticks
plt.xticks(())
plt.yticks(())
plt.show()
2、柱状图
# 生成12个柱状图
n = 12
X = np.arange(n)
Y1 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
Y2 = (1 - x / float(n)) * np.random.uniform(0.5, 1.0, n)
plt.bar(X, +Y1, facecolor = '#9999ff', edgecolor = 'white')
plt.bar(X, -Y2, facecolor = '#ff9999', edgecolor = 'white')
# 数据X,Y1打包,可以一次性输出两个数字
for x,y in zip(X, Y1):
# ha: horizontal alignment
plt.text(x + 0.4, y + 0.05, '%.2f'% y, ha = 'center', va = 'bottom')
for x,y in zip(X,Y2):
plt.text(x + 0.4, -y - 0.05, '-%.2f'%y, ha = 'center', va = 'top')
plt.xlim(-.5,n)
plt.ticks(())
plt.ylim(-1.25, 1.25)
plt.yticks(())
plt.show()
3.等高线
# 给定一个x,y坐标,生成对应的高度值
def z(x,y):
return (1 - x / 2 + x**5 + y**3) * np.exp(-x**2 - y**2)
# 把x和y绑定成网格的输入值
n = 256
X = np.linespace(-3, 3, n)
Y = np.linespace(-3, 3, n)
X,Y = np.meshgrid(x,y)
# 绘制出具有颜色的等高线,8即把等高线图分为10部分
plt.contourf(X,Y,z(X,Y), 8, alpha = 0.75, cmap = plt.cm.hot)
# 绘制等高线图中的线
C = plt.contour(X,Y,z(x,y), 8, colors = 'black', linewidth = .5)
plt.clabel(C,inline = True, fontsize = 10)
plt.xticks(())
plt.yticks(())
plt,show()
4、显示图片
# cmap = 'bone'两种方式,点的方式引用也可以
plt.imshow(a, interpolation = 'nearest', cmap = 'bone', origin = 'upper')
# 坐标,压缩坐标高度
plt.colorbar(shrink = 0.9)
5、3D数据额外添加包:
from mpl_toolkits.mplot3d import Axes3D
# 绘制3D图
fig = plt.figure()
ax = Axes3D(fig)
# 第一步mesh
X,Y = np.meshgrid(X,Y)
R = np.sqrt(X**2 + Y**2)
z = np.sin(R)
# rstride,cstride行跨和列跨,offset从坐标0点压倒坐标轴的那个位置(比零点低几个坐标轴点)
ax.plot_surface(X,Y,z,rstride = 1, cstride = 1, cmap = plt.get_cmap = ('rainbow'))
# zdir等高线从z轴压下去
ax.contoursf(X,Y,z,zdir = 'z', offset = -2, cmap = 'rainbow')
ax.set_zlim(-2,2)
plt,show()