Plotting stacked bar chart - python

I'm trying to create a stacked bar chart, with
xaxis = 'customer_id(count), yaxis = 'age_band',
and the 3 different loyalty groups stacked in the chart (hue), so I should see 6 bars each with 2-3 different colours.
code I've tried:
df.groupby(['age_band','loyalty']).agg(Loyalty=('customer_id', pd.Series.nunique)).plot(kind='bar', stacked=True)
Picture for reference:

IIUC use DataFrame.pivot_table:
(df.pivot_table(index='age_band',columns='loyalty', values='customer_id', aggfunc='nunique')
.plot(kind='bar', stacked=True))

Related

How do I group bars in a single trace of a plotly bar chart

I am trying to plot a bar chart in plotly with grouped bars for data points with the same x-value, but the barmode is being ignored. How can I do this for the following sample data:
x = [3,2,4,5,6,7,4,3,2,3,7]
y = [10,20,24,13,14,20,21,20,12,13,7]
For this data, I want three bars next to each other at x=3, two bars at x=2 and two bars at x=7
I have tried the following, updating the layout to group and setting base to 0, but the bars are still being stacked.
fig = go.Figure()
fig.add_trace(go.Bar(x=self.x, y=self.y,
base=0
))
fig.update_layout(barmode='group')
fig.show()
Thanks.

How to create a single series bar graph with legends using Python

How do I visualize the below dataframe in Python. I wish to visualize the data in a bar chart where the Year_of_Release is the X axis, Global_Sales is the bar height & the genre is the legend. The bar has to be colored separately for each Genre. I have shared a sample of what I'm looking for. The sample graph was created on R using GGPLOT.
Below are the column definitions
Year_of_Release - Year of Release
Genre - Game Genre
Global_Sales - Revenue made by a Genre in that given year
Images of the data frame & desired Bar plot as as below
Data Frame:
Desired Bar Chart:
you can use the code below to plot that you need...
fig, ax = plt.subplots(figsize=(12,6))
sns.set_theme(style="darkgrid")
ax=sns.barplot(x="Year_of_Release", y="Global_Sales", hue="Genre", dodge=False, palette="rocket", data=df)
plt.xticks(rotation=90)
ax.grid(True)
ax.legend(loc='upper left')
plt.show()
Plot
with dummy data
...

Creating a bar chart with 2 y axes from lists using matplotlib

I need to make the following chart: Number of Companies, Donations vs Year as a bar chart.
The following is my data:
Year = [2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018]
No_Companies = [123558,132335,147606,155790,161211,169784,174599,183888,198727,207317,217357,228996]
Donations=[144932,304607,642328,870509,1205382,1094624,2089240,2325322,2387036,3096069,4204255,3500766]
From what I have seen from other questions, most seem to have either their data in a dataframe or a list like [[x1,y1],[x2,y2]].
How can I get the chart I need from the data I have?
You can check this link out: Plot bar and line in same plot, different y-axes using matplotlib (no pandas)
The implementation can be done as follows:
plt.figure(1, figsize=(10,10))
barchart = plt.bar(Year, No_Companies, color='red')
plt.ylabel('No Companies')
plt.twinx()
barchart1 = plt.bar(Year, Donations, color='blue')
plt.ylabel('Donations')
Graph

Plot sub-bar charts on a dataframe groupby

Hi I am having some trouble plotting sub-bar charts after a dataframe groupby
Post groupby, the data is as per the below :
I tried the below to create a bar chart.
df_temp[df_temp.index =='ABC'].unstack().plot.bar(figsize=(10,2))
How can I plot a bar charts where the x-axis is the date and y-axis is the count and each row (ABC and EFG) is its own subplot (vertically stacked)
Example below
thanks for your help !
thanks to #r-beginnners
#remove the multi-level column
df.columns = df.columns.droplevel()
#plot the sub-plots
# if y-axis scale to be the same, use sharey=True
df.T.plot(subplots=True, layout=(2,1), kind='bar', sharey=True)

Matplotlib bar chart X-axis Labels order

Is there a way I can rearrange the x-axis of my bar chart so that the ages are listed in order?
Add a sort_index() after value_counts()
www['Age'].value_counts().sort_index().plot(kind='bar')

Categories

Resources