import numpy as np
import matplotlib.pyplot as plt
v1 = 10
v2 = 14
x = np.array([-4,-3,-2,-1,0,1,2,3,4])
y = np.array([v1,v1,v1,0,0,0,v2,v2,v2])
plt.plot(x,y,'b')# 折线 1 x 2 y 3 color
plt.xticks(())
plt.xlabel('time(s)')
plt.ylabel('speed(m/s)')
# 移动坐标轴,spines为脊梁,即4个边框
ax = plt.gca() # gca stands for 'get current axis'
ax.spines['right'].set_color('none') # 设置右‘脊梁’为无色
ax.spines['top'].set_color('none') # 设置上‘脊梁’为无色
ax.xaxis.set_ticks_position('bottom') # 底部‘脊梁’设置为X轴
ax.spines['bottom'].set_position(('data', 0)) # 底部‘脊梁’移动位置,y的data
ax.yaxis.set_ticks_position('left') # 左部‘脊梁’设置为Y轴
ax.spines['left'].set_position(('data', 0)) # 左部‘脊梁’移动位置,x的data
#
plt.scatter([-3], [v1], 40, 'r')
plt.scatter([3], [v2], 40, 'r')
#
plt.plot([-3, -3], [0, v1], 'r--')
plt.plot([3, 3], [0, v2], 'r--')
#
plt.annotate('t1', xy=(-3, 0), xytext=(-3.1, -1))
plt.annotate('t2', xy=(3, 0), xytext=(2.9, -1))
plt.annotate('v1', xy=(-3, v1), xycoords='data',
xytext=(+10, +30), textcoords='offset points',
fontsize=12, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.annotate('v2', xy=(3, v2), xycoords='data',
xytext=(+10, +30), textcoords='offset points',
fontsize=12, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.show()