使用Python制作条形图比赛轴格式化

3
我有一个数据集,其中有三列:“初始条件”、“给定初始条件发生A的概率”和“给定初始条件发生B的概率”。我想在Python中制作一个条形图动画gif,以展示随着初始条件的变化,柱状图(代表A和B的概率)的进展情况。我尝试过这样做,但是表示条形图值的x轴只显示0或1。我无法将其格式化为两个小数位。如果有人能帮助我,那将非常有帮助。以下是数据样本。

enter image description here

这是我使用的代码。

import pandas as pd
import matplotlib.pyplot as plt
import bar_chart_race as bcr

# Load the data
data = pd.read_csv('data.csv')

# Set the index to the starting capital of A
data = data.set_index('Initial captial of A')

# Create the animated bar chart race
bcr.bar_chart_race(
    df=data,
    filename='race.gif',
    orientation='h',
    sort='desc',
    n_bars=3,
    steps_per_period=10,
    interpolate_period=False,
    label_bars=True,
    bar_size=.95,
    period_length=500,
    figsize=(6, 3),
    dpi=144,
    title='Race',
    writer='pillow'
)

plt.show()

请翻译下面关于编程的内容:只返回翻译的文本:请添加 data.csv 内容的摘要 - Christian Karcher
嗨,谢谢你的建议。我已经添加了数据样本。希望这可以帮助到你。 - Abhi Raghu
2个回答

1
据我所知,您需要使用matplotlib包中的ax.set_xticks()手动设置x轴刻度。
以下是一个代码片段,用虚拟数据展示了这个想法:
import pandas as pd
import matplotlib.pyplot as plt
import bar_chart_race as bcr
import numpy as np
# Load the data
# data = pd.read_csv('data.csv')
N = 20
data = pd.DataFrame({'Initial captial of A': np.arange(N),
                     'Probability_A': np.linspace(0, 1, N),
                     'Probability B': np.linspace(1, 0, N)})
# Set the index to the starting capital of A
data = data.set_index('Initial captial of A')
print(data.head())

# Create the animated bar chart race
fig, ax = plt.subplots(1, 1, figsize=(8, 4), dpi=120, tight_layout=True)
bcr.bar_chart_race(
    df=data,
    filename='race.gif',
    orientation='h',
    sort='desc',
    n_bars=3,
    steps_per_period=1,
    interpolate_period=True,
    label_bars=True,
    bar_size=.95,
    period_length=1,
    dpi=144,
    fig=fig,
    title='Race',
    writer='pillow',
    fixed_max=True,
    fixed_order=['Probability_A', 'Probability B']
)
ax.set_xticks(np.linspace(0, 1, 10))
plt.show()

如果您有进一步的问题,请告诉我。 最好的问候。


0
我会将这个作为一个回答发布,因为如果作为评论可能会被忽略:
在当前版本0.1.0的包中,条形图标签的小数位数是硬编码为零的。最新版本0.2.0的bar_chart_race还不能通过PIP安装,但可以通过其他方式进行安装(请参见最后一个回答的链接):
终端:
pip uninstall bar_chart_race
pip install git+https://github.com/dexplo/bar_chart_race

使用这个最新版本,你可以添加关键字bar_texttemplate='{x:,.2f}'来获取两位小数(但是一些其他关键字如"label_bars"和"dpi"必须被移除)。对于xtick-labels也是一样,使用tick_template="{x:,.2f}"

enter image description here

import pandas as pd
import matplotlib.pyplot as plt
import bar_chart_race as bcr
import numpy as np

N = 20
data = pd.DataFrame(
    {
        "Initial captial of A": np.arange(N),
        "Probability_A": np.linspace(0, 1, N),
        "Probability B": np.linspace(1, 0, N),
    }
)
# Set the index to the starting capital of A
data = data.set_index("Initial captial of A")

# Create the animated bar chart race
bcr.bar_chart_race(
    df=data,
    filename="race.gif",
    interpolate_period=True,
    writer="pillow",
    fixed_max=True,
    bar_texttemplate="{x:,.2f}",
    tick_template="{x:,.2f}",
)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接