Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Rory235 opened this issue Apr 13, 2023 · 5 comments
Open

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

Rory235 opened this issue Apr 13, 2023 · 5 comments

Comments

@Rory235
Copy link

Rory235 commented Apr 13, 2023

I have tried the following

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

@nkeim
Copy link
Contributor

nkeim commented Apr 13, 2023

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
Copy link
Author

Rory235 commented Apr 13, 2023

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
Copy link
Contributor

nkeim commented Apr 13, 2023

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
Copy link
Author

Rory235 commented Apr 13, 2023

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
Copy link
Contributor

nkeim commented Apr 14, 2023

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(...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants