Add bar labels to pandas groupby stacked bar chart [duplicate] - python

This question already has answers here:
How to plot and annotate a grouped bar chart
(1 answer)
Labeling a Bar Graph Created from a Grouped Pandas DataFrame where there's a NaN Category
(2 answers)
Plot groupby percentage dataframe
(2 answers)
How to annotate grouped bar plot with percent by hue/legend group
(1 answer)
Closed 11 days ago.
Is is possible to add bar values to a pandas groupby plot?
df.groupby("Responded Year-Month")["Net New Record"].value_counts().unstack(level=1).plot.bar(
stacked=True,
title="Responses by Month & Net New Record",
ylabel="Responses",
xlabel="Month",
rot=0,
color=nnr_colors)
I checked the docs and could not find any reference of values in the bars both the df.plot.bar docs and the df.plot docs.
As an example, it is very easy to accomplish something similar with a pie plot using autopct:
df.groupby("Channel")["Responded"].sum().plot.pie(
autopct='%1.0f%%',
title="Responses by Channel"
)

Related

How do I change the labels of the style variables in the legend of a seaborn plot? [duplicate]

This question already has answers here:
Remap values in pandas column with a dict, preserve NaNs
(11 answers)
How to edit a seaborn legend title and labels for figure-level functions
(2 answers)
Edit legend title and labels of Seaborn scatterplot and countplot
(3 answers)
Closed 2 days ago.
I am trying to change a specific part of the legend in my plot in seaborn.
I wrote the following code to display a plot in seaborn. The plot can be seen below.
ax = sns.lineplot(data = weekly_port_ret,
x='week',y='R',hue='high_leverage',style='gsector')
ax.set(title='Weekly Portfolio Returns - Daily Rebalancing',
xlabel='Week in 2020',
ylabel='Weekly Return'
)
plt.show()
I am just trying to change where it says "20.0" in the legend to "Industrial", and where it says "45.0" to "IT". Does anyone know how to do this?
My plot:
You can assign the gsector column to the values (Industrial and IT) and map it so that you can see the legend as you want... Updated code below.
I used some dummy data, but your code should work as well.
Refer assign and map for more info..
mysector = {20:'Industrial', 45:'IT'}
ax = sns.lineplot(data = weekly_port_ret.assign(gsector=weekly_port_ret['gsector'].map(mysector)),
x='week',y='R',hue='high_leverage',style='gsector')
ax.set(title='Weekly Portfolio Returns - Daily Rebalancing',
xlabel='Week in 2020',
ylabel='Weekly Return'
)
plt.show()
Plot

How to get the median value in boxplot with seaborn and custom coordinate lists [duplicate]

This question already has answers here:
Getting values in Seaborn boxplot
(2 answers)
Extract outliers from Seaborn Boxplot
(2 answers)
Labeling boxplot in seaborn with median value
(3 answers)
Closed last month.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
I am passing two lists of coordinates obtained by selectively appending them from my original data frame. I use them to create a boxplot in which the median of those values is drawn.
df = pd.read_csv("path.csv")
# get data from dataframe depending on certain conditions and store it in xValue and yValue
...
g = sns.JointGrid(x=xValues, y=yValues)
g.plot_joint(sns.scatterplot, size=0.1, color='b', linewidth=0)
g.plot_marginals(sns.boxplot, width=0.3, color='b',notch=True, showcaps=False, medianprops={"color": "coral"})
Boxplot will have to compute the median value in order to show it on the graph, so is there any way to get that numerical value?
This question is different from Labeling boxplot in seaborn with median value, Extract outliers from Seaborn Boxplot and Getting values in Seaborn boxplot because these 3 questions use columns directly from the data frame. If you read carefully my question, the data I pass to the boxplot function is an extract of each of the columns of the data frame.

How to create a histogram with an aggregated dataset [duplicate]

This question already has answers here:
Ploting with seaborn histplot
(2 answers)
Plotting a histogram from pre-counted data in Matplotlib
(6 answers)
Histogram from data which is already binned, I have bins and frequency values
(3 answers)
Closed last month.
I have a dataset new_products which describes the number of months its been since a product launched. I aggregated that data together so that I have 'since_debut' and 'count'. Which describes the number of products that debuted 1, 2, 3....60 month ago. I am having trouble creating a histogram with seaborn.
df = since_debut count
1 1784
2 7345
3 11111
4 13255
sns.histplot(data=df, x="since_debut", y="count", bins=30, kde=True)
ValueError: Could not interpret value `since_debut` for parameter `x`
Unsure what is throwing this error and why it can't interpret the aggregated data. Any help or advice is appreciated.
Since you have already aggregated dataset shouldn't you use something like barplot:
sns.barplot(data=df, x="since_debut", y="count")
countplot should be used on original data and will aggregate data over one of the axis itself.

Seaborn FacetGrid - How to get % instead count? [duplicate]

This question already has answers here:
How to plot percentage with seaborn distplot / histplot / displot
(3 answers)
Plot a horizontal line on a given plot
(7 answers)
Box around text in matplotlib
(3 answers)
Closed 3 months ago.
I'm tring to create a Clustering Situation, with KMeans.
This is how my datasets looks like:
With these dataset, I apply FacetGrid this way:
for c in data:
grid= sns.FacetGrid(data, col='Clusters')
grid.map(plt.hist,c)
grid.set_xticklabels(rotation=90)
Output:
For all features.
This is working ok, but the FacetGrid only show Feature Value X Count for each clusters...
This information is not too relevant too me, since all clusters have different 'len'.
E.g Customer Age for Cluster 1 plot is very higher than Customer Age for Cluster 0, since Cluster 1 has more elements.
What I need:
I need a way to compare each column of the plot relative to its total.
E.g
I'd like to see:
For each cluster and each feature.
Is it possible?
Thank you.

How can I Display Bar Graph Numbers [duplicate]

This question already has answers here:
How to annotate horizontal bar plots with count and percent
(1 answer)
How to display the value of the bar on each bar with pyplot.barh()
(11 answers)
Horizontal stacked bar plot and add labels to each section
(3 answers)
how to add labels to a horizontal bar chart in matplotlib?
(1 answer)
Closed last year.
What can I add to this code to display Bar Graph Numbers?
plt.barh(regionTotal['WHO Region'],regionTotal['Confirmed'])
plt.title('Region Total Confirmed Cases')
plt.xlabel('Region')
plt.ylabel('Confirmed Cases')
plt.show()

Categories