Matplotlib-条形图上的百分比

时间:2020-04-08 12:44:33

标签: python pandas matplotlib

我想在我的水平条形图上显示每个条形的百分比,但是我不确定该怎么做。另外,如果可以的话,理想情况下,我会在每个小节的末尾都有百分比吗?

这是我当前的脚本:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
%matplotlib inline

...
percentage = []

for i in top_ten_districts:
    percentage.append(int((i / total_ads)*100))

percentage = ['{}%'.format(x) for x in percentage]

top_ten_districts = active_ads.district.value_counts().head(10)
plt.barh(top_ten_districts.index, top_ten_districts)
plt.xlabel('num of ads')
plt.ylabel('county')
plt.title('TOP 10 DISTRICTS')
top_ten_districts

我希望在图表上的每个百分比后面都有'%'符号,但是,当我执行脚本时,会得到以下信息:

ConversionError:无法将值转换为轴单位:'22%'

当前图表:

enter image description here

0 个答案:

没有答案