import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator
from matplotlib import patheffects
plt.rcParams['font.sans-serif'] = ['SimHei'] #定义使其正常显示中文字体黑体
plt.rcParams['axes.unicode_minus'] = False #用来正常显示表示负号
plt.rcParams['xtick.direction'] = 'in'#将x周的刻度线方向设置向内
plt.rcParams['ytick.direction'] = 'in'#将y轴的刻度方向设置向内
y = [[139.], [139.], [129.], [146.], [144.], [137.], [130.], [116.], [131.], [128.],[141.],[115.],[133.],[122.],[120.],[118.],[107.],[102.],[85.]]
x = 1:0.05:10; scatter(x,cos(x));
plt.figure(figsize=(5,3))
plt.scatter(x, y, label = 'scatter',c='k') #画散点图
plt.xticks(range(2000,2019,2),rotation=30)
plt.xlabel('年份', fontsize=10)
plt.ylabel('事故数/起', fontsize=10)
x_major_locator = MultipleLocator(2)
y_major_locator = MultipleLocator(20)
ax = plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
ax.yaxis.set_major_locator(y_major_locator)
plt.xlim(2000, 2018)
plt.ylim(80, 160)
plt.tick_params(axis='both', which='major',labelsize=14)
plt.tight_layout() # 解决图片显示不全问题,一行代码搞定
plt.show()
把x坐标的范围设置为1999 到 2019