Skip to content

Commit

Permalink
Merge pull request #5139 from janezd/distribution-bar-width
Browse files Browse the repository at this point in the history
[ENH] Distribution: Show equal bar widths on unique-valued bins
  • Loading branch information
ajdapretnar authored Jan 8, 2021
2 parents f7fd48c + ba76f17 commit 4a9f492
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions Orange/widgets/visualize/owdistributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,24 +668,29 @@ def _disc_split_plot(self):
def _cont_plot(self):
self._set_cont_ticks()
data = self.valid_data
y, x = np.histogram(
data, bins=self.binnings[self.number_of_bins].thresholds)
binning = self.binnings[self.number_of_bins]
y, x = np.histogram(data, bins=binning.thresholds)
total = len(data)
colors = [QColor(0, 128, 255)]
if self.fitted_distribution:
colors[0] = colors[0].lighter(130)

tot_freq = 0
lasti = len(y) - 1
width = np.min(x[1:] - x[:-1])
unique = self.number_of_bins == 0 and binning.width is None
xoff = -width / 2 if unique else 0
for i, (x0, x1), freq in zip(count(), zip(x, x[1:]), y):
tot_freq += freq
desc = self.str_int(x0, x1, not i, i == lasti)
desc = self.str_int(x0, x1, not i, i == lasti, unique)
tooltip = \
"<p style='white-space:pre;'>" \
f"<b>{escape(desc)}</b>: " \
f"{freq} ({100 * freq / total:.2f} %)</p>"
bar_width = width if unique else x1 - x0
self._add_bar(
x0, x1 - x0, 0, [tot_freq if self.cumulative_distr else freq],
x0 + xoff, bar_width, 0,
[tot_freq if self.cumulative_distr else freq],
colors, stacked=False, expanded=False, tooltip=tooltip,
desc=desc, hidden=self.hide_bars)

Expand All @@ -697,8 +702,8 @@ def _cont_plot(self):
def _cont_split_plot(self):
self._set_cont_ticks()
data = self.valid_data
_, bins = np.histogram(
data, bins=self.binnings[self.number_of_bins].thresholds)
binning = self.binnings[self.number_of_bins]
_, bins = np.histogram(data, bins=binning.thresholds)
gvalues = self.cvar.values
varcolors = [QColor(*col) for col in self.cvar.colors]
if self.fitted_distribution:
Expand All @@ -720,12 +725,17 @@ def _cont_split_plot(self):
tot_freqs = np.zeros(len(ys))

lasti = len(ys[0]) - 1
width = np.min(bins[1:] - bins[:-1])
unique = self.number_of_bins == 0 and binning.width is None
xoff = -width / 2 if unique else 0
for i, x0, x1, freqs in zip(count(), bins, bins[1:], zip(*ys)):
tot_freqs += freqs
plotfreqs = tot_freqs.copy() if self.cumulative_distr else freqs
desc = self.str_int(x0, x1, not i, i == lasti)
desc = self.str_int(x0, x1, not i, i == lasti, unique)
bar_width = width if unique else x1 - x0
self._add_bar(
x0, x1 - x0, 0 if self.stacked_columns else 0.1, plotfreqs,
x0 + xoff, bar_width, 0 if self.stacked_columns else 0.1,
plotfreqs,
gcolors, stacked=self.stacked_columns, expanded=self.show_probs,
hidden=self.hide_bars,
tooltip=self._split_tooltip(
Expand Down Expand Up @@ -899,12 +909,12 @@ def min_var_resolution(var):
return 0
return 10 ** -var.number_of_decimals

def str_int(self, x0, x1, first, last):
def str_int(self, x0, x1, first, last, unique=False):
var = self.var
sx0, sx1 = var.repr_val(x0), var.repr_val(x1)
if self.cumulative_distr:
return f"{var.name} < {sx1}"
elif first and last:
elif first and last or unique:
return f"{var.name} = {sx0}"
elif first:
return f"{var.name} < {sx1}"
Expand Down

0 comments on commit 4a9f492

Please sign in to comment.