0

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

  • 1
    you could interpolate the temperature data to find out the y position for x=230, then you have your coordinate to add whatever you want there - text, annotation, line, bar, etc. – Derek Eden Sep 11 '20 at 03:00

2 Answers2

0

Please check the snippet. I will suggest you to use shapely library to calculate intersection between 2 lines. This method also works if the paths do not use the same X-axis values. You will get the point of intersection which you can annotate using loops. Intersection

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

You can check more about Shapely here Shapely

Karthik
  • 2,181
  • 4
  • 10
  • 28
0

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