Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 491: Get rid of Error terms in Plots #539

Merged
merged 2 commits into from
Jun 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 73 additions & 65 deletions datascience/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@

_INTERACTIVE_PLOTS = False

# Set numpy printoptions to legacy to get around error terms, as described in
# https://github.com/data-8/datascience/issues/491
np.set_printoptions(legacy='1.13')

class Table(collections.abc.MutableMapping):
"""A sequence of string-labeled columns."""
plots = collections.deque(maxlen=10)
Expand Down Expand Up @@ -5393,79 +5397,83 @@ def prepare_hist_with_group(group):
right_end = max([max(self.column(k)) for k in self.labels if np.issubdtype(self.column(k).dtype, np.number)])

def draw_hist(values_dict):
with np.printoptions(legacy='1.13'):
# This code is factored as a function for clarity only.
n = len(values_dict)
colors = [rgb_color + (self.default_alpha,) for rgb_color in
itertools.islice(itertools.cycle(self.chart_colors), n)]
hist_names = list(values_dict.keys())
values = [v[0] for v in values_dict.values()]
weights = [v[1] for v in values_dict.values() if len(v) > 1]
if n > len(weights) > 0:
raise ValueError("Weights were provided for some columns, but not "
" all, and that's not supported.")
if rug and overlay and n > 1:
warnings.warn("Cannot plot overlaid rug plots; rug=True ignored", UserWarning)
# Check if np.printoptions is set to legacy. Throw UserWarning if not
if np.get_printoptions()['legacy'] != '1.13':
warnings.warn("We've detected you're not using the '1.13' legacy setting for `np.printoptions`. "
"This may cause excessive error terms in your plots. We recommend solving this by running the "
"following code: `np.set_printoptions(legacy='1.13')`", UserWarning)
# This code is factored as a function for clarity only.
n = len(values_dict)
colors = [rgb_color + (self.default_alpha,) for rgb_color in
itertools.islice(itertools.cycle(self.chart_colors), n)]
hist_names = list(values_dict.keys())
values = [v[0] for v in values_dict.values()]
weights = [v[1] for v in values_dict.values() if len(v) > 1]
if n > len(weights) > 0:
raise ValueError("Weights were provided for some columns, but not "
" all, and that's not supported.")
if rug and overlay and n > 1:
warnings.warn("Cannot plot overlaid rug plots; rug=True ignored", UserWarning)
if vargs['density']:
y_label = 'Percent per ' + (unit if unit else 'unit')
percentage = plt.FuncFormatter(lambda x, _: "{:g}".format(100*x))
else:
y_label = 'Count'

if overlay and n > 1:
# Reverse because legend prints bottom-to-top
values = values[::-1]
weights = weights[::-1]
colors = list(colors)[::-1]
if len(weights) == n:
vargs['weights'] = weights
if not side_by_side:
vargs.setdefault('histtype', 'stepfilled')
figure = plt.figure(figsize=(width, height))
plt.hist(values, color=colors, **vargs)
# if rug:
# plt.scatter(values, np.zeros_like(values), marker="|", color=colors)
axis = figure.get_axes()[0]
_vertical_x(axis)
axis.set_ylabel(y_label)
if vargs['density']:
y_label = 'Percent per ' + (unit if unit else 'unit')
percentage = plt.FuncFormatter(lambda x, _: "{:g}".format(100*x))
axis.yaxis.set_major_formatter(percentage)
x_unit = ' (' + unit + ')' if unit else ''
if group is not None and len(self.labels) == 2:
#There's a grouping in place but we're only plotting one column's values
label_not_grouped = [l for l in self.labels if l != group][0]
axis.set_xlabel(label_not_grouped + x_unit, fontsize=16)
else:
y_label = 'Count'

if overlay and n > 1:
# Reverse because legend prints bottom-to-top
values = values[::-1]
weights = weights[::-1]
colors = list(colors)[::-1]
if len(weights) == n:
vargs['weights'] = weights
if not side_by_side:
axis.set_xlabel(x_unit, fontsize=16)
plt.legend(hist_names, loc=2, bbox_to_anchor=(1.05, 1))
type(self).plots.append(axis)
else:
_, axes = plt.subplots(n, 1, figsize=(width, height * n))
if 'bins' in vargs:
bins = vargs['bins']
if isinstance(bins, numbers.Integral) and bins > 76 or hasattr(bins, '__len__') and len(bins) > 76:
# Use stepfilled when there are too many bins
vargs.setdefault('histtype', 'stepfilled')
figure = plt.figure(figsize=(width, height))
plt.hist(values, color=colors, **vargs)
# if rug:
# plt.scatter(values, np.zeros_like(values), marker="|", color=colors)
axis = figure.get_axes()[0]
_vertical_x(axis)
if n == 1:
axes = [axes]
for i, (axis, hist_name, values_for_hist, color) in enumerate(zip(axes, hist_names, values, colors)):
axis.set_ylabel(y_label)
if vargs['density']:
axis.yaxis.set_major_formatter(percentage)
x_unit = ' (' + unit + ')' if unit else ''
if group is not None and len(self.labels) == 2:
#There's a grouping in place but we're only plotting one column's values
label_not_grouped = [l for l in self.labels if l != group][0]
axis.set_xlabel(label_not_grouped + x_unit, fontsize=16)
else:
axis.set_xlabel(x_unit, fontsize=16)
plt.legend(hist_names, loc=2, bbox_to_anchor=(1.05, 1))
if len(weights) == n:
vargs['weights'] = weights[i]
axis.set_xlabel(hist_name + x_unit, fontsize=16)
heights, bins, patches = axis.hist(values_for_hist, color=color, **vargs)
if left_end is not None and right_end is not None:
x_shade, height_shade, width_shade = _compute_shading(heights, bins.copy(), left_end, right_end)
axis.bar(x_shade, height_shade, width=width_shade,
color=self.chart_colors[1], align="edge")
_vertical_x(axis)
if rug:
axis.scatter(values_for_hist, np.zeros_like(values_for_hist), marker="|",
color="black", s=100, zorder=10)
type(self).plots.append(axis)
else:
_, axes = plt.subplots(n, 1, figsize=(width, height * n))
if 'bins' in vargs:
bins = vargs['bins']
if isinstance(bins, numbers.Integral) and bins > 76 or hasattr(bins, '__len__') and len(bins) > 76:
# Use stepfilled when there are too many bins
vargs.setdefault('histtype', 'stepfilled')
if n == 1:
axes = [axes]
for i, (axis, hist_name, values_for_hist, color) in enumerate(zip(axes, hist_names, values, colors)):
axis.set_ylabel(y_label)
if vargs['density']:
axis.yaxis.set_major_formatter(percentage)
x_unit = ' (' + unit + ')' if unit else ''
if len(weights) == n:
vargs['weights'] = weights[i]
axis.set_xlabel(hist_name + x_unit, fontsize=16)
heights, bins, patches = axis.hist(values_for_hist, color=color, **vargs)
if left_end is not None and right_end is not None:
x_shade, height_shade, width_shade = _compute_shading(heights, bins.copy(), left_end, right_end)
axis.bar(x_shade, height_shade, width=width_shade,
color=self.chart_colors[1], align="edge")
_vertical_x(axis)
if rug:
axis.scatter(values_for_hist, np.zeros_like(values_for_hist), marker="|",
color="black", s=100, zorder=10)
type(self).plots.append(axis)

draw_hist(values_dict)

Expand Down