亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 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 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號