Julkar9
Julkar9

Reputation: 2110

Bar chart race with bars overtaking each other in python

How do I make bars over take each others in a sorted bar chart race ?
Basically the same as this question, just in python.

My minimal bar chart race code

import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt

import matplotlib.animation as animation

data = np.abs(np.random.randn(200, 3))
ind = ['a', 'b', 'c']
data = pd.DataFrame(data, columns=ind)

fig, ax = plt.subplots(1, 1, figsize=(16, 6.5))
# ax.barh(ind, data[0, :], color=['r', 'g', 'b'])
def getTopXY(data, i):
    top = data.iloc[i, :].T.sort_values(ascending=False)[::-1]
    return top.values, top.index

def update(i):
    ax.clear()
    X, Y = getTopXY(data, i)
    ax.barh(Y, X, color=['r', 'g', 'b'])
    
ani = animation.FuncAnimation(fig,
                              update,
                              frames=len(data),
                              interval=10,
                              blit=False)
plt.show()

Upvotes: 1

Views: 604

Answers (1)

Sander van den Oord
Sander van den Oord

Reputation: 12818

You can try:
github.com/dexplo/bar_chart_race

You can install via pip, but that's an older version 0.1.0:
pip install bar_chart_race

But it's better to install the latest version. You can do that as follows:
pip install git+https://github.com/dexplo/bar_chart_race

Upvotes: 1

Related Questions