I am trying to plot a horizontal line from a selected point in Y-axis (say, y = 150, in our following sample data) to a curve (blue colored straight line in our sample plot). Then, from the point of intersection of horizontal line with the curve, I want to drop a line perpendicular to the X - axis. I tried following code line but I could not limit the value of parameter xmax
such that the horizontal line ends exactly at the point of intersection with the curve. I also know that maximum limit of xmax
= 1.0
. For this case, xmax
= 0.5
may give me the point of intersection. However, I want to implement this on more complex curves. Can somebody please advise me?
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame({'x': np.arange(10),
'y': np.arange(100, 200, 10)})
fig,ax = plt.subplots(figsize =(4.5,4.5))
ax.plot(df.x,df.y)
plt.xlabel('X')
plt.ylabel('Y')
plt.xlim(0,10)
plt.ylim(100,200)
# ax.axhline(y=150, xmin=-0.02, xmax=0.5, clip_on= True)
ax.axhline(y=150, xmin=0, xmax= df.loc[df['y'] == 150, 'x'].iloc[0], clip_on= False, color = 'Green')
plt.show()
plt.clf()