Python plt 柱状图

import matplotlib.pyplot as plt
from matplotlib import rcParams


def autolabel(rects):
    for rect in rects:
        height = rect.get_height()
        plt.text(rect.get_x()+rect.get_width()/2. -
                 0.2, 1.03*height, '%s' % int(height))


name_list_en = ['Computer\n Science', 'Physics', 'Mathematics'	,
                'Statistics'	, 'Quantitative \n Biology', 'Quantitative\n Finance']


num_list = [8594, 6013, 5618, 5206, 587, 249]

# 设置字体类型
# 设置西文字体为新罗马字体
config = {
    "font.family": 'Times New Roman',
    # "font.size": 80,
    #     "mathtext.fontset":'stix',
}
rcParams.update(config)

# 去除边框
# 法1
# fig, ax = plt.subplots()
# ax.spines['top'].set_visible(False)
# ax.spines['right'].set_visible(False)
# 法2
plt.gca().spines['right'].set_color('none')
plt.gca().spines['top'].set_color('none')
plt.gca().spines['left'].set_color('none')
plt.gca().spines['bottom'].set_color('none')


# fig = plt.figure(figsize=(150, 6))    # 设置画布大小

plt.tick_params(axis='x', labelsize=10)    # 设置x轴标签大小

# plt.title('Statistics of tags in public English datasets') #设置标题

# 设置标签名
plt.xlabel('Labels')
plt.ylabel('The number of each label')

# autolabel 计数
autolabel(plt.bar(range(len(num_list)), num_list,
                  color='rgb', tick_label=name_list_en))

# 保存
# plt.savefig('./public.pdf')
plt.show()

  

posted on 2022-01-28 21:22  Caution_X  阅读(407)  评论(0编辑  收藏  举报

导航