#+title: Bar Chart Race * Initial imports :PROPERTIES: :header-args: :session barChart :results output :exports both :END: #+BEGIN_SRC python import numpy as np print("imported!") #+END_SRC #+RESULTS: : imported! * Loading the Dataset :PROPERTIES: :header-args: :session barChart :results output :END: #+BEGIN_SRC python from pathlib import Path file_name = Path('Sweeteners-Consumption.csv') root_folder = Path('data') file_path = root_folder / file_name print(file_path) #+END_SRC #+RESULTS: : data/Sweeteners-Consumption.csv #+BEGIN_SRC python import pandas as pd dataFrame = pd.read_csv(file_path, header=0, index_col="Calendar Year") # dataFrame = dataFrame.set_index('Calendar Year') dataFrame = dataFrame.drop('Total Sweeteners' ,axis=1) #+END_SRC #+RESULTS: #+BEGIN_SRC python :results output table print(dataFrame.head(10)) #+END_SRC #+RESULTS: #+begin_example Raw Sugar Refined Sugar High-corn ... Total Honey Other Syrups Calendar Year ... 1966 10235 9565 0 ... 1367 98 69 1967 10474 9789 3 ... 1415 89 50 1968 10656 9959 15 ... 1489 90 70 1969 10950 10234 33 ... 1553 101 61 1970 11163 10433 56 ... 1629 103 51 1971 11345 10603 86 ... 1731 93 52 1972 11487 10736 121 ... 1863 105 52 1973 11429 10681 218 ... 2092 95 53 1974 10945 10229 295 ... 2262 75 43 1975 10302 9628 527 ... 2515 108 43 [10 rows x 8 columns] #+end_example * Barchart animation :PROPERTIES: :header-args: :session barChart :results output :END: #+BEGIN_SRC python print(dataFrame.iloc[0].to_list()) #+END_SRC #+RESULTS: : [10235, 9565, 0, 952, 415, 1367, 98, 69] #+BEGIN_SRC python :results none from manim import * class AnimatedBarChart(Scene): def construct(self): bar_names = [ 'Raw Sugar','Refined Sugar','High-corn','Glucose','Dextrose','Total Corn','Honey','Other', ] self.camera.background_color = WHITE config.renderer='cairo' config.quality='low_quality' chart = BarChart( values = dataFrame.iloc[0].to_list(), bar_names=bar_names, y_range=[0,13000, 1000], y_length=7.0, x_length=10, x_axis_config={ "font_size":20, 'label_direction':DOWN, # 'stroke_color' : RED, # 'font_color' : BLACK, # 'color':BLACK }, y_axis_config={ "font_size":25, }, axis_config = { 'color':BLACK, # 'label_color':RED, 'tip_shape': StealthTip, } ) #================================== # this section is to set the axis tick color. The best solution I found was the following for loops # from this reddit post: # https://www.reddit.com/r/manim/comments/x7iqzp/colors_of_axis_text_for_barchart/ for tick in chart.get_x_axis(): tick.set_color(BLACK) for tick in chart.get_y_axis(): tick.set_color(BLACK) #================================== current_year =dataFrame.index[0] date_text = Integer( number=current_year, group_with_commas=False, ).set_color(BLACK).scale(2.5) date_text.move_to(UP*3) date_text.add_updater(lambda d: d.set_value(current_year)) self.add(chart, date_text) for i, row in enumerate(dataFrame.itertuples()): weights = list(row[1:]) #first item is the date current_year = row[0] if i == 0: continue # elif i > 5: #comment out to render the full animation # break self.play(chart.animate.change_bar_values(weights), # date_text.animate.become(new_date, match_center=True, match_height=True), rate_func=linear, run_time=0.7) scene = AnimatedBarChart() scene.render() #+END_SRC #+RESULTS: #+begin_example Animation 0: Create(Tex('1966')): 0% 0/60 [00:00 100 files). Therefore, manim has removed the 14 oldest file(s). You can change this behaviour by changing max_files_cached in config. INFO Rendered AnimatedBarChart scene.py:241 Played 26 animations #+end_example