You are currently viewing How to make racing bar charts in Python

How to make racing bar charts in Python

Data visualization is a key part of conveying information in a clear way. Bar charts are a popular way to show category data, and animations can make them even better. In this blog, we will look at how to make racing bar charts in Python. It show how values change over time and are an interesting way to see how data is changing.

Application of racing bar charts in python

Here are the applications of racing bar charts in bullet points:

  • Sports rankings: Racing bar charts can be used to show how the rankings of sports teams or players change over time, showing how they are doing and how they compare to each other.
  • Social media analytics: Racing bar charts are a good way to show how the number of fans or engagement measures for different accounts on Instagram or Twitter are growing.
  • Financial data analysis: Racing bar charts can show how stock prices or market shares change over time, giving a moving picture of financial data.
  • Competitive analysis: Racing bar charts make it easy to see how rivals are doing in terms of market share, sales, or customer happiness.
  • Election results: Racing bar charts can be used to show how votes for political candidates change over time. This gives a live look at how the election turned out.
  • Academic rankings: Racing bar charts can be used to show how universities, colleges, or schools rank based on things like how well they do in school or how much study they do.

Implementation of racing bar charts in python

The sjvisualizer library, which offers practical tools for data visualization, will be used to create racing bar charts in Python. Make sure you have the sjvisualizer package set up before you start. Using the following instructions, you can set it up:

But before that you have to download this whl file from here. And place in your folder where your Jupyter notebook file is present.

https://github.com/SjoerdTilmans/sjvisualizer/blob/main/dist/sjvisualizer-0.0.7-py2.py3-none-any.whl

You can download the dataset from google drive

https://drive.google.com/drive/folders/1BafDv-Dy-13Z0frzYwrYvFEhZqifGq8z?usp=sharing

!pip install "sjvisualizer-0.0.7-py2.py3-none-any.whl"

We can move forward with the implementation when the library has been installed. Let’s import the required modules first.

from sjvisualizer import Canvas
from sjvisualizer import DataHandler
from sjvisualizer import BarRace
import time
import json

The key function that we design next will be in charge of creating and displaying the racing bar chart. The function will receive the optional parameters FPS (frames per second) and duration (the duration of the animation in seconds). These factors determine the speed and length of the racing effect.

def main(fps=60, duration=0.35):
    number_of_frames = duration * 60 * fps

    # Load colors
    with open('colors.json') as f:
        colors = json.load(f)

    # Load data
    df = DataHandler.DataHandler(excel_file="Countries that smoke the most.xlsx", number_of_frames=number_of_frames).df

    # Create canvas
    canvas = Canvas.canvas()

    # Add bar chart
    bar_chart = BarRace.bar_race(canvas=canvas.canvas, df=df, colors=colors)
    canvas.add_sub_plot(bar_chart)

    # Add static text
    canvas.add_title("Top Smoking countries", color=(0, 132, 255))
    canvas.add_sub_title("By Number of Peoples", color=(0, 132, 255))

    # Add time indication
    canvas.add_time(df=df, time_indicator="month")

    # Save colors for next run
    with open("colors.json", "w") as file:
        json.dump(colors, file, indent=4)

    # Render the animation
    canvas.play(fps=fps)

if __name__ == "__main__":
    main()

In the main function, we begin by figuring out how many frames there will be using the given period and frames per second. The colour information is then loaded from a JSON file which is given down below.

https://drive.google.com/drive/folders/1BafDv-Dy-13Z0frzYwrYvFEhZqifGq8z?usp=sharing

  • The data is then loaded from an Excel file using the DataHandler class. . The number of frames for the racing effect are specified using the number_of_frames option, which is given.
  • We use the Canvas class to construct a canvas object after loading the data. Our visualisation components are housed in the canvas.
  • Using the BarRace class, we add the racing bar chart on the canvas. The colours parameter specifies the colour scheme for the bars, while the df parameter represents the data frame holding the values for each category across time.
  • Using the add_title and add_sub_title methods, we use static text components like the title and subtitle to improve the visualisation.
  • In order to display the current time frame being exhibited in the animation, we additionally add a time indicator using the add_time function.
  • Finally, we render the animation using the play function of the canvas and store the updated colors to the JSON file for further runs.

Second Implementation of advanced racing bar charts in Python

First of all the libraries are imported

from sjvisualizer import Canvas
from sjvisualizer import DataHandler
from sjvisualizer import BarRace
from sjvisualizer import Date
import time
import json

Next the main function is defined. The main difference is in canvas plot settings, static text elements, and rectangle shape is created on the entire canvas area

def main(fps = 60, duration = 0.35):

    number_of_frames = duration*60*fps

    # load colors
    with open('colors.json') as f:
        colors = json.load(f)

    df = DataHandler.DataHandler(excel_file="Insta.xlsx", number_of_frames=number_of_frames).df

    canvas = Canvas.canvas()

    # add bar chart
    bar_chart = BarRace.bar_race(canvas=canvas.canvas, df=df, colors=colors, height=720, width=720, x_pos=175, y_pos=200)
    canvas.add_sub_plot(bar_chart)

    # add static text
    static_text = canvas.canvas.create_text(720/2 + 150, 100, text="Most Followed Instagram Accounts", font=("Purisa", 30))

    # add time indication
    date = Date.date(canvas=canvas.canvas, x_pos=250, y_pos=1000, width=0, height=50, time_indicator="month", df=df)
    canvas.add_sub_plot(date)

    square = canvas.canvas.create_rectangle(0, 0, 1080, 1080)

    # save colors for next run
    with open("colors.json", "w") as file:
        json.dump(colors, file, indent=4)

    canvas.play(fps=fps)

if __name__ == "__main__":
    main()

Conclusion

In this blog, we have seen a great approach to see how data has evolved over time is via racing bar charts in python. Making dynamic and engaging racing bar charts using Python and the sjvisualizer package is simple. This course demonstrated how to create a racing bar chart and its potential applications. Now that you are aware of race bar charts, you may use them to your own statistics to enhance the visual appeal of your data visualizations.

If you like the article and would like to support me, make sure to: