I am trying to plot some values on the matplotlib. This is what I have achieved so far.
Problem is that the color bar only show some colors. how do I push different colors for each game entry?
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()
Data:
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']
CodePudding user response:
The tab20c
colorbar only has 20 colors which is smaller than your number of categories. One thing you could do though is to concatenate several colormaps together and use it for your plot. I used the approach from