【问题标题】:Bar chart race with bars overtaking each other in python条形图竞赛,条形图在 python 中相互超越
【发布时间】:2021-12-08 23:11:13
【问题描述】:

如何让条形图在排序条形图竞赛中相互超越?
和这个question基本一样,只是在python中。

我的最小条形图比赛代码

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

【问题讨论】:

标签: python matplotlib animation bar-chart


【解决方案1】:

你可以试试:
github.com/dexplo/bar_chart_race

您可以通过 pip 安装,但这是旧版本 0.1.0:
pip install bar_chart_race

但最好安装最新版本。你可以这样做:
pip install git+https://github.com/dexplo/bar_chart_race

【讨论】:

    猜你喜欢
    • 2020-12-11
    • 2023-01-21
    • 2018-11-09
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多