ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

matplotlib柱状图与标准线

2021-06-09 22:00:16  阅读:225  来源: 互联网

标签:plt bar 标准线 matplotlib label width 柱状图 0.005 center


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有