mirror of
https://github.com/3b1b/manim.git
synced 2025-08-05 16:49:03 +00:00
expanded (and fixed) Histogram class
This commit is contained in:
parent
dca4400b09
commit
e9f99f1cff
1 changed files with 210 additions and 169 deletions
|
@ -16,31 +16,44 @@ class Histogram(VMobject):
|
|||
"end_color" : BLUE,
|
||||
"x_scale" : 1.0,
|
||||
"y_scale" : 1.0,
|
||||
"x_labels" : "auto",
|
||||
"x_min" : 0
|
||||
}
|
||||
|
||||
def __init__(self, x_values, y_values, **kwargs):
|
||||
def __init__(self, x_values, y_values, mode = "widths", **kwargs):
|
||||
# mode = "widths" : x_values means the widths of the bars
|
||||
# mode = "posts" : x_values means the delimiters btw the bars
|
||||
|
||||
digest_config(self, kwargs)
|
||||
|
||||
if mode == "widths" and len(x_values) != len(y_values):
|
||||
raise Exception("Array lengths do not match up!")
|
||||
elif mode == "posts" and len(x_values) != len(y_values) + 1:
|
||||
raise Exception("Array lengths do not match up!")
|
||||
|
||||
# preliminaries
|
||||
self.x_values = x_values
|
||||
self.y_values = y_values
|
||||
self.y_values = np.array(y_values)
|
||||
|
||||
self.x_steps = x_values[1:] - x_values[:-1]
|
||||
self.x_min = x_values[0] - self.x_steps[0] * 0.5
|
||||
self.x_posts = (x_values[1:] + x_values[:-1]) * 0.5
|
||||
self.x_max = x_values[-1] + self.x_steps[-1] * 0.5
|
||||
self.x_posts = np.insert(self.x_posts,0,self.x_min)
|
||||
self.x_posts = np.append(self.x_posts,self.x_max)
|
||||
if mode == "widths":
|
||||
self.widths = x_values
|
||||
self.posts = np.cumsum(self.widths)
|
||||
self.posts = np.insert(self.posts, 0, 0)
|
||||
self.posts += self.x_min
|
||||
self.x_max = self.posts[-1]
|
||||
elif mode == "posts":
|
||||
self.posts = x_values
|
||||
self.widths = x_values[1:] - x_values[:-1]
|
||||
self.x_min = self.posts[0]
|
||||
self.x_max = self.posts[-1]
|
||||
else:
|
||||
raise Exception("Invalid mode or no mode specified!")
|
||||
|
||||
self.x_widths = self.x_posts[1:] - self.x_posts[:-1]
|
||||
self.x_mids = 0.5 * (self.posts[:-1] + self.posts[1:])
|
||||
|
||||
self.x_values_scaled = self.x_scale * x_values
|
||||
self.x_steps_scaled = self.x_scale * self.x_steps
|
||||
self.x_posts_scaled = self.x_scale * self.x_posts
|
||||
self.widths_scaled = self.x_scale * self.widths
|
||||
self.posts_scaled = self.x_scale * self.posts
|
||||
self.x_min_scaled = self.x_scale * self.x_min
|
||||
self.x_max_scaled = self.x_scale * self.x_max
|
||||
self.x_widths_scaled = self.x_scale * self.x_widths
|
||||
|
||||
self.y_values_scaled = self.y_scale * self.y_values
|
||||
|
||||
|
@ -50,18 +63,38 @@ class Histogram(VMobject):
|
|||
|
||||
def generate_points(self):
|
||||
|
||||
def empty_string_array(n):
|
||||
arr = []
|
||||
for i in range(n):
|
||||
arr.append("")
|
||||
return arr
|
||||
|
||||
def num_arr_to_string_arr(arr): # converts number array to string array
|
||||
ret_arr = []
|
||||
for x in arr:
|
||||
ret_arr.append(str(x))
|
||||
return ret_arr
|
||||
|
||||
previous_bar = ORIGIN
|
||||
self.bars = []
|
||||
outline_points = []
|
||||
self.x_labels = text_range(self.x_values[0], self.x_max, self.x_steps[0])
|
||||
if self.x_labels == "widths":
|
||||
self.x_labels = num_arr_to_string_arr(self.widths)
|
||||
elif self.x_labels == "mids":
|
||||
print self.x_mids
|
||||
self.x_labels = num_arr_to_string_arr(self.x_mids)
|
||||
elif self.x_labels == "none":
|
||||
self.x_labels = empty_string_array(len(self.widths))
|
||||
|
||||
for (i,x) in enumerate(self.x_values):
|
||||
print self.x_labels
|
||||
|
||||
for (i,x) in enumerate(self.x_mids):
|
||||
|
||||
bar = Rectangle(
|
||||
width = self.x_widths_scaled[i],
|
||||
width = self.widths_scaled[i],
|
||||
height = self.y_values_scaled[i],
|
||||
)
|
||||
t = float(x - self.x_values[0])/(self.x_values[-1] - self.x_values[0])
|
||||
t = float(x - self.x_min)/(self.x_max - self.x_min)
|
||||
bar_color = interpolate_color(
|
||||
self.start_color,
|
||||
self.end_color,
|
||||
|
@ -87,7 +120,6 @@ class Histogram(VMobject):
|
|||
outline_points.append(bar.get_anchors()[1])
|
||||
|
||||
previous_bar = bar
|
||||
|
||||
# close the outline
|
||||
# lower right
|
||||
outline_points.append(bar.get_anchors()[2])
|
||||
|
@ -103,6 +135,15 @@ class Histogram(VMobject):
|
|||
|
||||
|
||||
|
||||
class BuildUpHistogram(Animation):
|
||||
|
||||
def __init__(self, hist, **kwargs):
|
||||
self.histogram = hist
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class FlashThroughHistogram(Animation):
|
||||
|
|
Loading…
Add table
Reference in a new issue