简体   繁体   中英

How do I plot the point of max value on a line graph in matplotlib?

so I am running Python 3.4 and was wondering how I could use matplotlib to plot the maximum value as a point on my current linear graph. My current graph is very simply: it has two lines with y values as scores and x values as time. I am trying to plot a point on each individual line at the time where the maximum score is reached and also show its coordinates: (optimal time, max score). Does anyone know if there is a way to do this with matplotlib? Thanks in advance.

What I ended up doing was using two plots (time_list is the x-axis values and score is the list of y-values):

ordered_time = [time_list for (score,time_list) in sorted(zip(score,time_list))]
best_time = ordered_time[-1]
max_coords = '('+str(best_time)+', ' + str("%.4f" % (max(score)))+')'
max_point = pl.plot(best_time, max(score), 'bo', label="(Opt. Time, Max Score)")
pl.text(best_time, max(score), max_coords)
... (insert rest of stuff for your graph)

This will find the max point on a specific line, plot a point onto it, and then label the point with its coordinates.

If you want a different text label other than coordinates then just replace "max_coords" in the last line with whatever string you want.

If you want to find the max for EACH line then just have multiple x and y lists and go through the same process (eg instead of "time_list" and "score", use "time_list_1", "time_list_2", ... and "score_1", "score_2"...)

Hope this helped someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM