[英]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. 因此我正在运行Python 3.4,并且想知道如何使用matplotlib将最大值绘制为当前线性图上的一个点。 My current graph is very simply: it has two lines with y values as scores and x values as time.
我当前的图形非常简单:它有两条线,其中y值为得分,而x值为时间。 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?
有人知道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): 我最终要做的是使用两个图(time_list是x轴值,score是y值列表):
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. 如果要使用除坐标以外的其他文本标签,则只需将最后一行中的“ max_coords”替换为所需的任何字符串。
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"...) 如果要查找每个行的最大值,则只需具有多个x和y列表并执行相同的过程(例如,使用“ time_list_1”,“ time_list_2”,...和“ score_1“,”得分_2“ ...)
Hope this helped someone. 希望这对某人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.