如何在条形图的条形图内显示值? [英] How to show values inside the bars of a bargraph?
本文介绍了如何在条形图的条形图内显示值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的数据框:
I have a dataframe like this:
platform count
release_year
1996 PlayStation 138
1997 PlayStation 170
1998 PlayStation 155
1999 PC 243...
现在,我想在各个条形图中绘制带有平台名称的水平条形图,使其看起来像这样:
Now I want to plot horizontal bargraph with the Platform name inside the respective bars such that it looks something like this:
我该怎么做?
推荐答案
找到每个平台中的百分比后,这就是输入的data.csv
文件:
Here's the input data.csv
file once you find the percentage in each platform:
Platform,Percent
Nintendo,34
PC,16
Playstation,28
Xbox,22
这是代码:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("data.csv", index_col=0)
df.plot(kind="barh", legend=False, width=0.8)
for i, (p, pr) in enumerate(zip(df.index, df["Percent"])):
plt.text(s=p, x=1, y=i, color="w", verticalalignment="center", size=18)
plt.text(s=str(pr)+"%", x=pr-5, y=i, color="w",
verticalalignment="center", horizontalalignment="left", size=18)
plt.axis("off")
# xticks & yticks have empty lists to reduce white space in plot
plt.xticks([])
plt.yticks([])
plt.tight_layout()
plt.savefig("data.png")
这篇关于如何在条形图的条形图内显示值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文