如何在循环中的某些xy值处自动注释点或线

how to automatically annotate point or line at certain xy values in loop

嗨,我成功地使用 for in 循环从 excel 数据创建了多个压力和温度等数字,并生成了多个 png 文件。我附上了下面的脚本

是否可以在 230 摄氏度的温度与温度线图(蓝色虚线)相交处自动创建一条线或注释(红色划痕)?

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

path ='F:\Backup\JN\TOR\TOR HLS py.xlsx'
data= pd.ExcelFile(path)

sheets = data.sheet_names
well = ''

for i in sheets:
    well=pd.read_excel(data, sheet_name=i)
    fig=plt.figure(figsize=(8,12), constrained_layout='True')
    plt.plot(well['x csg'], well['mdpl csg'], marker='s', linestyle='solid', color='black')
    plt.plot(well['x liner'], well['mdpl liner'], marker='s', linestyle='dotted', color='black')
    plt.plot(well['T'], well['mdpl pt'], marker='o', color='blue', label='Temperature')
    plt.plot(well['P'], well['mdpl pt'], marker='o', color='crimson', label='Pressure')
    for x, txt in enumerate(well['csg']):
        plt.annotate(txt, ((well['x csg']+5)[x], well['mdpl csg'][x]), size=8)
    for y, txt in enumerate(well['liner']):
        plt.annotate(txt, ((well['x liner']+5)[y], well['mdpl liner'][y]), size=8)
    plt.savefig(str(i), dpi=300, transparent='True')
    plt.close(i)

请帮忙,谢谢

请检查代码段。我建议您使用 shapely 库来计算两条线之间的交点。如果路径不使用相同的 X-axis 值,此方法也适用。您将获得可以使用循环注释的交点。

from matplotlib import pyplot as plt
import numpy as np
import shapely
from shapely.geometry import LineString, Point

x = np.linspace(0, 5, 20)
y1 = x**2
y2 = 3*x

za1=[(i,j) for i,j in zip(x,y1)]    #points for (x,y) line1
za2=[(i,j) for i,j in zip(x,y2)]    #points for (x,y) line2

line1 = LineString(za1)
line2 = LineString(za2)

intersection = line1.intersection(line2)
intersect_points = [list(p.coords)[0] for p in intersection]
print(intersect_points)
#[(0.0, 0.0), (2.994555353901996, 8.983666061705987)]     point of intersection

fig, ax = plt.subplots()
plt.plot(x, y1, color='lightcoral')
plt.plot(x, y2, color='#4b0082')

for i in intersect_points:
  ax.annotate('x',xy=(i),fontsize=20,color='red')
plt.show()

您可以在此处查看有关 Shapely 的更多信息 Shapely

我只是从 x 中找到插值的 y 值,然后用直线注释插值的 y 值

y230=float(np.interp(xval, well['T'], well['mdpl pt']))
if math.isnan(y230) == False:
    plt.annotate('ToR 230 $^o$C', xy=(200, y230), xytext=(250, (y230-9)), 
    arrowprops=dict(color='green', arrowstyle="-", lw=2))