soft-matter / trackpy

Python particle tracking toolkit
http://soft-matter.github.io/trackpy
Other
437 stars 131 forks source link

How do you save the images TrackPy creates for reports #729

Open Rory235 opened 1 year ago

Rory235 commented 1 year ago

I have tried the following

plt.figure("1") tp.plot_traj(t1, superimpose=frame[1000]) plt.imsave("C:/Users/Rory/Downloads", arr="MxN")

nkeim commented 1 year ago

Have you tried not calling plt.figure() beforehand?

If you want to remove all ambiguity, you can get or create axes in the current figure and make sure that plot_traj uses them. Something like (not sure if it's 100% correct):


fig = plt.figure()
ax = fig.get_axes()
tp.plot_traj(..., ax=ax)
fig.savefig(...)
Rory235 commented 1 year ago

EDIT: Spelling So trackpy still creates the plot without the plt.figure()

I tried this

fig1=tp.plot_traj(t1, superimpose=frame[1000]) fig1.savefig(r'C:\Users\Rory\Downloads\1.png')

but get the error

`AttributeError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_24356/3548345737.py in 1 fig1=tp.plot_traj(t1, superimpose=frame[1000]) ----> 2 fig1.savefig(r'C:\Users\Rory\Downloads\1.png')

AttributeError: 'AxesSubplot' object has no attribute 'savefig'`

nkeim commented 1 year ago

That's right. plot_traj returns an Axes object, not a figure. So if you are being explicit about it, you should first get the figure, then tell plot_traj to use axes within it, and then save the figure.

I'll admit I'm a bit surprised that tp.plot_traj(...); plt.savefig(filename) doesn't work. I had thought that it was that simple.

Rory235 commented 1 year ago

Ahhhh I see. I gave this a try

fig1 = plt.figure() ax = fig1.get_axes() tp.plot_traj(t1, ax=ax) fig1.savefig(r'C:\Users\Rory\Downloads\1.png')

getting

AttributeError: 'list' object has no attribute 'set_xlabel'

nkeim commented 1 year ago

You're asking the wrong person for matplotlib help, but you could do this instead.

fig = plt.figure()
ax = plt.gca()
tp.plot_traj(..., ax=ax)
fig.savefig(...)

or

ax = tp.plot_traj(...)
ax.figure.savefig(...)