[Python] How to create a bar chart for this code using matplotlib

Discussão em 'Python' iniciado por Stack, Abril 24, 2025.

  1. Stack

    Stack Membro Participativo

    I'm currently in a project with some friends, trying to work out some winrates of LoL teams. I want to make a bar chart based on each teams' winrate, but I'm constantly running into "TypeError: only size-1 arrays can be converted to Python scalars", and I can't seem to understand where I'm getting it wrong.

    My question would be, how do I transform this into a matplotlib bar chart? I'm just getting started into Python and programming in general, so I understand I may have made a lot of mistakes here.

    I tried the following:

    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()


    Resulting in the TypeError I mentioned above.

    Continue reading...

Compartilhe esta Página