create stacked plot of percentages [duplicate] - python

This question already has answers here:
Stacked bar chart in Seaborn
(2 answers)
How to make a horizontal stacked histplot based on counts?
(2 answers)
Closed 25 days ago.
I have a dataframe containing the percentage of people in my dataset stratified per Gender and Age.
df_test = pd.DataFrame(data=[['Male','16-24',10],
['Male','25-34',5],
['Male','35-44',2],
['Female','16-24',3],
['Female','25-34',60],
['Female','35-444',20],
],
columns=['Gender','Age','Percentage'])
First I create a plot showig the percentages of Male and Female in the dataset
df_test.groupby('Gender').sum().plot(kind='bar',rot=45)
Now I would like to add within each bar the percentage of people in the age ranges in a stacked kind of way...
Could you help ?

You can use the cumsum() method for cumulative summation as
df_test.groupby('Age').sum().cumsum().plot(kind="bar",rot=45)

Related

Displaying KDE plots in a grid format using Seaborn [duplicate]

This question already has answers here:
How to make seperate Seaborn kdeplots for all different columns from the same pandas Dataframe?
(2 answers)
How to Plot a Matrix of Seaborn Distplots for All Columns in the Dataframe
(3 answers)
Plot multiple columns of pandas DataFrame using Seaborn
(2 answers)
Closed 10 months ago.
I am using the Wisconsin Breast Cancer dataset for a class project and I am attempting to display 10 KDE plots in 5 x 2 layout. Unfortunately,the plots are displayed in scrollable window which is definitely not what I would like.
features = ['radius_mean', 'texture_mean', 'perimeter_mean', 'area_mean', 'smoothness_mean', 'compactness_mean', 'concavity_mean', 'concave_points_mean', 'symmetry_mean', fractal_dimension_mean']
for feature in features:
df[feature].plot.kde(title=feature)
plt.show()
Any help would be appreciated.
Thank you.

Seaborn with multiple Columns [duplicate]

This question already has answers here:
Plotting two columns of dataFrame in seaborn
(1 answer)
Seaborn multiple barplots
(2 answers)
seaborn multiple variables group bar plot
(1 answer)
Closed 12 months ago.
I have a dataframe data_gender:
gender
math_score
reading_score
writing_score
avg_score
female
63.63
72.61
72.47
69.57
male
68.73
65.47
63.31
65.84
and I want to make a seaborn barplot that looks like this plot that I made with matplotlib with simple line
data_gender.plot.bar(figsize=(8,6))
How would one do it with seaborn?
You can reshape with melt and pass the data to sns.barplot:
sns.barplot(data=data_gender.melt(id_vars='gender',
value_name='score', var_name='course'),
x='gender', y='score', hue='course')
output:
sns.barplot(x='gender', y='score', hue='course', data=data_gender)

How to add individual data labels to countplot in seaborn? [duplicate]

This question already has answers here:
How to plot and annotate grouped bars in seaborn / matplotlib
(1 answer)
How to plot and annotate a grouped bar chart
(1 answer)
How to add value labels on a bar chart
(7 answers)
Closed 1 year ago.
I have been working on a campus recruitment dataset. The target variable in the dataset is "status", which indicates if the student is placed or not. Now, I am comparing each variable (for e.g. gender) with the target variable (status of placement), to know which variable affects the target variable the most. To compare two variables, I have been using countplots in seaborn. The plot for the variable "gender" looks like this.
Image showing the sns plot
The code for the sns plot is as follows:
ax = sns.countplot(x = "cat_degree_t", hue = "status", order = df['cat_degree_t'].value_counts().index, data = df);
abs_values = df["cat_degree_t"].value_counts().values;
ax.bar_label(container=ax.containers[0], labels=abs_values);
Now I want to know how I could add values of individual bars in the countplot (not the total value like already written in the figure shown above, but on every individual bar). This would help me find out the percentage of placed and not placed for each category in the variable "gender".
Any help would be really appreciated.
Thanks

How to ignore the outliers in a seaborn violin plot? [duplicate]

This question already has answers here:
Detect and exclude outliers in a pandas DataFrame
(19 answers)
Closed 1 year ago.
See the violinplot:
here I'm showing the points to show that the long tail of the violin is due to a single point. I would like to ignore these outliers points so that I have a more concise violin plot. Can I do that with seaborn when plotting the violin or do I have to remove them from the distribution myself?
You can do it by excluding the outlier data while passing it through the plot function.
e.g.
sns.violinplot(y = df[df["Column"]<x]["Column"])
wherein, df is your dataframe. Column is the name of the column you want to plot and x is the outlier value that you want to exclude.

Facing Problem in counting multi-label for classification [duplicate]

This question already has answers here:
Plot key count per unique value count in pandas
(3 answers)
Closed 2 years ago.
I am trying to count the number of labels for my multilabel classification, but I fail to plot a bar graph for my label column. Is there anybody who can help me out? i already used below code to plot but it shows
*'DataFrame' object has no attribute 'arange'
As you can see the multiple labels are there in a Label column so I want to plot a bar graph for them please help me out
i=data.arange(20)
tag_df_sorted.head(20).plot(kind='bar')
plt.title('Frequency of top 20 tags')
plt.xticks(i, tag_df_sorted['Labels'])
plt.xlabel('Tags')
plt.ylabel('Counts')
plt.show()
Seems like you want to have a histogram.
You can either go like this:
tag_df_sorted.groupby('Labels').count().plot()
or with Pandas's hist function:
# number of unique values in the column "Labels"
Num = len(tag_df_sorted['Labels'].unique())
# plot histogram
hist = tag_df_sorted['Labels'].hist(bins=Num )
There is a nice little tutorial on plotting histograms here.

Categories

Resources