fig, ax = plt.subplots(figsize=(10.8, 6.4)) width = 0.35 # the width of the bars h1 = ax.bar(range(len(x)), y, width)#画直方图 yrange = np.arange(0, 0.5, 0.1) ax.set_yticks(yrange) ax.set_xticks(range(len(x)))#x轴坐标个数 ax.set_xticklabels(x, rotation=45, fontsize='small')#x轴坐标标记 ax.set_title(str(off_days) + ' days') ax.set_ylabel('新闻数量占比')#坐标轴名称
ax2 = plt.twinx() #创建新的坐标轴与原坐标轴共用x轴 h2, = ax2.plot(range(len(x)), y_sequential, 'bo', linestyle='-', markersize=2, linewidth=1)#画线 ax2.set_yticks(np.arange(-1, 1, 0.2)) ax2.set_ylabel('同比') plt.legend(handles=[h1, h2], labels=['新闻数量占比', '新闻数量占比同比'], loc='upper right', fontsize=7)#标识
fig.savefig(fig_path)#保存图像