### Verify ImageMagick installation Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/installation.md Checks the installed version of ImageMagick to confirm it is correctly installed and added to the system's PATH. ```Shell magick -version ``` -------------------------------- ### Verify ffmpeg installation Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/installation.md Checks the installed version of ffmpeg to confirm it is correctly installed and added to the system's PATH. ```Shell ffmepg -version ``` -------------------------------- ### Install bar_chart_race with pip Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/installation.md Installs the bar_chart_race library using the pip package manager. ```Shell pip install bar_chart_race ``` -------------------------------- ### Install bar_chart_race with pip Source: https://github.com/dexplo/bar_chart_race/blob/master/README.md Installs the bar_chart_race library using the pip package manager. ```Shell pip install bar_chart_race ``` -------------------------------- ### Install bar_chart_race with conda Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/installation.md Installs the bar_chart_race library using the conda package manager from the conda-forge channel. ```Shell conda install -c conda-forge bar_chart_race ``` -------------------------------- ### Install bar_chart_race with conda Source: https://github.com/dexplo/bar_chart_race/blob/master/README.md Installs the bar_chart_race library using the conda package manager from the conda-forge channel. ```Shell conda install -c conda-forge bar_chart_race ``` -------------------------------- ### Setting Colormap and Limiting Bars (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Sets the colormap and limits the number of bars displayed. This example demonstrates how color repetition can occur if the number of unique bars appearing over time exceeds the colormap's colors, even if `n_bars` is less than the colormap size. ```python df.bcr.bar_chart_race(cmap='accent', n_bars=7) ``` -------------------------------- ### Customizing Bar Chart Race with Fixed Order/Max and Summary Function (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/index.md This example demonstrates advanced customization options, including fixing the bar order and maximum x-value throughout the animation. It also defines a custom `period_summary_func` to display dynamic information about the leading bar and its lead over the second bar. ```python def period_summary(values, ranks): top2 = values.nlargest(2) leader = top2.index[0] lead = top2.iloc[0] - top2.iloc[1] s = f'{leader} by {lead:.0f}' return {'s': s, 'x': .99, 'y': .03, 'ha': 'right', 'size': 8} df_baseball = bcr.load_dataset('baseball').pivot(index='year', columns='name', values='hr') df_baseball.bcr.bar_chart_race( period_length=1000, fixed_max=True, fixed_order=True, n_bars=10, period_summary_func=period_summary, period_label={'x': .99, 'y': .1}, period_template='Season {x:,.0f}', title='Top 10 Home Run Hitters by Season Played') ``` -------------------------------- ### Creating a Basic Bar Chart Race (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Imports the bar_chart_race library, loads a sample dataset ('covid19_tutorial'), and generates a basic horizontal bar chart race animation using default settings. The output is an HTML string or embedded video in Jupyter. ```python import bar_chart_race as bcr df = bcr.load_dataset('covid19_tutorial') df.bcr.bar_chart_race() ``` -------------------------------- ### Customizing Plot Properties (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Demonstrates setting various plot properties including figure size, DPI, disabling bar labels, customizing the period label's position and style, and adding a main title to the animation. ```python df.bcr.bar_chart_race(figsize=(5, 3), dpi=100, label_bars=False, period_label={'x': .99, 'y': .1, 'ha': 'right', 'color': 'red'}, title='COVID-19 Deaths by Country') ``` -------------------------------- ### Save Bar Chart Race Animation to File (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Illustrates how to save the generated bar chart race animation directly to a file on disk. It calls the `df.bcr.bar_chart_race` method, providing the desired output filename as the first argument. ```python df.bcr.bar_chart_race('docs/videos/covid19.mp4', figsize=(5, 3)) ``` -------------------------------- ### Animating Long Data with Custom Summary (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/data_preparation.md Demonstrates preparing 'long' data with `steps_per_period=1` for direct animation and includes a custom function `period_summary` to generate text overlays for each frame. Finally, it calls `bcr.bar_chart_race` to create the animation. ```python df_values, df_ranks = bcr.prepare_long_data(df_baseball, index='year', columns='name',\ values='hr', steps_per_period=1,\ orientation='h', sort='desc') def period_summary(values, ranks): top2 = values.nlargest(2) leader = top2.index[0] lead = top2.iloc[0] - top2.iloc[1] s = f'{leader} by {lead:.0f}' return {'s': s, 'x': .95, 'y': .07, 'ha': 'right', 'size': 8} bcr.bar_chart_race(df_values, period_length=1000,\ fixed_max=True, fixed_order=True, n_bars=10,\ figsize=(5, 3), period_fmt='Season {x:,.0f}',\ title='Top 10 Home Run Hitters by Season Played') ``` -------------------------------- ### Formatting Period Label with String Format (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Demonstrates using a string format specifier with `{x}` to format the period label when the index is numerical (after dropping the original date index), combined with period interpolation. ```python bcr.bar_chart_race(df.reset_index(drop=True), interpolate_period=True, period_fmt='Index value - {x:.2f}') ``` -------------------------------- ### Loading and Displaying Long Data (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/data_preparation.md Loads the 'baseball' dataset using `bcr.load_dataset` and displays the resulting pandas DataFrame, illustrating the 'long' data format where different data types are in separate columns. ```python df_baseball = bcr.load_dataset('baseball') df_baseball ``` -------------------------------- ### Loading and Displaying Wide Data (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/data_preparation.md Loads the last three rows of the 'covid19_tutorial' dataset using `bcr.load_dataset` and displays the resulting pandas DataFrame, demonstrating the 'wide' data format required by `bar_chart_race`. ```python df = bcr.load_dataset('covid19_tutorial').tail(3) df ``` -------------------------------- ### Create and save a bar chart race (Matplotlib) Source: https://github.com/dexplo/bar_chart_race/blob/master/README.md Demonstrates creating a horizontal bar chart race animation using the bar_chart_race function with numerous customization options and saving it to a file. ```Python import bar_chart_race as bcr df = bcr.load_dataset('covid19_tutorial') bcr.bar_chart_race( df=df, filename='../docs/images/covid19_horiz.gif', orientation='h', sort='desc', n_bars=8, fixed_order=False, fixed_max=True, steps_per_period=20, period_length=500, end_period_pause=0, interpolate_period=False, period_label={'x': .98, 'y': .3, 'ha': 'right', 'va': 'center'}, period_template='%B %d, %Y', period_summary_func=lambda v, r: {'x': .98, 'y': .2, 's': f'Total deaths: {v.sum():,.0f}', 'ha': 'right', 'size': 11}, perpendicular_bar_func='median', colors='dark12', title='COVID-19 Deaths by Country', bar_size=.95, bar_textposition='inside', bar_texttemplate='{x:,.0f}', bar_label_font=7, tick_label_font=7, tick_template='{x:,.0f}', shared_fontdict=None, scale='linear', fig=None, writer=None, bar_kwargs={'alpha': .7}, fig_kwargs={'figsize': (6, 3.5), 'dpi': 144}, filter_column_colors=False) ``` -------------------------------- ### Customize bar chart race with fixed order/max and summary function Source: https://github.com/dexplo/bar_chart_race/blob/master/README.md Shows how to customize a bar chart race by defining a custom summary function, fixing the maximum x-value and bar order, and using a different dataset. ```Python def period_summary(values, ranks): top2 = values.nlargest(2) leader = top2.index[0] lead = top2.iloc[0] - top2.iloc[1] s = f'{leader} by {lead:.0f}' return {'s': s, 'x': .99, 'y': .03, 'ha': 'right', 'size': 8} df_baseball = bcr.load_dataset('baseball').pivot(index='year', columns='name', values='hr') df_baseball.bcr.bar_chart_race( period_length=1000, fixed_max=True, fixed_order=True, n_bars=10, period_summary_func=period_summary, period_label={'x': .99, 'y': .1}, period_template='Season {x:,.0f}', title='Top 10 Home Run Hitters by Season Played') ``` -------------------------------- ### Retrieve HTML Data from Bar Chart Race Output (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Shows how to obtain the raw HTML string generated by `bar_chart_race` when not in a Jupyter environment or when needing the HTML directly. It calls the function and accesses the `.data` attribute of the returned object. ```python html = bcr.bar_chart_race(df) html.data # very long string of HTML ``` -------------------------------- ### Setting Shared Font Properties (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Applies a common set of font properties (family, weight, color) to all text elements in the plot using the `shared_fontdict` parameter. ```python df.bcr.bar_chart_race(title='COVID-19 Deaths by Country', shared_fontdict={'family': 'Helvetica', 'weight': 'bold', 'color': 'rebeccapurple'}) ``` -------------------------------- ### Customizing Bar Appearance (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Passes a dictionary of keyword arguments to the underlying Matplotlib bar plotting function to customize bar appearance, such as transparency (`alpha`), edge color (`ec`), and line width (`lw`). ```python df.bcr.bar_chart_race(bar_kwargs={'alpha': .2, 'ec': 'black', 'lw': 3}) ``` -------------------------------- ### Using a Custom Matplotlib Figure (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Creates a custom Matplotlib figure and axes, modifies the axes properties (like face color), and then passes the figure object to `bar_chart_race` to render the animation within the predefined figure. ```python import matplotlib.pyplot as plt fig, ax = plt.subplots(figsize=(5, 2), dpi=120) ax.set_facecolor((0, 0, 1, .3)) df.bcr.bar_chart_race(n_bars=3, fig=fig) ``` -------------------------------- ### Adjusting Animation Smoothness (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Controls the number of intermediate frames generated between each data period to smooth the transition, setting it to 3 steps per period. ```python df.bcr.bar_chart_race(steps_per_period=3) ``` -------------------------------- ### Interpolating Period Labels (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Enables linear interpolation of the period label displayed on the plot, showing fractional values between data periods rather than only the exact period value. ```python df.bcr.bar_chart_race(interpolate_period=True) ``` -------------------------------- ### Sorting Bars in Ascending Order (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Creates a bar chart race where the bars are sorted in ascending order based on their values, overriding the default descending sort order. ```python df.bcr.bar_chart_race(sort='asc') ``` -------------------------------- ### Setting Steps and Period Length (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Adjusts both the number of steps between periods (20) and the duration each period is displayed (200 milliseconds), controlling the animation's speed and smoothness. ```python df.bcr.bar_chart_race(steps_per_period=20, period_length=200) ``` -------------------------------- ### Create Bar Chart Race with Matplotlib Subplots (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Demonstrates how to generate a bar chart race animation within a specific subplot of a Matplotlib figure. It sets up a 2x2 subplot grid, plots data on the other subplots, and then uses `df.bcr.bar_chart_race` targeting the first subplot. ```python from matplotlib import dates fig, ax_array = plt.subplots(2, 2, figsize=(8, 4), dpi=120, tight_layout=True) ax1, ax2, ax3, ax4 = ax_array.flatten() fig.suptitle('Animation in First Axes', y=1) ax2.plot(df) ax2.xaxis.set_major_locator(dates.DayLocator([3, 7, 12])) ax3.bar(df.index, df.median(axis=1)) ax3.xaxis.set_major_locator(dates.DayLocator([3, 7, 12])) ax4.pie(df.iloc[-1], radius=1.5, labels=df.columns) df.bcr.bar_chart_race(n_bars=3, fig=fig) ``` -------------------------------- ### Creating a Bar Chart Race with Matplotlib in Python Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/index.md This snippet demonstrates how to generate an animated horizontal bar chart race using the `bar_chart_race` function with Matplotlib. It loads a sample dataset and applies various parameters to customize the appearance, sorting, number of bars, period display, and output filename. ```python import bar_chart_race df = bcr.load_dataset('covid19_tutorial') df.bcr.bar_chart_race( filename='../docs/images/covid19_horiz.gif', orientation='h', sort='desc', n_bars=8, fixed_order=False, fixed_max=True, steps_per_period=20, period_length=500, end_period_pause=0, interpolate_period=False, period_label={'x': .98, 'y': .3, 'ha': 'right', 'va': 'center'}, period_template='%B %d, %Y', period_summary_func=lambda v, r: {'x': .98, 'y': .2, 's': f'Total deaths: {v.sum():,.0f}', 'ha': 'right', 'size': 11}, perpendicular_bar_func='median', colors='dark12', title='COVID-19 Deaths by Country', bar_size=.95, bar_textposition='inside', bar_texttemplate='{x:,.0f}', bar_label_font=7, tick_label_font=7, tick_template='{x:,.0f}', shared_fontdict=None, scale='linear', fig=None, writer=None, bar_kwargs={'alpha': .7}, fig_kwargs={'figsize': (6, 3.5), 'dpi': 144}, filter_column_colors=False) ``` -------------------------------- ### Controlling Label Sizes (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Adjusts the font sizes for bar labels, tick labels on the axis, and the main plot title using specific size parameters. ```python df.bcr.bar_chart_race(bar_label_size=4, tick_label_size=5, title='COVID-19 Deaths by Country', title_size='smaller') ``` -------------------------------- ### Embed bar chart race in Jupyter Notebook Source: https://github.com/dexplo/bar_chart_race/blob/master/README.md Generates a bar chart race animation and embeds it directly into a Jupyter Notebook output by setting the filename parameter to None. ```Python bcr.bar_chart_race(df=df, filename=None) ``` -------------------------------- ### Preparing Wide Data for Animation (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/data_preparation.md Processes a 'wide' pandas DataFrame using `bcr.prepare_wide_data` to generate linearly interpolated values and ranks for smooth animation transitions. It returns two DataFrames: one for bar values and one for bar ranks. ```python df_values, df_ranks = bcr.prepare_wide_data(df, steps_per_period=4, \ orientation='h', sort='desc') ``` -------------------------------- ### Filtering Column Colors for Limited Bars (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Sets the colormap, limits the number of bars, and enables `filter_column_colors` to assign colors only to columns that actually appear in the animation, reducing color repetition when `n_bars` is used. ```python df.bcr.bar_chart_race(cmap='accent', n_bars=7, filter_column_colors=True) ``` -------------------------------- ### Preparing Long Data for Animation (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/data_preparation.md Transforms a 'long' pandas DataFrame into the 'wide' format required for animation using `bcr.prepare_long_data`. It pivots the data based on specified index, columns, and values, then prepares it for smooth transitions, returning value and rank DataFrames. ```python df_values, df_ranks = bcr.prepare_long_data(df_baseball, index='year', columns='name',\ values='hr', steps_per_period=5) df_values.head(16) ``` -------------------------------- ### Formatting Period Label with Date Directive (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Formats the period label displayed on the plot using a date format string, allowing customization of how date/time index values are shown. ```python df.bcr.bar_chart_race(period_fmt='%b %-d, %Y') ``` -------------------------------- ### Embedding a Bar Chart Race in Jupyter Notebook (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/index.md This snippet shows how to display the generated bar chart race animation directly within the output of a Jupyter Notebook cell. By setting the `filename` parameter to `None`, the animation is rendered inline instead of being saved to a file. ```python bcr.bar_chart_race(df=df, filename=None) ``` -------------------------------- ### Adding a Custom Period Summary Text (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Defines a custom function that calculates a summary statistic (total deaths) for the current period and adds it as text to the plot using the `period_summary_func` parameter. ```python def summary(values, ranks): total_deaths = int(round(values.sum(), -2)) s = f'Total Deaths - {total_deaths:,.0f}' return {'x': .99, 'y': .05, 's': s, 'ha': 'right', 'size': 8} df.bcr.bar_chart_race(period_summary_func=summary) ``` -------------------------------- ### Displaying Prepared Long Data Ranks (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/data_preparation.md Displays the head of the `df_ranks` DataFrame generated by `bcr.prepare_long_data`. This DataFrame contains the numerical rankings used for positioning bars during the animation. ```python df_ranks.head(16) ``` -------------------------------- ### Fixing the Order of Specific Bars (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Fixes the display order of the bars throughout the animation to a specific list of column names, preventing them from reordering based on value. ```python df.bcr.bar_chart_race(fixed_order=['Iran', 'USA', 'Italy', 'Spain', 'Belgium']) ``` -------------------------------- ### Setting Vertical Bar Orientation (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Generates a bar chart race animation with vertical bars instead of the default horizontal orientation by setting the `orientation` parameter to 'v'. ```python df.bcr.bar_chart_race(orientation='v') ``` -------------------------------- ### Setting Bar Colormap (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Sets the colormap used for the bars in the animation to a specified Matplotlib or Plotly colormap name, such as 'antique'. ```python df.bcr.bar_chart_race(cmap='antique') ``` -------------------------------- ### Limiting the Number of Displayed Bars (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Limits the number of bars displayed in the animation to the top N bars (in this case, 6) based on their value in each frame. ```python df.bcr.bar_chart_race(n_bars=6) ``` -------------------------------- ### Adding a Perpendicular Mean Bar (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Adds a vertical bar perpendicular to the main horizontal bars, positioned at the mean value of the data for the current period, using a string name for a pandas aggregation function. ```python df.bcr.bar_chart_race(perpendicular_bar_func='mean') ``` -------------------------------- ### Adding a Perpendicular Custom Function Bar (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Adds a vertical bar perpendicular to the main bars, positioned at a value calculated by a user-defined function (here, the 90th percentile) for the current period. ```python def func(values, ranks): return values.quantile(.9) df.bcr.bar_chart_race(perpendicular_bar_func=func) ``` -------------------------------- ### Fixing the Maximum Axis Value (Python) Source: https://github.com/dexplo/bar_chart_race/blob/master/docs/tutorial.md Sets a fixed maximum value for the axis across all frames of the animation, equal to the largest overall value in the entire dataset, preventing the axis from scaling dynamically. ```python df.bcr.bar_chart_race(fixed_max=True) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.