如何从每条等高线找到并连接最大点

2024-04-25 10:08:24 发布

您现在位置:Python中文网/ 问答频道 /正文

如何找到等高线图生成的曲线的最大点,然后连接它们

import matplotlib.pyplot as plt
import numpy as np

t = np.arange(263, 383, 0.001) 
r=np.arange(0.1,0.9,0.1)
T, R = np.meshgrid(t, r)
k1=np.exp(17.34-(48900./(8.314*T)))
k2=np.exp(42.02-(124200./(8.314*T))) 
XA=(k1-R)/(k1+k2)
plt.contour(T,XA,R)
plt.axis([263,383,0,1])
plt.show()

contour curves


Tags: importnumpymatplotlibasnppltk2k1
1条回答
网友
1楼 · 发布于 2024-04-25 10:08:24
  • 从数组的每一行XA提取最大值的索引idx
  • 使用TXA上的idx提取x轴和y轴值。
    • 为数组编制索引比使用y = XA.max(axis=1)获取max{}值稍微快一些
  • XA的形状是(8, 120000),因此有8个最大值。我不确定为什么只显示了7条等高线。
    • 使用x[:-1]y[:-1]不绘制最后一点
# get index of max value for each row
idx = np.argmax(XA, axis=1)
# use idx to get the x-axis values from T that correspond to max XA
x = np.take_along_axis(T, np.expand_dims(idx, axis=-1), axis=-1).squeeze(axis=-1)
# use idx to get the max y-axis values from XA
y = np.take_along_axis(XA, np.expand_dims(idx, axis=-1), axis=-1).squeeze(axis=-1)

# plot
plt.contour(T,XA,R)
plt.plot(x, y, marker='o')  # plot the points
plt.axis([263,383,0,1])
plt.show()

enter image description here

相关问题 更多 >