0

I do much (practically all) of my data analysis in Jupyter/iPython-notebooks. For convenience, I obviously also plot my data in those notebooks using matplotlib/pyplot. Some of those plots I need to recreate externally later on, for example to use them in latex. For this I save the corresponding data as textfiles to the harddrive. Right now, I manually create a numpy-array by stacking all the data needed for the plot, and save this using numpy.savetxt.

What I would like to have is a way to save all data needed for a specific plot written to the same file in an (semi)automatic way, but I am at a loss if it comes to the smart way of doing so.

Thus I have two questions:

  • Is it possible (and save to do) to create something like a plot-memory object, that stores all data plotted per figure, and has a method similar to Memoryobject.save_plot_to_file(figname)? This object would need to know which figure I am working on, so I would need to create a layer above matplotlib, or get this information from the matplotlib objects

  • Is there a simpler way? The python universe is huge, and I do not know half of it. Maybe something like this already exists?

Edit: Clarification: I do not want to save the figure object. What I want to do is something like this:

fig = plt.figure()
fig.plot(x1, y1)
fig.plot(x2, y2 + y3)

# and at a later point
arrays = get_data_from_plot(fig)
data = process(arrays)
np.savetxt('textfile', data)
Thomas K
  • 39,200
  • 7
  • 84
  • 86
Dux
  • 1,226
  • 10
  • 29
  • Never tried it myself, but maybe pickling the figure object would work??? – Julien Jul 30 '16 at 13:13
  • @Dux To extract data from the plot, maybe you can use [this answer](http://stackoverflow.com/questions/8938449/how-to-extract-data-from-matplotlib-plot). – Praveen Jul 30 '16 at 17:13

1 Answers1

0

You could pickle the object (using the cPickle module). See this question here.

Community
  • 1
  • 1
p-robot
  • 4,652
  • 2
  • 29
  • 38
  • Sorry, it seems my question was misleading. I updated it. Bottom line, I only want the data of the plot during runtime, NOT save the figure object – Dux Jul 30 '16 at 14:01
  • You can use `cPickle` for saving data. I used `cPickle` within Jupyter notebooks to save data and reload it at a later time. You can save a variable within the pickled object to tell you which plotting function you used if that's what you're after. – p-robot Jul 30 '16 at 15:22
  • I know that. But I *do* need the data saved to the disk in text format, e.g. csv. – Dux Jul 30 '16 at 15:35