rast
rast

Reputation: 3

Is there an easy way in Python to get the coordinates from a plot?

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

Answers (2)

pakpe
pakpe

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.enter image description here

Upvotes: 0

Chris
Chris

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

enter image description here

Upvotes: 1

Related Questions