欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

matplotlib绘制条形图

程序员文章站 2022-03-21 19:49:18
...
from matplotlib import pyplot as plt
import matplotlib

font = {'family': 'MicroSoft YaHei'
        }
matplotlib.rc("font",**font)

a = ["复仇者联盟","调音师","龙猫","海王","蜘蛛侠-平行世界"]
print(len(a))
b = [78.01,53.02,25.67,32.11,10.55]
plt.figure(figsize=(20,8),dpi=80)

#绘制横着的条形图
plt.barh(range(len(a)),b,height=0.3,color="orange")
plt.yticks(range(len(a)),a)
plt.grid(alpha=0.3)
# plt.bar(range(len(a)),b,width=0.3)
# plt.xticks(range(len(a)),a,rotation=90)
plt.savefig("./bar.png")
plt.show()

绘制多条条形图

from matplotlib import pyplot as plt
import matplotlib

font = {
    'family': 'MicroSoft YaHei',
}
matplotlib.rc("font",**font)
a = ["猩球崛起3","复仇者联盟3","终极之战","蜘蛛侠:英雄归来","战狼2"]
b_16 = [15746,112,4407,3190,200]
b_15 = [12357,150,2945,1600,345]
b_14 = [2350,400,2500,362,700]
x_14 = list(range(len(a)))
x_15 = [i + 0.2 for i in x_14]
x_16 = [i + 0.2*2 for i in x_14]

plt.figure(figsize=(20,8),dpi=80)

plt.bar(range(len(a)),b_14,width=0.3,label="9月14日")
plt.bar(x_15,b_15,width=0.2,label="9月15日")
plt.bar(x_16,b_16,width=0.2,label="9月16日")
plt.legend()
#设置xz轴刻度
plt.xticks(x_15,a)
plt.show()