Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在未堆叠的条形图上添加值-python_Python_Pandas_Matplotlib_Charts_Enumerate - Fatal编程技术网

在未堆叠的条形图上添加值-python

在未堆叠的条形图上添加值-python,python,pandas,matplotlib,charts,enumerate,Python,Pandas,Matplotlib,Charts,Enumerate,这是我的堆叠条形图代码。我可以为第一段(AA)添加百分比值,但如何为所有4段添加值 df = pd.read_csv("123.csv") df1 = df.groupby(['Country', 'ClassWeight']) ['Count'].sum().unstack('ClassWeight').fillna(0) #sort on the 'total' column, and then drop it to avoid double plotting ax = df1.so

这是我的堆叠条形图代码。我可以为第一段(AA)添加百分比值,但如何为所有4段添加值

df = pd.read_csv("123.csv")

df1 = df.groupby(['Country', 'ClassWeight']) 
['Count'].sum().unstack('ClassWeight').fillna(0)

#sort on the 'total' column, and then drop it to avoid double plotting

ax = df1.sort_values(['total']).iloc[:,:-1].plot(kind='barh', width=0.8, 
stacked=True, figsize=(15, 10),colormap=ListedColormap(sns.color_palette("Blues_d")))

#plot barchart
ax.set_xlabel('No.of Shipments',fontsize=15)

ax.set_ylabel('Country',fontsize=15)

plt.xticks(fontsize=15)

plt.yticks(fontsize=15)

plt.title('Total Shipments by Country and Customer Class',fontsize=15)

df2=df1.sort_values(['total'],ascending=True)

df2['AA'] = 100*df2['AA']/df2['total']

df2['A'] = 100*df2['A']/df2['total']

df2['B'] = 100*df2['B']/df2['total']

df2['C'] = 100*df2['C']/df2['total']

df3 = df2.iloc[:,:-1]

#Can only enumerate on the AA column. How could we do all 4 columns?

for i, v in enumerate(df3['AA']):
    ax.text(v + -1, i + -0.2, str("{0:.1f}%".format(v)), color='white', 
fontweight='bold', fontsize=15)

示例数据:

ClassWeight            AA          A          B          C
Country                                                   
Romania         17.142857  32.268908  28.235294  22.352941
Finland         60.325203  13.495935  12.682927  13.495935

{'Country':{0:233,1:232,2:286,3:236,4:223},'SumWeight':{0:8072469.5,1:6689511.05,2:5158305.25,3:4675914.53,4:3536684.52},'AvgWeight':{0:34645.79,1:28834.1,2:18036.03,3:19813.2,4:15859},'ClassWeight}{0:'AA',1:'AA',2:'AA',3:'AA',4:'AA'}

我尝试过重新排列您的代码-您不需要每次都创建新的数据帧,您只需要按总数排序一次

import pandas as pd

import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import seaborn as sns

df = pd.DataFrame(
    {'Country': {0: 'France', 1: 'France', 2: 'France', 3: 'France', 4: 'France'},
     'Count': {0: 100, 1: 232, 2: 286, 3: 236, 4: 854},
     'ClassWeight': {0: 'AA', 1: 'A', 2: 'B', 3: 'C', 4: 'total'}}
)

# Track which value columns we want to plot
VALUE_COLS =['AA', 'A', 'B', 'C']

# We only need to sort_values once, so we might as well do it as we generate df1
df1 = df.groupby(['Country', 'ClassWeight'])['Count']\
        .sum()\
        .unstack('ClassWeight')\
        .fillna(0)\
        .sort_values(by='total', ascending=False)

# Get percentage values 
for col in VALUE_COLS:
    df1[col + '_%'] = 100*df1[col]/df1['total']

ax = df1[VALUE_COLS].plot(kind='barh', width=0.8,stacked=True,
                          figsize=(15, 10),
                          colormap=ListedColormap(sns.color_palette("Blues_d")))

# Set up labels and ticks
ax.set_xlabel('No.of Shipments',fontsize=15)
ax.set_ylabel('Country',fontsize=15)
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plt.title('Total Shipments by Country and Customer Class',fontsize=15)

# Add in text labels
df1['label_tot'] = 0
for col in VALUE_COLS:
    df1['label_tot'] += df1[col]
    for i, (val, pos) in enumerate(df1[[col + '_%', 'label_tot']].itertuples(index=False, name=None)):
        ax.text(pos + -1, i, str("{0:.1f}%".format(val)),
                color='white',fontweight='bold', fontsize=15, ha='right')
我对您的输入数据稍加修改后,会得到如下结果:


你能将一些示例数据作为文本而不是图像编辑到文章中吗?这使人们更容易使用。感谢编辑你的示例数据代表哪一帧?这段代码用于打印4列的值,但它们都在彼此的顶部。然后我如何将它们分布在每个堆栈栏上?That ax.text正在为每个国家在同一点绘制4个值。你是对的-我编辑了以尝试跟踪一个运行总数。如果你能解释所提供的数据对应于什么,那将很有帮助,这样我就可以运行你的代码并检查这一点。我在ax.text--->TypeError行上得到以下错误:必须是str,而不是int。columns是客户类型,值是每个国家4列的百分比细分。抱歉,这是我第一次使用此网站,所以我还不知道我需要包含哪些信息。如果您可以
打印(df.head(5).to_dict())
,然后将结果编辑到您的帖子中?这样可以让我知道您从一开始就在处理哪些数据