其他分享
首页 > 其他分享> > matplotlib柱状图与标准线

matplotlib柱状图与标准线

作者:互联网

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns

mpl.rcParams["font.sans-serif"] = ["SimHei"]
mpl.rcParams["axes.unicode_minus"] = False

y1=0.48
y2=0.46
y3=0.47
y4=0.45
y5=0.46
x1=0
x2=1
x3=2
x4=3
x5=4

bar_width = 0.25
tick_label = ['Comments rated 1', 'Comments rated 2', 'Comments rated 3', 'Comments rated 4']
#fig1 = plt.figure(facecolor='white',figsize=(10,6))
fig, ax = plt.subplots(facecolor='white',figsize=(16,9))
# 显示每个柱的具体高度

plt.text(x1 + 0.005, y1 + 0.005, '%.2f' % y1, ha='center', va='bottom')
plt.text(x2 + 0.005, y2 + 0.005, '%.2f' % y2, ha='center', va='bottom')
plt.text(x3 + 0.005, y3 + 0.005, '%.2f' % y3, ha='center', va='bottom')
plt.text(x4 + 0.005, y4 + 0.005, '%.2f' % y4, ha='center', va='bottom')
plt.text(x5 + 0.005, y5 + 0.005, '%.2f' % y5, ha='center', va='bottom')

# 绘制柱状图
plt.bar(x1, y1, bar_width, align="center", color="#60ACFC", label="The natural sciences", alpha=0.9)
plt.bar(x2, y2, bar_width, align="center", color="#32D3EB", label="Engineering and technology", alpha=0.9)
plt.bar(x3, y3, bar_width, align="center", color="#5BC49F", label="Life Sciences and Medicine", alpha=0.9)
plt.bar(x4, y4, bar_width, align="center", color="#FEB64D", label="Social Science and Management", alpha=0.9)
plt.bar(x5, y5, bar_width, align="center", color="#FF7C7C", label="Arts and Humanities", alpha=0.9)
ax.hlines(0.4, -1,5, linestyles='--', colors='#4472C4', label='Education Gini coefficient warning line')
plt.xticks((-1,0,1,2,3,4),
           ('','The natural sciences','Engineering and technology','Life Sciences and Medicine',
            'Social Science and Management','Arts and Humanities'), fontsize=10)#, rotation=90

plt.xlim((-1.1, 5.1))
my_x_ticks = np.arange(-1,5,1)
plt.xticks(my_x_ticks)
plt.ylim((0, 0.7))
plt.xlabel("Subject categories", fontsize=13)
plt.ylabel("Gini coefficient of education", fontsize=13)
plt.annotate(u"warning line", xy=(4.6, 0.4), xytext=(4.6, 0.5), arrowprops=dict(facecolor='red', shrink=0.1, width=2), fontsize=12)
# plt.title('Picture Name')
ax.spines['bottom'].set_linewidth(1)###设置底部坐标轴的粗细
ax.spines['left'].set_linewidth(1)####设置左边坐标轴的粗细
ax.spines['right'].set_linewidth(1)###设置右边坐标轴的粗细
ax.spines['top'].set_linewidth(1)###设置底部坐标轴的粗细

# 显示图例
plt.legend(fontsize=11)
plt.show()
fig.savefig('result1.jpg', dpi=400)


在这里插入图片描述
欢迎加群:620139909
在这里插入图片描述

标签:plt,bar,标准线,matplotlib,label,width,柱状图,0.005,center
来源: https://blog.csdn.net/kun_coder/article/details/117755139