科研绘图 - Python - 柱状图代码及展示

1

python 复制代码
import pandas as pd
import matplotlib.pyplot as plt

students = pd.read_excel('./Students.xlsx')

print('----原始数据----')
print(students)

students.sort_values(by='2017',inplace=True,ascending=False)
students.plot.bar(x='Field',y=['2016','2017'],color=['orange','red'])

plt.title('International Students by Field',fontsize=16,fontweight='bold')
plt.xlabel('Field',fontweight='bold')
plt.ylabel('Numbers',fontweight='bold')
ax = plt.gca() # 获取图表的轴
ax.set_xticklabels(students['Field'],rotation=45,ha='right')
f = plt.gcf() # 获取图表的图形
f.subplots_adjust(left=0.2,bottom=0.42)
# plt.tight_layout()
plt.show()

2

python 复制代码
import pandas as pd  
import matplotlib.pyplot as plt  
  
users = pd.read_excel('./Users.xlsx',index_col='ID')  
users['Total'] = users['Oct'] + users['Nov'] + users['Dec']  
users.sort_values(by='Total',inplace=True,ascending=True)  
print(users)  
  
# stacked: 叠加(默认为False)  
users.plot.barh(x='Name',y=['Oct','Nov','Dec'],stacked=True,title='User Behavior')  
  
plt.tight_layout()  
plt.show()  
  
# 补充说明  
# users.plot.bar(...) 表示制作竖直柱状图  
# users.plot.barh(...) 表示制作水平柱状图

3


资源部分原创部分整理自网络,仅供分享

相关推荐
copyer_xyf9 小时前
Agent 流程编排
后端·python·agent
copyer_xyf10 小时前
Agent RAG
后端·python·agent
copyer_xyf10 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf10 小时前
Agent 记忆管理
后端·python·agent
星云穿梭1 天前
用Python写一个带图形界面的学生管理系统——完整教程
python
金銀銅鐵1 天前
用 Pygame 实现 15 puzzle
python·数学·游戏
黄忠1 天前
大模型之LangGraph技术体系
python·llm
hboot2 天前
AI工程师第二课 - 数据处理
人工智能·python·数据分析
用户8356290780512 天前
使用 Python 自动化 PowerPoint 形状布局与格式设置
后端·python
用户8356290780512 天前
用 Python 自动化 PowerPoint 演讲者备注添加
后端·python