Reputation: 3
is there a way to obtain specific coordinates from a plot even when they aren't used in the plotting process?
for example: can i extract the value at x=0.5 from the plot below? (just an easy example, want to use it for more complicated ones too)
import matplotlib.pyplot as plt
x=[0,1]
y=[1,2]
plt.plot(x,y)
Upvotes: 0
Views: 1175
Reputation: 5479
No. This is not a limitation of Python. If your graph conforms precisely to a function, as in your example, you can obviously use the function to interpolate. If it does not, it wouldn't make sense to read intermediate data from your plot. For instance, you shouldn't try to read the y value for x = 4 from the graph below. In these cases, you need to resort to linear or non-linear curve fitting as suggested by the other responders.
Upvotes: 0
Reputation: 16147
Theoretically you can do something like this, but since the equation itself isn't something you can extract you're limited in precision.
import seaborn as sns
x=[0,1]
y=[1,2]
p = sns.regplot(x=x,y=y, ci=None)
line = dict(zip(p.get_lines()[0].get_xdata().round(1),p.get_lines()[0].get_ydata().round(1)))
print(line[0.5])
Output
1.5
Upvotes: 1