python - 查找 y 最大值的 x 坐标
问题描述
我有一个分成 3. (0 < x < L1) (L1 < x < a) (a < x L2) 的函数。
无论 x 在 (0 < x < L2) 上的哪个位置,我都需要在图上为最大值添加一个符号。我有:
c1 = np.arange(0,L1+0.1, 0.1)
c2 = np.arange(L1,a+0.1, 0.1)
c3 = np.arange(a,L+0.1, 0.1)
y1 = -q*c1
y2 = -q*c2 + RAV
y3 = -q*c3 + RAV - P
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
ax1.fill_between(c1, y1)
ax1.fill_between(c2, y2)
ax1.fill_between(c3, y3)
ax1.set_title('S curve')
Mmax1=np.max(y1)
Mmax2=np.max(y2)
Mmax3=np.max(y3)
Mmax= round(max(Mmax1,Mmax2,Mmax3), 2)
现在我想找到 y 值 Mmax 的 x 坐标,但我不知道如何使用 x[np.argmax(Mmax)] where x = a.any(c1, c2, c3) 之类的东西。
我需要 x 坐标,以便我可以将它绘制在值出现的位置
ax2.annotate(text2,
xy=(max_x, Mmax), xycoords='data',
xytext=(0, 30), textcoords='offset points',
arrowprops=dict(arrowstyle="->"))
我该如何解决?谢谢!
解决方案
在这里,我将说明什么可能对您有用,因为我没有完整的代码来创建您想要的函数,所以我定义了 3 个简单的函数,描述如下:out_arr=np.maximum.reduce([y1,y2,y3])
我们将在 x 中获得最大值,并result = np.where(out_arr == np.amax(out_arr))
找出哪个索引有最大值。那么最大品脱将是point=[Max_X,out_arr[Max_X]]
import matplotlib.pyplot as plt
import numpy as np
x= np.arange(0., 6., 1)
y1=x
y2=x**2
y3=x**3
out_arr=np.maximum.reduce([y1,y2,y3])
result = np.where(out_arr == np.amax(out_arr))
Max_X=result[0]
print(Max_X)
point=[Max_X,out_arr[Max_X]]
plt.plot(Max_X,out_arr[Max_X],'ro')
# red dashes, blue squares and green triangles
plt.plot(x, y1, 'r--', x, y2, 'bs', x, y3, 'g^')
plt.show()
推荐阅读
- ionic-framework - 在模块 ts 中输入提供程序时出错
- python - 使用 cuda 10 安装 pip tensorflow-gpu 后出错
- gitlab - Gitlab CI 对发布或调试构建的依赖
- laravel - 在特征或模型中模拟依赖注入
- javascript - 未捕获的 TypeError:$.getScript(...).done 不是函数
- apache-beam - Apache Beam 2.9 使用 writeDynamic 将 Avro 文件写入 GCS 上的多个目录
- amazon-web-services - AWS Data Pipeline:IAM 角色的权限 S3 访问问题
- ruby-on-rails - 如何修复'TypeError - nil 不是符号也不是字符串:'?
- angular - 带有嵌套对象的 BreezeJs 查询
- python-3.x - 设置 tkinter ttk 框架的背景颜色