From c7434ec9c0099ad3c160ec9c0d604a3ca576d491 Mon Sep 17 00:00:00 2001 From: mjmroz Date: Tue, 10 Dec 2024 05:36:29 +0100 Subject: [PATCH] marge functions, add minor ticks, dynamic resolution of interactive lc --- examples/example_16/ulens_model_fit.py | 97 ++++++++++++-------------- 1 file changed, 44 insertions(+), 53 deletions(-) diff --git a/examples/example_16/ulens_model_fit.py b/examples/example_16/ulens_model_fit.py index 2d710179..c0e4a848 100644 --- a/examples/example_16/ulens_model_fit.py +++ b/examples/example_16/ulens_model_fit.py @@ -47,7 +47,7 @@ raise ImportError('\nYou have to install MulensModel first!\n') -__version__ = '0.41.1' +__version__ = '0.42.0' class UlensModelFit(object): @@ -3691,21 +3691,16 @@ def _mark_second_Y_axis_in_best_plot(self): Mark the second (right-hand side) scale for Y axis in the best model plot """ - (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_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_range) - if out1 or out2: + (warning1, warning2, label, ylim, color, ax2, labels, ticks, minor_ticks) = self._get_second_Y_axis_settings() + if warning1 or warning2: ax2.get_yaxis().set_visible(False) - return + return + if minor_ticks is not None: + ax2.set_yticks(minor_ticks, minor=True) + ax2.set_ylabel(label).set_color(color) + ax2.spines['right'].set_color(color) + ax2.tick_params(axis='y', direction="in", which="both", colors=color) - ticks = mm.Utils.get_mag_from_flux(flux) try: # matplotlib version 3.5 or later ax2.set_yticks(ticks=ticks, labels=labels) except Exception: # matplotlib version 3.4.X or smaller @@ -3714,23 +3709,36 @@ def _mark_second_Y_axis_in_best_plot(self): ax2.set_ylim(ylim[0], ylim[1]) + def _get_second_Y_axis_settings(self, ylim=False): + """Creats settings for the second Y axis for the best model plot + """ + if not ylim: + ylim = plt.ylim() + (magnifications, color, label, labels, ax2) = self._second_Y_axis_settings() + (A_range, ref_fluxes) = self._second_Y_axis_get_fluxes(ylim) + warning1, warning2 = False, False + minor_ticks = None + if magnifications == "optimal": + (magnifications, labels, warning1) = self._second_Y_axis_optimal( + ax2, *A_range) + minor_ticks = self._second_Y_axis_minor_ticks(ax2, magnifications, ref_fluxes) + flux = ref_fluxes[0] * np.array(magnifications) + ref_fluxes[1] + warning2 = self._second_Y_axis_warnings(flux, labels, magnifications, *A_range) + ticks = mm.Utils.get_mag_from_flux(flux) + + return (warning1, warning2, label, ylim, color, ax2, labels, ticks, minor_ticks) + def _second_Y_axis_settings(self): """ - Get and apply settings for the second Y axis + Get 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.tick_params(axis='y', direction="in", which="both", colors=color) - - return (magnifications, labels, ylim, ax2) + return (magnifications, color, label, labels, ax2) def _second_Y_axis_get_fluxes(self, ylim): """ @@ -3779,10 +3787,9 @@ def _second_Y_axis_minor_ticks(self, ax2, A_values, ref_fluxes): ax2.minorticks_on() minor_ticks_A = np.array(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) + return minor_ticks_mag def _second_Y_axis_warnings(self, flux, labels, A_values, A_min, A_max): """ @@ -3943,45 +3950,28 @@ def _make_interactive_layout(self, ylim, ylim_residuals, height_ratios, hspace, xaxis2=dict(title_text=xtitle, anchor="y2", mirror='all', scaleanchor='x', matches='x', **kwargs_x) ) return layout - + def _add_second_Y_axis_to_interactive_layout(self, layout, ylim): """Modifiing plotly.graph_objects.Layout object by adding second Y axis """ layout['yaxis']['mirror'] = False layout['yaxis3'] = layout['yaxis'].copy() layout['yaxis3'].update(dict(overlaying="y", side="right", scaleanchor="y")) - layout['yaxis3'].update(self._get_interactive_second_Y_axis_settings_(ylim)) + settings = self._get_interactive_second_Y_axis_settings(ylim) + layout['yaxis3'].update(settings) return layout - def _get_interactive_second_Y_axis_settings_(self, ylim): + def _get_interactive_second_Y_axis_settings(self, ylim): """Creats dictionary with settings for the second Y axis for the interactive plot """ - (magnifications, color, label, labels, ax2) = self._interactive_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_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_range) - if out1 or out2: + (warning1, warning2, label, ylim, color, ax2, labels, ticks, + minor_ticks) = self._get_second_Y_axis_settings(ylim) + if warning1 or warning2: return {} - ticks = mm.Utils.get_mag_from_flux(flux) - return dict(title_text=label, linecolor=color, tickmode='array', tickvals=ticks, ticktext=labels) - - def _interactive_second_Y_axis_settings(self): - """ - Get settings for the interactive 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") - ax2 = plt.gca().twinx() - return (magnifications, color, label, labels, ax2) + if minor_ticks is not None: + minor_ticks = dict(ticks='inside', tickmode='array', tickvals=minor_ticks) + return dict(title_text=label, linecolor=color, tickcolor=color, tickmode='array', tickvals=ticks, + ticktext=labels, minor=minor_ticks) def _get_time_span_data(self): """ @@ -4003,7 +3993,8 @@ def _make_interactive_lc_traces(self, f_source_0, f_blend_0, sizes, colors, opac """ traces_lc = [] subtract = mm.utils.PlotUtils.find_subtract(subtract_2450000, subtract_2460000) - times = np.linspace(t_start, t_stop, num=5000) + time_grid = int(t_stop-t_start)*7 + times = np.linspace(t_start, t_stop, num=time_grid) if isinstance(name, type(None)): showlegend = False