Percentage Stacked Bar Chart With A Specific Data Structure
I have the following specific data structure: df = pd.DataFrame(columns=['Feature1', 'Target'], data=[['A', 0], ['A', 0],
Solution 1:
You could first count the values of each type, then divide by the total for each feature and multiply by 100.
The resulting dataframe can be plotted as follows:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
sns.set()
df = pd.DataFrame(columns=["Feature1", "Target"],
data=[["A", 0], ["A", 0], ["A", 0], ["A", 1], ["A", 1], ["A", 1],
["B", 1], ["B", 1], ["B", 0], ["B", 0], ["B", 0]])
df1 = (df.groupby(["Feature1", "Target"]).size() / df.groupby(["Feature1"]).size() * 100)
df1 = df1.reset_index(name='Percentage')
features = np.unique(df1['Feature1'])
plt.bar(x=features, height=100, color='dodgerblue', label='Target = 1')
plt.bar(x=features,
height=[df1[(df1['Feature1'] == x) & (df1['Target'] == 0)]['Percentage'].values[0] for x in features],
color='crimson', label='Target = 0')
plt.legend()
plt.xlabel('Feature1')
plt.ylabel('Percentage')
plt.show()
Post a Comment for "Percentage Stacked Bar Chart With A Specific Data Structure"