From b54358bdc130b3df4b0aaebb6c6b143a692a55ad Mon Sep 17 00:00:00 2001 From: Jaladh Singhal Date: Tue, 15 Sep 2020 03:28:10 +0530 Subject: [PATCH] Bring hidden scatter point in mid to remove extra space --- tardis/widgets/line_info.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/tardis/widgets/line_info.py b/tardis/widgets/line_info.py index a74da3945e6..110aacc9887 100644 --- a/tardis/widgets/line_info.py +++ b/tardis/widgets/line_info.py @@ -446,6 +446,25 @@ def get_middle_half_edges(arr): (arr[-1] - arr[0]) * 3 / 4 + arr[1], ] + @staticmethod + def get_mid_point_idx(arr): + """ + Get index of the middle point of a sorted array (ascending or descending). + + The values in array may not be evenly distributed so it picks the middle + point not by index but by their values. + + Parameters + ---------- + arr : np.array + + Returns + ------- + int + """ + mid_value = (arr[0] + arr[-1]) / 2 + return np.abs(arr - mid_value).argmin() + def plot_spectrum( self, wavelength, @@ -473,8 +492,13 @@ def plot_spectrum( ------- plotly.graph_objects.FigureWidget """ + # Initially zoomed range in rangeslider should be middle half of spectrum initial_zoomed_range = self.get_middle_half_edges(wavelength.value) + # The scatter point should be a middle point in spectrum otherwise + # the extra padding around it will be oddly visible when near the edge + scatter_point_idx = self.get_mid_point_idx(wavelength.value) + return go.FigureWidget( [ go.Scatter( @@ -489,8 +513,8 @@ def plot_spectrum( ), # Hide a one point scatter trace, to bring boxselect in modebar go.Scatter( - x=wavelength[0], - y=luminosity_density_lambda[0], + x=wavelength[scatter_point_idx], + y=luminosity_density_lambda[scatter_point_idx], mode="markers", marker=dict(opacity=0), showlegend=False,