Reputation: 400
I have a simple figure which I have used matplotlib.plot function in order to plot it. For example:
Is there a way to extract the data points and paste it (like in matlab) to excel sheet which I didn't know about? I want to assume that many figures were created randomly and I didn't know which data/figure I needed until I see the results.
Upvotes: 0
Views: 3564
Reputation: 376
To extract the data-points, you can assign a variable to your plot:
graph = plt.plot(your_data)
data_points = graph[0].get_data()
Example that extracts the data-points of a line plot and saves them to a csv file:
In[1]: import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1, 1, 5)
y = 2*x + 1
xy = plt.plot(x, y)
data = xy[0].get_data()
print(data)
plt.show()
with open('data.csv', 'w') as myfile:
writer = csv.writer(myfile)
writer.writerow(['x', 'y'])
for i in range(len(data[0])):
writer.writerow([data[0][i], data[1][i]])
Out[1]: (array([-1. , -0.5, 0. , 0.5, 1. ]), array([-1., 0., 1., 2., 3.]))
Upvotes: 2