可视化模拟闯红灯现象柱状图
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = 'simhei'
plt.rcParams['axes.unicode_minus']=False
boy = [450,800,200]
girl = [150,100,300]
x1 = list(range(len(boy)))
x2 = [i+0.35 for i in x1]
plt.bar(x1,boy,alpha=0.7,width=0.3,color='blue',label='男性')
plt.bar(x2,girl,alpha=0.7,width=0.3,color='green',label='女性')
x_tick = ['从不闯红灯','跟从别人闯红灯','带头闯红灯']
plt.xticks([i+0.2 for i in x1],x_tick)
plt.legend()
plt.show()
效果图如下: