Skip to content

Bugfix example16: minor changes to magnification axis #120

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

Merged
merged 7 commits into from
Mar 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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