我正在嘗試在 matplotlib 上繪制一些值。這是我迄今為止所取得的成就。

問題是顏色條只顯示一些顏色。我如何為每個游戲條目推送不同的顏色?
if __name__ == '__main__':
# reading excel file
games_male_attendance = collections.OrderedDict()
games_female_attendance = collections.OrderedDict()
df = pd.read_excel("Olympic-dataset.xlsx", usecols=["Game_Discipline", "Male", "Female"])
for index, row in df.iterrows():
game_name = row["Game_Discipline"]
male_attendance = row["Male"]
female_attendance = row["Female"]
if game_name not in games_male_attendance:
games_male_attendance[game_name] = male_attendance
if game_name not in games_female_attendance:
games_female_attendance[game_name] = female_attendance
list_male_attendance = list(games_male_attendance.values())
list_female_attendance = list(games_female_attendance.values())
classes = list(games_male_attendance.keys())
# set colors and legends
colors = plt.cm.get_cmap('tab20c')
indexes = []
for i in range(len(classes)):
indexes.append(i)
scatter = plt.scatter(y=list_male_attendance, x=list_female_attendance, c=range(len(classes)), cmap=colors, vmin=0, vmax=len(classes))
cbar = plt.colorbar(scatter)
cbar.ax.get_yaxis().set_ticks(indexes, labels=classes)
plt.ylabel("male attendance")
plt.xlabel("female attendance")
plt.show()
資料:
print(list_male_attendance)
[131, 98, 265, 144, 168, 42, 193, 0, 144, 1072, 0, 178, 86, 201, 344, 65, 187, 99, 64, 108, 71, 87, 48, 175, 192, 16, 25, 418, 55, 41, 146, 20, 126, 24, 151, 144, 125, 97, 99, 60, 40, 36, 38, 32, 9, 20]
print(list_female_attendance)
[70, 98, 257, 144, 168, 40, 96, 96, 90, 969, 105, 178, 86, 192, 264, 65, 102, 98, 64, 107, 72, 86, 48, 175, 192, 16, 25, 361, 55, 41, 122, 20, 123, 24, 146, 144, 73, 94, 90, 60, 40, 36, 38, 32, 10, 20]
print(classes)
['Cycling Road', 'Artistic Gymnastics', 'Rowing', 'Basketball', 'Handball', 'Karate', 'Wrestling', 'Rhythmic Gymnastics', 'Baseball/Softball', 'Athletics', 'Artistic Swimming', 'Shooting', 'Table Tennis', 'Judo', 'Football', 'Taekwondo', 'Boxing', 'Weightlifting', 'Archery', 'Fencing', 'Diving', 'Badminton', 'Beach Volleyball', 'Sailing', 'Hockey', 'Trampoline Gymnastics', 'Marathon Swimming', 'Swimming', 'Triathlon', 'Canoe Slalom', 'Water Polo', 'Surfing', 'Canoe Sprint', 'Cycling BMX Racing', 'Rugby Sevens', 'Volleyball', 'Equestrian', 'Tennis', 'Cycling Track', 'Golf', 'Skateboarding', 'Modern Pentathlon', 'Cycling Mountain Bike', '3x3 Basketball', 'Cycling BMX Freestyle', 'Sport Climbing']
uj5u.com熱心網友回復:
該tab20c彩條只有20種顏色比你的類別的數量。您可以做的一件事是將多個顏色圖連接在一起并將其用于您的繪圖。我使用了
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/374556.html
標籤:Python matplotlib 散点图
下一篇:為什么我會在我的情節上收到錯誤“ValueError:系列的真值不明確。使用a.empty、a.bool()、a.item()、a.any()或a.all()”?[復制]
