Home > OS >  Matplotlib scatter points with random color and legend
Matplotlib scatter points with random color and legend

Time:05-02

Noob here but I've searched all over and cannot find any hints to help me with what I'm trying to do. I've written a python program to create a world ranking for marathon swimmers, and based on the database of results, a ranking can be generated for any given day. I want to create a not-crappy-looking chart showing a given athlete's ranking progression over time with a step chart, and overlay points to represent days that they actually competed, and what the competition was.

Here's what I have so far:

dates = [list of consecutive dates]
ranks = [list of the athlete's rank of each of the dates in dates list]
race_dates = [list of dates athlete raced]
race_date_ranks = [list of the athlete's rank of each of the dates in race_date_ranks list]
race_labels = [list of races where the athlete raced (as string)]

plt.step(dates, ranks, where="post")
plt.plot(race_dates, race_date_ranks, "o")
for i, label in enumerate(race_labels):
    plt.text(race_dates[i], race_date_ranks[i], label, rotation=25, fontsize="x-small")

visualization of matplotlib plot based on code above

Problem is...it looks terrible and is illegible (sorry, don't have enough status points or whatever on stackoverflow to embed... some day!). What I want is to kill the last two lines of code above, thereby removing the labels, and have each of the dots representing a race be a randomly colored dot with no label. Then, add a legend with the dot color and the corresponding race label. How can I do this? Help is appreciated!

Here's more about my project if you're interested: enter image description here

  • Related