我正在嘗試繪制一個分組條形圖,其中我在條形旁邊顯示相應的值。目前,我的代碼所做的是,它在條形旁邊顯示值,比實際值大 1。我需要在我的代碼中進行哪些更改,以便在條形旁邊顯示實際值?

# importing package
import matplotlib.pyplot as plt
import pandas as pd
# create data
df = pd.DataFrame([['A', 10, 20, 10, 30], ['B', 20, 25, 15, 25], ['C', 12, 15, 19, 6],
['D', 10, 29, 13, 19]],
columns=['Team', 'Round 1', 'Round 2', 'Round 3', 'Round 4'])
# view data
print(df)
# plot grouped bar chart
ax=df.plot.barh(x='Team',
stacked=False,
log=True,
figsize=(12, 7),
title='Grouped Bar Graph with dataframe')
for c in ax.containers:
ax.bar_label(c, label_type='edge')
uj5u.com熱心網友回復:
如果您查看檔案help(ax.bar_label),則會暗示label_type="edge"并label_type="center"顯示不同的值。解決這個問題的一種方法是傳遞我們的標簽,如下所示:
for c in ax.containers:
labels = c.datavalues.astype(str)
ax.bar_label(c, label_type='edge', labels=labels)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/473867.html
標籤:Python 熊猫 matplotlib
