Skip to content

Commit

Permalink
fix plot saving bug in imexam loop
Browse files Browse the repository at this point in the history
  • Loading branch information
sosey committed Oct 18, 2021
1 parent cd21c7a commit f9cc6ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion imexam/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def _run_imexam(self):
# This loop now recognizes the arrow keys
# for moving the cursor in the window, it calls
# the windows cursor function. However, depending
# on how the use has their windowing focus setup
# on how the user has their windowing focus setup
# they might loose focus on the DS9 window and have to
# move the cursor to gain focus again, is there a way
# around this? Cursor is not implemented in the ginga
Expand Down
38 changes: 21 additions & 17 deletions imexam/imexamine.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ def __init__(self):
self._define_default_pars()
# default plot name saved with "s" key
self.plot_name = "imexam_plot.pdf"
# let users have multiple plot windows, the list stores their names
# let users have multiple plot windows, the list stores their instance reference
self._plot_windows = list()
# this contains the name of the current plotting window
self._figure_name = "imexam"
self._plot_windows.append(self._figure_name)
self._plot_windows.append(plt.gcf())
self._reserved_keys = ['q', '2'] # not to be changed with user funcs
self._fit_models = ["Gaussian1D",
"Moffat1D",
Expand Down Expand Up @@ -130,7 +130,7 @@ def setlog(self, filename=None, on=True, level=logging.INFO):
def _close_plots(self):
"""Make sure to release plot memory at end of exam loop."""
for plot in self._plot_windows:
plt.close()
plt.close(plot)

def close(self):
"""For use with the Imexamine object standalone."""
Expand Down Expand Up @@ -296,8 +296,9 @@ def new_plot_window(self, x, y, data=None):
"""
if data is None:
data = self._data
# save the old figure
self._plot_windows.append(plt.gcf())
self._figure_name = "imexam" + str(len(self._plot_windows) + 1)
self._plot_windows.append(self._figure_name)
self.log.info(f"Plots now directed towards {self._figure_name}")

def plot_line(self, x, y, data=None, fig=None):
Expand Down Expand Up @@ -353,6 +354,7 @@ def plot_line(self, x, y, data=None, fig=None):
plt.pause(0.001)
else:
fig.canvas.draw_idle()
self._plot_windows[-1] = plt.gcf()

def plot_column(self, x, y, data=None, fig=None):
"""column plot of data at point y.
Expand Down Expand Up @@ -409,6 +411,7 @@ def plot_column(self, x, y, data=None, fig=None):
plt.pause(0.001)
else:
fig.canvas.draw_idle()
self._plot_windows[-1] = plt.gcf()

def show_xy_coords(self, x, y, data=None):
"""print the x,y,value to the screen.
Expand Down Expand Up @@ -479,17 +482,10 @@ def save_figure(self, fig=None):
Parameters
----------
data: numpy array
The data array to work on
fig: figure for redirect
Used for interaction with the ginga GUI
"""
if fig is None:
fig = plt.figure(self._figure_name)
ax = fig.gca()
fig.savefig(self.plot_name)
pstr = f"plot saved to {self.plot_name}"
self.log.info(pstr)
self.save(fig=fig)

def save(self, filename=None, fig=None):
"""Save to file the figure that's currently displayed.
Expand All @@ -510,10 +506,10 @@ def save(self, filename=None, fig=None):
self.set_plot_name(self._figure_name + ".pdf")

if fig is None:
fig = plt.figure(self._figure_name)
ax = fig.gca()
fig.savefig(self.plot_name)
pstr = f"plot saved to {self.plot_name}"
fig = self._plot_windows[-1]

fig.savefig(self.plot_name)
pstr = f"plot {fig.number} saved to {self.plot_name}"
self.log.info(pstr)

def aper_phot(self, x, y, data=None,
Expand Down Expand Up @@ -697,6 +693,7 @@ def aper_phot(self, x, y, data=None,
plt.pause(0.001)
else:
fig.canvas.draw_idle()
self._plot_windows[-1] = plt.gcf()
else:
return (apertures, annulus_apertures, rawflux_table, sky_per_pix)

Expand Down Expand Up @@ -880,6 +877,8 @@ def line_fit(self, x, y, data=None, form=None, genplot=True, fig=None, col=False
plt.pause(0.001)
else:
fig.canvas.draw_idle()
self._plot_windows[-1] = plt.gcf()


else:
return fitted
Expand Down Expand Up @@ -1044,7 +1043,7 @@ def radial_profile(self, x, y, data=None, form=None,
"""
pars = self.radial_profile_pars

if data is None:
data = self._data

Expand Down Expand Up @@ -1262,6 +1261,7 @@ def radial_profile(self, x, y, data=None, form=None,
plt.pause(0.001)
else:
fig.canvas.draw_idle()
self._plot_windows[-1] = plt.gcf()
else:
return radius, flux

Expand Down Expand Up @@ -1358,6 +1358,7 @@ def curve_of_growth(self, x, y, data=None, genplot=True, fig=None):
plt.pause(0.001)
else:
fig.canvas.draw_idle()
self._plot_windows[-1] = plt.gcf()

else:
return rapert, flux
Expand Down Expand Up @@ -1527,6 +1528,7 @@ def histogram(self, x, y, data=None, genplot=True, fig=None):
plt.pause(0.001)
else:
fig.canvas.draw_idle()
self._plot_windows[-1] = plt.gcf()
else:
hist, bin_edges = np.histogram(flat_data,
num_bins,
Expand Down Expand Up @@ -1600,6 +1602,7 @@ def contour(self, x, y, data=None, fig=None):
plt.pause(0.001)
else:
fig.canvas.draw_idle()
self._plot_windows[-1] = plt.gcf()

def surface(self, x, y, data=None, fig=None):
"""plot a surface around the specified location.
Expand Down Expand Up @@ -1706,6 +1709,7 @@ def surface(self, x, y, data=None, fig=None):
plt.pause(0.001)
else:
fig.canvas.draw_idle()
self._plot_windows[-1] = plt.gcf()

def cutout(self, x, y, data=None, size=None, fig=None):
"""Make a fits cutout around the pointer location without wcs.
Expand Down

0 comments on commit f9cc6ca

Please sign in to comment.