Skip to content

Commit 4a9f492

Browse files
authored
Merge pull request #5139 from janezd/distribution-bar-width
[ENH] Distribution: Show equal bar widths on unique-valued bins
2 parents f7fd48c + ba76f17 commit 4a9f492

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

Orange/widgets/visualize/owdistributions.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -668,24 +668,29 @@ def _disc_split_plot(self):
668668
def _cont_plot(self):
669669
self._set_cont_ticks()
670670
data = self.valid_data
671-
y, x = np.histogram(
672-
data, bins=self.binnings[self.number_of_bins].thresholds)
671+
binning = self.binnings[self.number_of_bins]
672+
y, x = np.histogram(data, bins=binning.thresholds)
673673
total = len(data)
674674
colors = [QColor(0, 128, 255)]
675675
if self.fitted_distribution:
676676
colors[0] = colors[0].lighter(130)
677677

678678
tot_freq = 0
679679
lasti = len(y) - 1
680+
width = np.min(x[1:] - x[:-1])
681+
unique = self.number_of_bins == 0 and binning.width is None
682+
xoff = -width / 2 if unique else 0
680683
for i, (x0, x1), freq in zip(count(), zip(x, x[1:]), y):
681684
tot_freq += freq
682-
desc = self.str_int(x0, x1, not i, i == lasti)
685+
desc = self.str_int(x0, x1, not i, i == lasti, unique)
683686
tooltip = \
684687
"<p style='white-space:pre;'>" \
685688
f"<b>{escape(desc)}</b>: " \
686689
f"{freq} ({100 * freq / total:.2f} %)</p>"
690+
bar_width = width if unique else x1 - x0
687691
self._add_bar(
688-
x0, x1 - x0, 0, [tot_freq if self.cumulative_distr else freq],
692+
x0 + xoff, bar_width, 0,
693+
[tot_freq if self.cumulative_distr else freq],
689694
colors, stacked=False, expanded=False, tooltip=tooltip,
690695
desc=desc, hidden=self.hide_bars)
691696

@@ -697,8 +702,8 @@ def _cont_plot(self):
697702
def _cont_split_plot(self):
698703
self._set_cont_ticks()
699704
data = self.valid_data
700-
_, bins = np.histogram(
701-
data, bins=self.binnings[self.number_of_bins].thresholds)
705+
binning = self.binnings[self.number_of_bins]
706+
_, bins = np.histogram(data, bins=binning.thresholds)
702707
gvalues = self.cvar.values
703708
varcolors = [QColor(*col) for col in self.cvar.colors]
704709
if self.fitted_distribution:
@@ -720,12 +725,17 @@ def _cont_split_plot(self):
720725
tot_freqs = np.zeros(len(ys))
721726

722727
lasti = len(ys[0]) - 1
728+
width = np.min(bins[1:] - bins[:-1])
729+
unique = self.number_of_bins == 0 and binning.width is None
730+
xoff = -width / 2 if unique else 0
723731
for i, x0, x1, freqs in zip(count(), bins, bins[1:], zip(*ys)):
724732
tot_freqs += freqs
725733
plotfreqs = tot_freqs.copy() if self.cumulative_distr else freqs
726-
desc = self.str_int(x0, x1, not i, i == lasti)
734+
desc = self.str_int(x0, x1, not i, i == lasti, unique)
735+
bar_width = width if unique else x1 - x0
727736
self._add_bar(
728-
x0, x1 - x0, 0 if self.stacked_columns else 0.1, plotfreqs,
737+
x0 + xoff, bar_width, 0 if self.stacked_columns else 0.1,
738+
plotfreqs,
729739
gcolors, stacked=self.stacked_columns, expanded=self.show_probs,
730740
hidden=self.hide_bars,
731741
tooltip=self._split_tooltip(
@@ -899,12 +909,12 @@ def min_var_resolution(var):
899909
return 0
900910
return 10 ** -var.number_of_decimals
901911

902-
def str_int(self, x0, x1, first, last):
912+
def str_int(self, x0, x1, first, last, unique=False):
903913
var = self.var
904914
sx0, sx1 = var.repr_val(x0), var.repr_val(x1)
905915
if self.cumulative_distr:
906916
return f"{var.name} < {sx1}"
907-
elif first and last:
917+
elif first and last or unique:
908918
return f"{var.name} = {sx0}"
909919
elif first:
910920
return f"{var.name} < {sx1}"

0 commit comments

Comments
 (0)