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()