import matplotlib.pyplot as plt
import numpy as np
# 这两行代码解决 plt 中文显示的问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 输入统计数据
X_data = ('Seg1', 'Seg2', 'Seg3', 'Seg4', 'Seg5', 'Seg6', 'Seg7', 'Seg8', 'Seg9', 'Seg10')
StopNum_No_Control = [0.0, 1.02, 4.26, 10.1, 16.89, 23.26, 30.26, 37.12, 44.16, 55.81]
StopNum_Trans_17min = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.3, 2.04, 7.65]
StopNum_Ours_17min = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.12, 1.1, 6.62]
# 计算差距
Trans_Ours_distance = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.18, 0.94, 1.03]
NoControl_Trans_distance = [0.0, 1.02, 4.26, 10.1, 16.89, 23.26, 30.26, 36.82, 42.12, 48.16]
zhexian_No_Control = [0.0, 1.02, 4.26, 10.1, 16.89, 23.26, 30.26, 37.12, 44.16, 55.81]
zhexian_Trans_17min = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.3, 2.04, 7.65]
zhexian_Ours_17min = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.12, 1.1, 6.62]
bar_width = 0.3 # 条形宽度
# New_created:创建网格
plt.figure(figsize=(10,5))
plt.grid(linestyle = "--") #设置背景网格线为虚线
ax = plt.gca()
ax.spines['top'].set_visible(False) #去掉上边框
ax.spines['right'].set_visible(False) #去掉右边框
# 使用两次 bar 函数画出两组条形图
plt.bar(X_data, height=NoControl_Trans_distance, width=bar_width, color='#CB181B', ec='black', label='No Control', lw=.5, bottom=StopNum_Trans_17min)
plt.bar(X_data, height=Trans_Ours_distance, width=bar_width, color='lightblue', ec='black', label='RB VSL', lw=.5, bottom=StopNum_Ours_17min)
plt.bar(X_data, height=StopNum_Ours_17min, width=bar_width, color='#008B45', ec='black', label='Our VSL', lw=.5)
plt.plot(X_data,StopNum_Ours_17min,color="yellowgreen",linestyle="--")
plt.plot(X_data,StopNum_Trans_17min,color="yellowgreen",linestyle="--")
plt.plot(X_data,StopNum_No_Control,color="yellowgreen",linestyle="--")
for i, j in zip(X_data, StopNum_Ours_17min):
plt.text(i, j + 0.05, "%d" % j, ha="center", va="bottom")
for i, j in zip(X_data, zhexian_Trans_17min):
plt.text(i, j + 0.05, "%d" % j, ha="center", va="bottom")
for i, j in zip(X_data, zhexian_No_Control):
plt.text(i, j + 0.05, "%d" % j, ha="center", va="bottom")
# plt.legend() # 显示图例
plt.legend(loc='upper left')
plt.xticks(X_data,fontsize=15,fontweight='bold') # 让横坐标轴刻度显示 X_data 里的饮用水, index_first + bar_width/2 为横坐标轴刻度的位置
plt.ylabel('The number of vehicle stops',fontsize=15,fontweight='bold') # 纵坐标轴标题
plt.title('Mean NVS for each Segment at each time interval',fontsize=15,fontweight='bold') # 图形标题
plt.savefig('图2.Mean—NVS_柱状图堆叠.png') #建议保存为svg格式,再用inkscape转为矢量图emf后插入word中
plt.show()