将堆叠条形图更改为堆叠百分比条形图

Change the stacked bar chart to Stacked Percentage Bar Plot

如何将此堆叠条形图更改为带有百分比标签的堆叠百分比条形图

代码如下:

df_responses= pd.read_csv('https://raw.githubusercontent.com/eng-aomar/Security_in_practice/main/secuirtyInPractice.csv')

df_new =df_responses.iloc[:,9:21]
image_format = 'svg' # e.g .png, .svg, etc.

# initialize empty dataframe
df2 = pd.DataFrame()

# group by each column counting the size of each category values
for col in df_new:
    grped = df_new.groupby(col).size()
    grped = grped.rename(grped.index.name)
    df2 = df2.merge(grped.to_frame(), how='outer', left_index=True, right_index=True)

# plot the merged dataframe
df2.plot.bar(stacked=True)
plt.show()

您可以自己计算百分比,例如在您的数据框的新列中,因为您确实拥有绝对值并改为绘制此列。 使用 sum() 和使用数据帧进行除法,你应该很快就能做到。

您可能想看看 GeeksForGeeks post,其中显示了如何完成此操作。

编辑

我现在已经开始调整你的程序,所以它会给出你想要的结果(至少是我认为你会喜欢的结果)。 我使用而你没有使用的两个关键函数是 df.value_counts() and df.transpose()。您可能想阅读这两个内容,因为它们在很多情况下都非常有用。

import pandas as pd
import matplotlib.pyplot as plt
df_responses= pd.read_csv('https://raw.githubusercontent.com/eng-aomar/Security_in_practice/main/secuirtyInPractice.csv')

df_new =df_responses.iloc[:,9:21]
image_format = 'svg' # e.g .png, .svg, etc.


# initialize empty dataframe providing the columns
df2 = pd.DataFrame(columns=df_new.columns)


# loop over all columns
for col in df_new.columns:
    # counting occurences for each value can be done by value_counts()
    val_counts = df_new[col].value_counts()
    # replace nan values with 0
    val_counts.fillna(0)
    # calculate the sum of all categories
    total = val_counts.sum()
    # use value count for each category and divide it by the total count of all categories
    # and multiply by 100 to get nice percent values
    df2[col] = val_counts / total * 100

# columns and rows need to be transposed in order to get the result we want  
df2.transpose().plot.bar(stacked=True)
plt.show()