Skip to content

Commit

Permalink
Merge pull request #120 from rapoliveira/bugfix-example16-residuals
Browse files Browse the repository at this point in the history
Bugfix example16: minor changes to magnification axis
  • Loading branch information
rpoleski authored Mar 18, 2024
2 parents d54c837 + 323976b commit cc365ed
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions examples/example_16/ulens_model_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
except Exception:
raise ImportError('\nYou have to install MulensModel first!\n')

__version__ = '0.34.3'
__version__ = '0.34.4'


class UlensModelFit(object):
Expand Down Expand Up @@ -3096,32 +3096,41 @@ def _mark_second_Y_axis_in_best_plot(self):
Mark the second (right-hand side) scale for Y axis in
the best model plot
"""
settings = self._plots['best model']["second Y scale"]
magnifications = settings['magnifications']
color = settings.get("color", "red")
label = settings.get("label", "magnification")
labels = settings.get("labels")

ylim = plt.ylim()
ax2 = plt.gca().twinx()
(A_min, A_max, sb_fluxes) = self._second_Y_axis_get_fluxes(ylim)
(magnifications, labels, ylim, ax2) = self._second_Y_axis_settings()
(A_range, ref_fluxes) = self._second_Y_axis_get_fluxes(ylim)
out1, out2 = False, False
if magnifications == "optimal":
(magnifications, labels, out1) = self._second_Y_axis_optimal(
ax2, A_min, A_max)
flux = sb_fluxes[0] * magnifications + sb_fluxes[1]
ax2, *A_range)
self._second_Y_axis_minor_ticks(ax2, magnifications, ref_fluxes)
flux = ref_fluxes[0] * np.array(magnifications) + ref_fluxes[1]
out2 = self._second_Y_axis_warnings(flux, labels, magnifications,
A_min, A_max)
*A_range)
if out1 or out2:
ax2.get_yaxis().set_visible(False)
return

ticks = mm.Utils.get_mag_from_flux(flux)
ax2.set_yticks(ticks, labels)
ax2.set_ylim(ylim[0], ylim[1])

def _second_Y_axis_settings(self):
"""
Get and apply settings for the second Y axis
"""
settings = self._plots['best model']["second Y scale"]
magnifications = settings['magnifications']
color = settings.get("color", "black")
label = settings.get("label", "Magnification")
labels = settings.get("labels")
ylim = plt.ylim()

ax2 = plt.gca().twinx()
ax2.set_ylabel(label).set_color(color)
ax2.spines['right'].set_color(color)
ax2.set_ylim(ylim[0], ylim[1])
ax2.tick_params(axis='y', colors=color)
plt.yticks(ticks, labels, color=color)
ax2.tick_params(axis='y', direction="in", which="both", colors=color)

return (magnifications, labels, ylim, ax2)

def _second_Y_axis_get_fluxes(self, ylim):
"""
Expand All @@ -3135,7 +3144,7 @@ def _second_Y_axis_get_fluxes(self, ylim):
A_min = (flux_min - blend_flux) / total_source_flux
A_max = (flux_max - blend_flux) / total_source_flux

return (A_min, A_max, [total_source_flux, blend_flux])
return ([A_min, A_max], [total_source_flux, blend_flux])

def _second_Y_axis_optimal(self, ax2, A_min, A_max):
"""
Expand Down Expand Up @@ -3163,6 +3172,18 @@ def _second_Y_axis_optimal(self, ax2, A_min, A_max):

return (A_values[fnum < 4], labels[fnum < 4].tolist(), False)

def _second_Y_axis_minor_ticks(self, ax2, A_values, ref_fluxes):
"""
Get minor ticks for magnification axis from matplotlib
"""
ax2.minorticks_on()
minor_ticks_A = ax2.yaxis.get_ticklocs(minor=True)
minor_ticks_A = minor_ticks_A[~np.isin(minor_ticks_A, A_values)]

minor_ticks_flux = ref_fluxes[0] * minor_ticks_A + ref_fluxes[1]
minor_ticks_mag = mm.Utils.get_mag_from_flux(minor_ticks_flux)
ax2.set_yticks(minor_ticks_mag, minor=True)

def _second_Y_axis_warnings(self, flux, labels, A_values, A_min, A_max):
"""
Issue warnings for negative flux or bad range of magnificaitons
Expand Down

0 comments on commit cc365ed

Please sign in to comment.