我正在嘗試在 python 中繪制一個圖表,該圖表將向我展示隨著時間的推移出現的專案。所以我想找出每年有多少符合兩個類別的專案出現,并根據這些專案繪制圖表。
這是我在excel中的資料:

我最終想要的是一份既是奇幻又是動作的電影的清單,以及它們每年出現的次數。這是我得到的最終結果(這是正確的)

即2004年有兩部電影既是奇幻又是動作,2005年有一部電影既是奇幻又是動作等。
以下是我為獲得結果而采取的步驟:
#import data:
data = pd.read_csv("data.csv")
#put all fantasy movies in a list:
fantasy_movies = data[['Name', 'Genre']][(data['Genre'] == 'Fantasy')]
fantasy_movies.rename(columns={'Genre' : 'Fantasy'}, inplace = True)
#put all action movies in a list:
action_movies = data[['Name', 'Genre']][(data['Genre'] == 'Action')]
action_movies.rename(columns={'Genre' : 'Action'}, inplace = True)
#merge the two datasets:
action_fantasy = pd.merge(fantasy_movies, action_movies)
#obtain a list of unique movie names:
unique = action_fantasy.Name.unique()
#make dates the column and unique names the rows
filter_data = data[(data.Name.isin(unique))]
table = filter_data.pivot_table(filter_data, index = ['Name'],columns=['year'])
#replace all NaNs with zero
table1 = table.fillna(0)
#Count items in years
table1.gt(0).astype(int).sum(axis=0)
現在,從這里開始,我想使用 Matplotlib 制作某種圖表(我正在考慮條形圖),該圖表的底部有幾年,并根據 table1 結果增加數量。我正在努力創建一個,盡管它在技術上應該像將資料放在 x 列和資料放在 y 列一樣簡單。
就像來自 W3 Schools 的代碼:
matplotlib 條形圖
如果您真的想使用
seaborn 條形圖
或者將 DataFrame 傳遞到
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/447246.html
標籤:Python 熊猫 数据框 matplotlib 条形图
上一篇:繪圖:x必須是標簽或位置
下一篇:如何用繪圖更新散點圖?
