我正在嘗試查看我所做的是否正確,但不太確定如何做到這一點。
我有這個字典:
k = {'Boston': [{'name': 'Jayson Tatum','rebounds': 8.0, 'assists': 4.4, 'points': 26.9}, {'name': 'Jaylen Brown','rebounds': 6.1, 'assists': 3.5, 'points': 23.6}, {'name': 'Marcus Smart', 'rebounds': 3.8, 'assists': 5.9, 'points': 12.1}], 'Phoenix': [{'name': 'Devin Booker', 'rebounds': 5.0, 'assists': 4.8, 'points': 26.8}, {'name': 'Deandre Ayton', 'rebounds': 10.2, 'assists': 1.4, 'points': 17.2}, {'name': 'Chris Paul', 'rebounds': 4.4, 'assists': 0.0, 'points': 14.7}], 'Philadelphia': [{'name': 'Tyrese Maxey', 'rebounds': 3.2, 'assists': 4.3, 'points': 17.5}, {'name': 'Tobias Harris', 'rebounds': 6.8, 'assists': 3.5, 'points': 17.2}, {'name': 'Georges Niang','rebounds': 2.7, 'assists': 1.3, 'points': 9.2}], 'Milwaukee': [{'name': 'Giannis Antetokounmpo', 'rebounds': 11.6, 'assists': 5.8, 'points': 29.9}, {'name': 'Khris Middleton','rebounds': 5.4, 'assists': 5.4, 'points': 20.1}, {'name': 'Jrue Holiday','rebounds': 4.5, 'assists': 6.8, 'points': 18.3}], 'Golden State': [{'name': 'Stephen Curry', 'rebounds': 5.2, 'assists': 6.3, 'points': 25.5}, {'name': 'Klay Thompson', 'rebounds': 3.9, 'assists': 2.8, 'points': 20.4}, {'name': 'Jordan Poole', 'rebounds': 3.4, 'assists': 4.0, 'points': 18.5}], 'Miami': [{'name': 'Jimmy Butler', 'rebounds': 5.9, 'assists': 5.5, 'points': 21.4}, {'name': 'Tyler Herro', 'rebounds': 5.0, 'assists': 4.0, 'points': 20.7}, {'name': 'Bam Adebayo', 'rebounds': 10.1, 'assists': 3.4, 'points': 19.1}], 'Dallas': [{'name': 'Luka Don?i?''rebounds': 9.1, 'assists': 8.7, 'points': 28.4}, {'name': 'Jalen Brunson', rebounds': 3.9, 'assists': 4.8, 'points': 16.3}, {'name': 'Tim Hardaway Jr.', 'rebounds': 3.7, 'assists': 2.2, 'points': 14.2}], 'Memphis': [{'name': 'Ja Morant', 'rebounds': 5.7, 'assists': 6.7, 'points': 27.4}, {'name': 'Dillon Brooks','rebounds': 3.2, 'assists': 2.8, 'points': 18.4}, {'name': 'Desmond Bane', 'rebounds': 4.4, 'assists': 2.7, 'points': 18.2}]}
這個字典有一個團隊作為鍵,一個串列作為值,這個串列包含三個字典,每個玩家一個。
我想知道的是如何繪制這個,我想在列印籃板、助攻或得分之間進行選擇,我如何指定要繪制哪一個?
我試圖將 k 變成一個資料框,但這些行仍然是一個 dicts 串列,我不知道如何進行繪圖。
我也嘗試過這樣的事情:
for team in k:
players = top_players_in_teams[team]
names = list(players.keys())
values = [value[wantedType] for value in list(players.values())]
team_bars = ax.bar(names, values, label=team, zorder=3)
team_bars.set_label(team)
ax.set_title("SOMETHING")
ax.legend(title="TeaM", bbox_to_anchor=(1.05, 1))
plt.grid(b=True, which="major", axis="y", zorder=0)
plt.xticks(rotation=90)
uj5u.com熱心網友回復:
您可以先將資料轉換為更結構化的資料框,然后根據需要進行繪圖。
import pandas as pd
import matplotlib.pyplot as plt
mydict = {}
i = 0
for team in k:
for player in k[team]:
mydict[i] = [team, player["name"], player["rebounds"], player["assists"], player["points"]]
i = 1
df = pd.DataFrame.from_dict(mydict, orient="index", columns=["team", "name", "rebounds", "assists", "points"])
print(df)
產生:
team name rebounds assists points
0 Boston Jayson Tatum 8.0 4.4 26.9
1 Boston Jaylen Brown 6.1 3.5 23.6
2 Boston Marcus Smart 3.8 5.9 12.1
3 Phoenix Devin Booker 5.0 4.8 26.8
4 Phoenix Deandre Ayton 10.2 1.4 17.2
5 Phoenix Chris Paul 4.4 0.0 14.7
6 Philadelphia Tyrese Maxey 3.2 4.3 17.5
7 Philadelphia Tobias Harris 6.8 3.5 17.2
8 Philadelphia Georges Niang 2.7 1.3 9.2
9 Milwaukee Giannis Antetokounmpo 11.6 5.8 29.9
10 Milwaukee Khris Middleton 5.4 5.4 20.1
11 Milwaukee Jrue Holiday 4.5 6.8 18.3
12 Golden State Stephen Curry 5.2 6.3 25.5
13 Golden State Klay Thompson 3.9 2.8 20.4
14 Golden State Jordan Poole 3.4 4.0 18.5
15 Miami Jimmy Butler 5.9 5.5 21.4
16 Miami Tyler Herro 5.0 4.0 20.7
17 Miami Bam Adebayo 10.1 3.4 19.1
18 Dallas Luka Don?i? 9.1 8.7 28.4
19 Dallas Jalen Brunson 3.9 4.8 16.3
20 Dallas Tim Hardaway Jr. 3.7 2.2 14.2
21 Memphis Ja Morant 5.7 6.7 27.4
22 Memphis Dillon Brooks 3.2 2.8 18.4
23 Memphis Desmond Bane 4.4 2.7 18.2
作為示例圖:
df.plot(x='name', y='rebounds', kind='bar')
plt.show()
uj5u.com熱心網友回復:
您可以使用字典鍵和串列理解的組合提取每個團隊的所有元素,例如籃板:
rebounds = [y['rebounds'] for team in mydict for y in mydict[team]]
這將為每支球隊的每位球員提供籃板。
編輯:我測驗的完整代碼:
mydict = {'Boston':[{'name':'a', 'rebounds':2.0}, {'name':'c', 'rebounds':2.1}], 'Cleveland':[{'name':'b', 'rebounds':3.3}, {'name':'d', 'rebounds':0.9}]}
thing = [y['rebounds'] for team in mydict for y in mydict[team]]
thing
結果:
[2.0, 2.1, 3.3, 0.9]
更新:您可以使用更多字典鍵和串列理解按團隊進行顏色編碼。
names = [y['name'] for team in mydict for y in mydict[team]]
colors = {'Boston':'red','Cleveland':'blue'}
colorlist = [colors[team] for team in mydict for i in range(len(mydict[team]))]
import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(5,5))
ax.bar(x=names,height=thing,color=colorlist)
plt.show()
更新 2:讓我們添加一個團隊名稱與顏色匹配的圖例,為什么不呢。
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(5,5))
ax.bar(x=names,height=thing,color=colorlist)
labels = list(colors.keys())
handles = [plt.Rectangle((0,0),1,1, color=colors[label]) for label in labels]
plt.legend(handles, labels)
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/524020.html
上一篇:將緯度和經度轉換為浮點數
下一篇:如何使用元組串列過濾資料框
