diff --git a/seaborn/distributions.py b/seaborn/distributions.py index edaeb2d1ca..8a8a0fc9dd 100644 --- a/seaborn/distributions.py +++ b/seaborn/distributions.py @@ -654,6 +654,18 @@ def plot_univariate_histogram( # Now we handle linewidth, which depends on the scaling of the plot + # We will base everything on the minimum bin width + hist_metadata = pd.concat([ + # Use .items for generality over dict or df + h.index.to_frame() for _, h in histograms.items() + ]).reset_index(drop=True) + thin_bar_idx = hist_metadata["widths"].idxmin() + binwidth = hist_metadata.loc[thin_bar_idx, "widths"] + left_edge = hist_metadata.loc[thin_bar_idx, "edges"] + + # Set initial value + default_linewidth = math.inf + # Loop through subsets based only on facet variables for sub_vars, _ in self.iter_data(): @@ -663,15 +675,6 @@ def plot_univariate_histogram( # Innocuous in other cases? ax.autoscale_view() - # We will base everything on the minimum bin width - hist_metadata = pd.concat([ - # Use .items for generality over dict or df - h.index.to_frame() for _, h in histograms.items() - ]).reset_index(drop=True) - thin_bar_idx = hist_metadata["widths"].idxmin() - binwidth = hist_metadata.loc[thin_bar_idx, "widths"] - left_edge = hist_metadata.loc[thin_bar_idx, "edges"] - # Convert binwidth from data coordinates to pixels pts_x, pts_y = 72 / ax.figure.dpi * abs( ax.transData.transform([left_edge + binwidth] * 2) @@ -684,24 +687,24 @@ def plot_univariate_histogram( # The relative size of the lines depends on the appearance # This is a provisional value and may need more tweaking - default_linewidth = .1 * binwidth_points + default_linewidth = min(.1 * binwidth_points, default_linewidth) - # Set the attributes - for bar in hist_artists: + # Set the attributes + for bar in hist_artists: - # Don't let the lines get too thick - max_linewidth = bar.get_linewidth() - if not fill: - max_linewidth *= 1.5 + # Don't let the lines get too thick + max_linewidth = bar.get_linewidth() + if not fill: + max_linewidth *= 1.5 - linewidth = min(default_linewidth, max_linewidth) + linewidth = min(default_linewidth, max_linewidth) - # If not filling, don't let lines dissapear - if not fill: - min_linewidth = .5 - linewidth = max(linewidth, min_linewidth) + # If not filling, don't let lines dissapear + if not fill: + min_linewidth = .5 + linewidth = max(linewidth, min_linewidth) - bar.set_linewidth(linewidth) + bar.set_linewidth(linewidth) # --- Finalize the plot ----