Plot a stacked bar graph from two pandas groupby objects?
Tag : python , By : General Mills
Date : March 29 2020, 07:55 AM
I hope this helps you . You can do it by using concat to make a new dataframe and plotting that, though I think you'll have to rename one of the columns. cgs = cg.sum()
cgs.columns = ['number2']
d = pd.concat([bg.sum(), cgs], axis=1)
d.plot(kind='bar', stacked=True)
|
How to get the label on bar plot/stacked bar plot in matplotlib?
Tag : python , By : Longchao Dong
Date : March 29 2020, 07:55 AM
hop of those help? This gets a little tricky because bar is a complex plot object that is really composed of multiple components. You can use get_legend_handles_labels to get all the artists and labels for the axes. Then you can look so see which group your current artist belongs to. def on_pick(event)
rect = event.artist
# Get the artists and the labels
handles,labels = rect.axes.get_legend_handles_labels()
# Search for your current artist within all plot groups
label = [label for h,label in zip(handles, labels) if rect in h.get_children()]
# Should only be one entry but just double check
if len(label) == 1:
label = label[0]
else:
label = None
print label
|
Draw lines between different elements in a stacked bar plot
Tag : r , By : Chandra P Singh
Date : March 29 2020, 07:55 AM
wish of those help Instead of hard-coding the start and end positions of the segments, you may grab this data from the plot object. Here's an alternative where you provide the names of the x categories and bar elements between which the lines should be drawn. Assign the plot to a variable: p <- ggplot() +
geom_bar(data = Example,
aes(x = X_Axis, y = Percent, fill = Stack_Group), stat = 'identity', width = 0.5)
d <- ggplot_build(p)$data[[1]]
setDT(d)
d[ , r := rank(group), by = x]
Example[ , x := .GRP, by = X_Axis]
Example[ , r := rank(Stack_Group), by = x]
d <- d[Example[ , .(X_Axis, Stack_Group, x, r)], on = .(x, r)]
x_start_nm <- "Count"
x_end_nm <- "Dollars"
e_start <- "A & B"
e_upper <- "A Mixed dollars"
e_lower <- "B Mixed Dollars"
d2 <- data.table(x_start = rep(d[X_Axis == x_start_nm & Stack_Group == e_start, xmax], 2),
y_start = d[X_Axis == x_start_nm & Stack_Group == e_start, c(ymax, ymin)],
x_end = rep(d[X_Axis == x_end_nm & Stack_Group == e_upper, xmin], 2),
y_end = c(d[X_Axis == x_end_nm & Stack_Group == e_upper, ymax],
d[X_Axis == x_end_nm & Stack_Group == e_lower, ymin]))
p +
geom_segment(data = d2, aes(x = x_start, xend = x_end, y = y_start, yend = y_end))
|
Stacked bar plot from Dataframe using groupby
Date : March 29 2020, 07:55 AM
With these it helps I have the following dataframe and I am trying to create a stacked bar plot , IIUC, this is what you want: df2 = (df.groupby(['race','qualifier','participant'])
.size()
.unstack(level=-1)
.reset_index()
)
fig,axes = plt.subplots(1,2,figsize=(12,6),sharey=True)
for ax,q in zip(axes.ravel(),['first','last']):
tmp_df = df2[df2.qualifier.eq(q)]
tmp_df.plot.bar(x='race', ax=ax, stacked=True)
|
How to plot a stacked bar using the groupby data from the dataframe in python?
Date : March 29 2020, 07:55 AM
With these it helps from what i understand you should try something like : df.groupby(['country', 'Year']).value.sum().unstack().plot(kind='bar', stacked=True)
|