简体   繁体   English

如何在matplotlib的折线图中绘制最大值点?

[英]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.

相关问题 我如何 plot 和 animation 和一个点在同一个 matplotlib Z32FA6E1B78A9D4028953E6AA5C - How do I plot an animation and a point in the same matplotlib plot 如何使用 matplotlib 子图为每一行绘制折线图? - How do I plot a line graph for each row using matplotlib subplots? Matplotlib-如何在x值是每个y值的频率的位置绘制图? - Matplotlib-How do I plot a graph where the x values are the frequency of each y value? 如何在 matplotlib plot 中添加点图例? - How do I add a point legend in the matplotlib plot? 对于折线图(matplotlib,pandas)的 dataframe 中缺少 x 值,如何将 y 值设置为 0 - How do I set y value as 0 for missing x values in dataframe for line graph (matplotlib, pandas) Python Matplotlib-如何在X轴上绘制一条线? - Python matplotlib - How do I plot a line on the x-axis? 我如何延长 matplotlib plot 中的趋势线? - How do i extend trend line in matplotlib plot? 如何将 Xarray plot xr.ufuncs.log(data_slice).plot(cmap='magma', vmin=0, vmax = max_value*.7) 转换为 matplotlib Z32AA49E1B78A942A - How do I convert the Xarray plot xr.ufuncs.log(data_slice).plot(cmap='magma', vmin=0, vmax = max_value*.7) into the matplotlib plot? 如何使matplotlib生成单个图而不是每个数据点的图? - How do I make matplotlib generate a single plot instead of a plot for each data point? 如何用 matplotlib 绘制这样的图 - How to plot such a graph with matplotlib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM