python plt 并列柱状图3个柱,双坐标轴,legend图注合并

# 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()

表格:

python plt 并列柱状图3个柱,双坐标轴,legend图注合并_第1张图片

 柱状图:

python plt 并列柱状图3个柱,双坐标轴,legend图注合并_第2张图片

 

你可能感兴趣的:(python实例练习,python,开发语言,matplotlib)