Search code examples
pythonpandasmatplotlibplotannotate

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


Hi I succeeded creating multiple figures of pressure and temperature, etc from excel data using for in loop and generate multiple png files. I attached the script below

Is it possible to automatically create a line or annotation (red scratches) where the temperature of 230 deg C intersects the temperature line plot (blue dotted line)?

figure

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)

Please help, thanks


Solution

  • I just find the interpolated yvalues from the x and then annotate that interpolated y values with straight line

    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))