基本用法
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1,1,50)
y = 2 * x + 1
plt.plot(x,y)
plt.show()
Figure图像
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 50)
y1 = 2 * x + 1
y2 = x ** 2
plt.figure()
plt.plot(x, y1)
plt.figure(num=3, figsize=(8, 5))
plt.plot(x, y2)
plt.plot(x, y1,color = 'red',linewidth=1.0,linestyle='--')
plt.show()
坐标轴设置
plt.xlim((-1,2))
plt.ylim((-2,3))
plt.xlabel("X")
plt.ylabel("Y")
plt.xticks(np.linspace(-1,2,5))
plt.yticks([-2,0,2],[r'$good$',r'$midle\ score$',r'$bad$'])
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', -1))
ax.spines['left'].set_position(('data', 0))
Legend图例
l1, = plt.plot(x, y2)
l2, = plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')
plt.legend(handles=[l1,l2], labels=['up','down'], loc='best')
Annotation标注
x0 = 1
y0 = 2 * x0 + 1
plt.scatter(x0, y0, s=50, color='r')
plt.plot([x0, x0], [y0, 0], 'r--', lw=2)
plt.annotate(r'$2x+1=%s$' % y0, xy=(x0, y0), xycoords='data',
xytext=(+30, -30), textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=.2'))
plt.text(-3,3,r'$This\ is\ a\ test\ \sigma_t\ .$',fontdict={'size':16,'color':'r'})
Tick 能见度
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontsize(12)
label.set_bbox(dict(facecolor='red', edgecolor='None', alpha=0.5))
Scatter散点图
n = 1024
X = np.random.normal(0, 1, n)
Y = np.random.normal(0, 1, n)
T = np.arctan2(Y, X)
plt.scatter(X, Y, s=75, c=T, alpha=0.5)
plt.scatter(np.arange(5), np.arange(5), c='red')
plt.xlim((-1.5, 1.5))
plt.ylim((-1.5, 1.5))
plt.xticks(())
plt.yticks(())
Bar柱状图
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='blue', edgecolor='white')
plt.bar(X, -Y2, facecolor='pink', edgecolor='white')
for x, y in zip(X, Y1):
plt.text(x, y+0.05, '-%.2f' % y, ha='center', va='bottom')
for x, y in zip(X, Y2):
plt.text(x, -y-0.05, '%.2f' % y, ha='center', va='top')
Contours等高线图
def height(x, y):
return (1 - x/2 + x**5 + y**3) * np.exp(-x**2 - y**2)
n = 256
x = np.linspace(-3, 3, n)
y = np.linspace(-3, 3, n)
X, Y = np.meshgrid(x, y)
plt.contourf(X, Y, height(X, Y), 8, alpha=0.75, cmap=plt.cm.cool)
C = plt.contour(X, Y, height(X, Y), 8, color='black', linewidth=.5)
plt.clabel(C, inline=True, fontsize=10)
Image图片
a = np.array([0.313, 0.365, 0.423, 0.365, 0.439,
0.525, 0.423, 0.525, 0.651]).reshape(3, 3)
plt.imshow(a, interpolation='none', cmap='bone', origin='upper')
plt.colorbar(shrink=0.8)
3D 数据
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-4, 4, 0.5)
Y = np.arange(-4, 4, 0.5)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')
ax.contourf(X, Y, Z, zdir='x', offset=-4, cmap='rainbow')
ax.set_zlim(-2, 2)
Subplot 多合一
plt.figure()
plt.subplot(2, 1, 1)
plt.plot([-1, 1], [-1, 1])
plt.subplot(2, 3, 4)
plt.plot([-1, 1], [-1, 1])
plt.subplot(2, 3, 5)
plt.plot([-1, 1], [-1, 1])
plt.subplot(2, 3, 6)
plt.plot([-1, 1], [-1, 1])
Subplot 分格显示
plt.figure()
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3, rowspan=1)
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=1)
ax3 = plt.subplot2grid((3, 3), (1, 2), colspan=1, rowspan=1)
ax3 = plt.subplot2grid((3, 3), (2, 0), colspan=3, rowspan=1)
plt.figure()
gs = gridspec.GridSpec(3, 3)
ax1 = plt.subplot(gs[0, :])
ax2 = plt.subplot(gs[1, :2])
ax3 = plt.subplot(gs[1:, 2])
ax4 = plt.subplot(gs[-1, 0])
ax5 = plt.subplot(gs[-1, -2])
f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=True, sharey=True)
ax1.plot()
ax2.plot()
ax3.plot()
ax4.plot()
Animation动画
fig, ax = plt.subplots()
x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))
def animate(i):
line.set_ydata(np.sin(x+i/100))
return line,
def init():
line.set_ydata(np.sin(x))
return line,
ani = animation.FuncAnimation(
fig, func=animate, frames=100, init_func=init, interval=20, blit=True)
微信关注『方糖算法』
各类面试资料、内推资源,关注微信公众号获取哦。
|