# 1.导入表格,设置各轴的值
a = pd.read_excel("data/电影票房.xls")
name = a["movie name"].values
y1 = a["box office"].values
y2 = a["price"].values
y3 = a["person times"].values
# 2.设置双坐标,即将若干个bars合并在一起
fig,ax1=plt.subplots()
x = np.arange(len(name))
# 3.左边坐标及绘制柱状图
ax1.set_ylabel('box office')
ax1.set_ylim(0,80000)
a = ax1.bar(x-0.2,y1,width=0.2,color="#fc8251",label="box office")
# 4.右边坐标轴及绘制柱状图
ax2 = ax1.twinx()
ax2.set_ylim(0,50)
ax2.set_ylabel('price & person times')
b = ax2.bar(x,y2,width=0.2,color="#5470c6",label="price")
c = ax2.bar(x+0.2,y3,width=0.2,color="#f9c956",label="person times")
ax1.set_xticks(x)
ax1.set_xticklabels(name)
plt.rcParams["figure.figsize"] = (10.0,6.0)
# 5.双坐标轴的图注!!!
g = [a,b,c]
f = [l.get_label() for l in g]
plt.legend(g,f)
plt.show()
表格:
柱状图: