要点:
条形图:plt.bar(x,y)
横向条形图:plt.barh(x,y)
例题:
假设a电影在2017-09-14(b_14),2017-09-15(b_15),2017-09-16(b_16)三天的票房,为了展示本电影的票房和其他同期电影的票房数据,请直观呈现数据
a=["猿球崛起3:终极之战","敦刻尔克","蜘蛛侠:英雄归来","战狼2"]
b_14=[15746,312,4497,319]
b_15=[12357,156,2045,168]
b_16=[2358,399,2358,362]
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font=font_manager.FontProperties(fname="C:/Windows/Fonts/STXIHEI.TTF")
a=["猿球崛起3:终极之战","敦刻尔克","蜘蛛侠:英雄归来","战狼2"]
b_14=[15746,312,4497,319]
b_15=[12357,156,2045,168]
b_16=[2358,399,2358,362]
bar_width=0.2
x_14=list(range(len(a)))#结果[0,1,2,3]
x_15=[i+bar_width for i in x_14]
x_16=[i+bar_width*2 for i in x_14]
plt.bar(x_14,b_14,width=bar_width,label="2017-9-14",color="#E6E6FA")
plt.bar(x_15,b_15,width=bar_width,label="2017-9-15",color="#FFF0F5")
plt.bar(x_16,b_16,width=bar_width,label="2017-9-16",color="#FFE4E1")
#设置x轴的刻度
plt.xticks(x_15,a,fontproperties=my_font)
plt.legend(prop=my_font)
plt.xlabel("电影名",fontproperties=my_font)
plt.ylabel("票房数量",fontproperties=my_font)
plt.title("电影票房详情",fontproperties=my_font)
plt.show()
|