numpy 如何使用matplotlib为这段代码创建条形图

cygmwpex  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(105)

我目前在一个项目与一些朋友,试图制定出一些胜率的LOL队。我想根据每个团队的胜率制作一个条形图,但我经常遇到“TypeError:只有大小为1的数组可以转换为Python标量”,我似乎不明白我错在哪里。
我的问题是,如何将其转换成matplotlib条形图?我刚刚开始学习Python和编程,所以我知道我可能在这里犯了很多错误。
我尝试了以下方法:

import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = 10,4

# Include EU team when defined (between JDG and C9)
teams = ['GenG', 'T1', 'JD Gaming', 'Cloud9', 'Bilibili Gaming', 'G2 Esports', 'Golden Guardians', 'PSG Talon', 'GAM Esports', 'Detonation FocusMe', 'LOUD', 'Movistar R7']
teams_dict = {'GenG':0, 'T1':1, 'JD Gaming':2, 'EU(TBD)':3, 'Cloud9':4, 'Bilibili Gaming': 5, 'G2 Esports': 6, 'Golden Guardians':7, 'PSG Talon':8, 'GAM Esports':9, 'Detonation FocusMe':10, 'LOUD':11, 'Movistar R7':12}

# Winrates
GenG_WR = [0.70]
T1_WR = [0.75]
JDG_WR = [0.77]
# EU_WR = TBD
C9_WR = [0.80]
BLG_WR = [0.59]
G2_WR = [0.67]
GG_WR = [0.54]
PSG_WR = [0.77]
GAM_WR = [0.76]
DFM_WR = [0.82]
LLL_WR = [0.72]
MR7_WR = [0.58]
# Winrates Array (include EU team when defined)
Winrates = np.array([GenG_WR, T1_WR, JDG_WR, C9_WR, BLG_WR, G2_WR, GG_WR, PSG_WR, GAM_WR, DFM_WR, LLL_WR, MR7_WR])

plt.bar(teams, Winrates)
plt.xticks(len(teams), teams)
plt.ylabel('Winrate %')
plt.title('Winrates for the Split')

plt.show()

导致了我上面提到的TypeError。

sxpgvts3

sxpgvts31#

plt中使用的索引。xticks()函数不正确。应该是plt。xticks(np.arange(len(teams)),teams)在每个整数位置设置刻度。##EU战队胜率未定义,JD Gaming和Cloud9之间缺少一个战队。## www.example.com ()函数期望每个团队都有一个值数组,但Winrates数组是一个2D数组,每个团队只有一个值。

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['figure.figsize'] = 10,4

# Include EU team when defined (between JDG and C9)
teams = ['GenG', 'T1', 'JD Gaming', 'EU(TBD)', 'Cloud9', 'Bilibili Gaming', 'G2 Esports', 'Golden Guardians', 'PSG Talon', 'GAM Esports', 'Detonation FocusMe', 'LOUD', 'Movistar R7']

# Winrates
GenG_WR = [0.70]
T1_WR = [0.75]
JDG_WR = [0.77]
EU_WR = [] # empty list for now, to be filled when EU team is defined
C9_WR = [0.80]
BLG_WR = [0.59]
G2_WR = [0.67]
GG_WR = [0.54]
PSG_WR = [0.77]
GAM_WR = [0.76]
DFM_WR = [0.82]
LLL_WR = [0.72]
MR7_WR = [0.58]

# Winrates Array (include EU team when defined)
Winrates = [GenG_WR, T1_WR, JDG_WR, EU_WR, C9_WR, BLG_WR, G2_WR, GG_WR, PSG_WR, GAM_WR, DFM_WR, LLL_WR, MR7_WR]
Winrates = np.array([np.array(team_wr, dtype=np.float64) if team_wr else np.nan for team_wr in Winrates])

plt.bar(teams, Winrates.flatten())
plt.xticks(np.arange(len(teams)), teams)
plt.ylabel('Winrate %')
plt.title('Winrates for the Split')

plt.show()

相关问题