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

Fix crash due to bad rounding in BarChart when no y_step was provided and the max y value was close to 0 #3935

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion manim/mobject/graphing/probability.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,13 @@ def __init__(
]

elif len(y_range) == 2:
y_range = [*y_range, round(max(self.values) / y_length, 2)]
y_step_target = max(self.values) / y_length
# Round the step size to a suitable (ceil) number of decimal places based on its magnitude (log10(x)). Avoids step size of 0.
# At least 2 decimals will be used in the case of large numbers.
y_range = [
*y_range,
round(y_step_target, max(2, int(np.ceil(-np.log10(y_step_target))))),
]

if x_length is None:
x_length = min(len(self.values), config.frame_width - 2)
Expand Down
15 changes: 15 additions & 0 deletions tests/module/mobject/graphing/test_bar_chart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from manim.mobject.graphing.probability import BarChart


def test_values_close_to_zero():
"""Checks that BarChart supports values/heights close to zero without crashing if ."""
values = [1 / 10000 for _ in range(8)]
names = list(range(len(values)))
chart = BarChart(
values=values,
bar_names=["one", "two", "three", "four", "five"],
Comment on lines +7 to +10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to pass names to bar_names?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thank you, I will fix that.

y_range=[0, 2 / 10000],
y_length=6,
x_length=10,
x_axis_config={"font_size": 36},
)
Loading