Function Repository Resource:

BarChartRace

Source Notebook

Make an animated bar chart race

Contributed by: Kotaro Okazaki

ResourceFunction["BarChartRace"][{data1,data2.},{label1,label2.}]

makes an animated bar chart race from the datai with corresponding labels labeli, with the top 10 rankings shown.

ResourceFunction["BarChartRace"][{data1,data2.},{label1,label2.},k]

shows the top k rankings.

Details and Options

Each datai is a list of equal length, arranged in the same order as the labeli.
If k is bigger than the number of labels, the number of labels is automatically set.
ResourceFunction["BarChartRace"] supports all the options of BarChart, with the following additional option:
"InterpolatingFrames"22the number of bar charts interpolating between datai and datai+1

Examples

Basic Examples (2) 

Show a few frames from a bar chart race:

In[1]:=
ResourceFunction[
 "BarChartRace"][{{777, 555, 111}, {222, 666, 888}}, {"a", "b", "c"}, "InterpolatingFrames" -> 4]
Out[1]=

Animate a bar chart race with ListAnimate:

In[2]:=
ListAnimate[
 ResourceFunction[
  "BarChartRace"][{{777, 555, 111}, {222, 666, 888}}, {"a", "b", "c"}],
 AnimationRepetitions -> 1,
 AnimationRunning -> False,
 AppearanceElements -> "ResetButton"]
Out[2]=

Applications (1) 

The 2021 FIA F1 World Championship was contested over twenty-two Grand Prix races, and held around the world. Drivers competed for the titles of World Drivers’ Champion. Visualize the rankings over time as bar charts:

In[3]:=
(* data from https://www.formula1.com/ *)
f1y2021 = {{"-", "Bahrain", "Emilia Romagna", "Portugal", "Spain", "Monaco", "Azerbaijan", "France", "Styria", "Austria", "Great Britain", "Hungary", "Belgium", "Netherlands", "Italy", "Russia", "Turkey", "United States ", "Mexico", "Brazil", "Qatar",
     "Saudi Arabia", "Abu Dhabi"}, {"Verstappen", 18, 43, 61, 80, 105,
     105, 131, 156, 182, 185, 187, 199.5, 224.5, 226.5, 244.5, 262.5, 287.5, 312.5, 332.5, 351.5, 369.5, 395.5}, {"Hamilton", 25, 44, 69, 94, 101, 101, 119, 138, 150, 177, 195, 202.5, 221.5, 221.5, 246.5, 256.5, 275.5, 293.5, 318.5, 343.5, 369.5, 387.5}, {"Bottas", 16, 16, 32, 47, 47, 47, 59, 74, 92, 108, 108, 108, 123, 141, 151, 177, 185, 185, 203, 203, 218, 226}, {"Perez", 10, 10, 22, 32, 44, 69, 84, 96, 104, 104, 104, 104, 108, 118, 120,
     135, 150, 165, 178, 190, 190, 190}, {"Sainz", 4, 14, 14, 20, 38, 42, 42, 50, 60, 68, 83, 83.5, 89.5, 97.5, 112.5, 116.5, 122.5, 130.5, 139.5, 145.5, 149.5, 164.5}, {"Norris", 12, 27, 37, 41, 56,
     66, 76, 86, 101, 113, 113, 113, 114, 132, 139, 145, 149, 150, 151, 153, 154, 160}, {"Leclerc", 8, 20, 28, 40, 40, 52, 52, 58, 62, 80, 80, 82, 92, 104, 104, 116, 128, 138, 148, 152, 158, 159}, {"Ricciardo", 6, 14, 16, 24, 24, 26, 34, 34, 40, 50, 50, 56,
     56, 83, 95, 95, 105, 105, 105, 105, 115, 115}, {"Gasly", 0, 6, 7,
     8, 16, 31, 37, 37, 39, 39, 50, 54, 66, 66, 66, 74, 74, 86, 92, 92, 100, 110}, {"Alonso", 0, 1, 5, 5, 5, 13, 17, 19, 20, 26, 38, 38, 46, 50, 58, 58, 58, 60, 62, 77, 77, 81}, {"Ocon", 0, 2, 8, 10,
     12, 12, 12, 12, 12, 14, 39, 42, 44, 45, 45, 46, 46, 46, 50, 60, 72, 74}, {"Vettel", 0, 0, 0, 0, 10, 28, 30, 30, 30, 30, 30, 35, 35, 35, 35, 35, 36, 42, 42, 43, 43, 43}, {"Stroll", 1, 5, 5, 5, 9,
     9, 10, 14, 14, 18, 18, 18, 18, 24, 24, 26, 26, 26, 26, 34, 34, 34}, {"Tsunoda", 2, 2, 2, 2, 2, 8, 8, 9, 9, 10, 18, 18, 18, 18, 18, 18, 20, 20, 20, 20, 20, 32}, {"Russell", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 13, 13, 15, 16, 16, 16, 16, 16, 16, 16, 16}, {"Räikkönen", 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 6, 6,
     6, 10, 10, 10, 10, 10}, {"Latifi", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}, {"Giovinazzi", 0, 0, 0, 0, 1,
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3}, {"Schumacher", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 0}, {"Kubica", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {"Mazepin", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
In[4]:=
itemLabels = Style[#, Bold, Black] & /@ f1y2021[[2 ;;, 1]]; 
itemData = Transpose@f1y2021[[2 ;;, 2 ;;]];
In[5]:=
(* Display the top 10 *)
interpolatingframes = 22; (*default*)
bc = ResourceFunction["BarChartRace"][itemData, itemLabels, 10,
   "InterpolatingFrames" -> interpolatingframes,
   ChartStyle -> {
RGBColor[1., 0.2, 1.], 
RGBColor[0.4, 0.8, 0.], 
RGBColor[0.4, 1., 1.], 
RGBColor[0.4, 0.4, 0.2], 
RGBColor[1., 0.2, 0.4], 
RGBColor[0.4, 0.6, 0.4], 
RGBColor[0.8, 0.4, 0.4], 
RGBColor[1., 0.8, 0.2], 
RGBColor[1., 0.6, 1.], 
RGBColor[0.4, 0., 0.6], 
RGBColor[1., 0.4, 0.6], 
RGBColor[0.6, 1., 0.6], 
RGBColor[1., 1., 0.4], 
RGBColor[0.4, 0.8, 1.], 
RGBColor[0.6, 0.8, 0.8], 
RGBColor[0.4, 0.6, 1.], 
RGBColor[0.6, 1., 0.2], 
RGBColor[1., 0.4, 0.], 
RGBColor[1., 1., 0.8], 
RGBColor[0.2, 0.4, 0.6], 
RGBColor[0.6, 0.2, 0.4]},
   ChartBaseStyle -> EdgeForm[Transparent],
   FrameLabel -> {{None, None}, {None, Style["The 2021 FIA F1 Driver Standings", 20, Bold]}}, ImageSize -> Large];
In[6]:=
(* Display the locations *)
locations = Style[#, 24, Bold, Black] & /@ f1y2021[[1, 2 ;;]];
bc2 = Table[
   Overlay[{bc[[i]], locations[[Ceiling[i/(interpolatingframes + 2)] + 1]]}, Alignment -> {Right, Bottom}], {i, 1, Length@bc}];
bc2[[1]] = Overlay[{bc[[1]], locations[[1]]}, Alignment -> {Right, Bottom}];
In[7]:=
(* Animate *)
ListAnimate[bc2,
 AnimationRepetitions -> 1, AnimationRunning -> False, AppearanceElements -> "ResetButton"]
Out[7]=

Publisher

Kotaro Okazaki

Version History

  • 1.0.0 – 25 July 2022

Related Resources

License Information