bar_chart_race

repository·master·Indexed 23 days ago

https://github.com/dexplo/bar_chart_race

A Python library for creating animated bar and line chart races using matplotlib or plotly. It integrates with pandas DataFrames in 'wide' format and provides tools like prepare_wide_data and prepare_long_data for data transformation. The library supports custom period summaries, perpendicular bars, and exports to video (via ffmpeg) or GIF (via ImageMagick).

Tokens
4.4K
Snippets
18
Records
27
Agent score
74%

What's inside bar_chart_race

  1. Prepare data for bar chart races

    master

    To create a bar chart race, your data must be in a 'wide' format within a pandas DataFrame:

    • Rows: Represent a single period of time.
    • Columns: Each column holds the value for a particular category.
    • Index: Contains the time component (optional).

    Example structure: A DataFrame where the index is a date, columns are country names, and values are death counts.

  2. Create a basic bar chart race

    master

    Use the bar_chart_race function to create animations. If you are in a Jupyter Notebook, the animation is automatically embedded as a video. You can use load_dataset to access sample DataFrames.

    import bar_chart_race
    df = bcr.load_dataset('covid19_tutorial')
    df.bcr.bar_chart_race()
  3. Save animations to disk

    master

    To save the animation as a video file (e.g., .mp4, .mov) or a .gif, pass the filename as the first argument to bar_chart_race.

    Requirements:

    • For video formats: Install ffmpeg.
    • For GIF format: Install ImageMagick.
    # Save as MP4
    df.bcr.bar_chart_race('output_video.mp4', figsize=(5, 3))

    If you are not in a Jupyter Notebook but have IPython installed, the function returns an HTML object. You can access the raw HTML string via the .data attribute.

    df.bcr.bar_chart_race('docs/videos/covid19.mp4', figsize=(5, 3))
  4. Configure animation pauses and titles

    master

    New parameters in version 0.2 allow for better animation control and title styling:

    • end_period_pause: Sets a pause (in milliseconds) at the end of each period.
    • title: Can be a string or a dictionary. When using a dictionary, use the 'label' key for the title text, and other keys to control text properties.
  5. Customize bar labels and text formatting

    master

    Use the following parameters to control how bar labels appear:

    • bar_textposition: Specifies the position of the bar label. Options are 'outside', 'inside', or None.
    • bar_texttemplate: Allows for label formatting using either a string or a function.