为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 Matplotlib 中绘制 Excel 文件中的特定行?

如何在 Matplotlib 中绘制 Excel 文件中的特定行?

UYOU 2023-03-16 17:29:25
我有一个如下所示的 Excel 文件:我想在条形图上绘制 2020 年 1 月 1 日所有 3 个人的体重,以便使用 Matplotlib 在 Python 中进行直观比较。我怎样才能做到这一点?
查看完整描述

2 回答

?
临摹微笑

TA贡献1982条经验 获得超2个赞

这可能是用熊猫最简单的方法:


import pandas as pd

import datetime as dt



df = pd.read_excel('your_file_location', sheet_name='sheet_name', parse_dates=['Date'])

df = df.loc[df['Date'] == dt.date(year=2020, month=1, day=1)]

ax = df.plot.bar(df['Name'], df['Weight'])

在这里,我们首先从您的 excel 文件的特定工作表加载数据(sheet_name如果您的 excel 文件只有一个工作表,您可以省略参数),然后我们过滤数据以仅显示特定日期的记录,然后在 x 轴上绘制名称和 y 轴上的权重。


查看完整回答
反对 回复 2023-03-16
?
当年话下

TA贡献1890条经验 获得超9个赞

import pandas as pd

import matplotlib.pyplot as plt


df = pd.read_csv('C:\\Desktop\\file.csv', index_col = 'Date', 

parse_dates = True)  #importing data to python and making date column as 

index

df['year'] = df.index.year #extracting year from index

data_20 = df[df['year'] == 2020] # Filtering out 2020 date

ax = data_20.plot(kind='bar',x='Name',y='Weight') #Plotting by name for 2020

只为 2 人绘制:


ax = data_20[data_20['Name'] != 'John Smith']

.plot(kind='bar',x='Name',y='Weight') #Plotting by name for 2020

ax.set_ylabel('Weights in lbs') #Labeling y-axis

ax.set_xlabel('Names') #Labeling x-axis

ax.set_title('Weights for 2020') # Adding the title

为了让它漂亮,只需添加标签:


ax.set_ylabel('Weights in lbs') #Labeling y-axis

ax.set_xlabel('Names') #Labeling x-axis

ax.set_title('Weights for 2020'); # Adding the title


查看完整回答
反对 回复 2023-03-16
  • 2 回答
  • 0 关注
  • 146 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号