how to draw a stacked bar - python

I have a dataframe - df as below :
df = pd.DataFrame({"Card_name":['AAA','AAA','AAA','BBB','BBB','BBB','CCC','CCC','CCC'],
"Amount":['900','800','700','600','500','400','400','300','200'],
"Category" :['Grocery','Bank','Gas','Bank','Grocery','Recreation',
'Bank','Grocery','Gas']})
I want to build a visualization plot, where i can show for all the "Card_name" the Categories along with the amount. Maybe a stacked bar chart which shows all the categories for each "Card_name". Each area(size of the area) in the stacked bar chart depends on the Amount.
I tried many possible ways but i am not able to visualize ? Any help will be appreciated.

First pivot your df, then call give the option stacked=True:
df = pd.DataFrame({"Card_name":['AAA','AAA','AAA','BBB','BBB','BBB','CCC','CCC','CCC'],
"Amount":['900','800','700','600','500','400','400','300','200'],
"Category" :['Grocery','Bank','Gas','Bank','Grocery','Recreation','Bank','Grocery','Gas']})
df['Amount'] = pd.to_numeric(df['Amount'])
df.pivot(index='Card_name', columns='Category', values='Amount').plot(kind='bar', stacked=True)

Related

Grouped Bar chart for Grouped Dataframe for my x Object

I am new in this field and I want to create grouped bar graph for the given grouped df.
Can anyone help me?
x = df.groupby(["year","continent"])[["lifeExp"]].mean()
The above is the grouped df that I have created but I don't know how to create grouped bar graph where x-axis should be year y-axis is life_exp and for different continent on xaxis.
My x object for grouped df
Need my grouped graph chart like this
I am not able to come up to any solution yet as i recently started my study in data visualization

Plotting stacked bar chart

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))

Python & Pandas: Plotting a Pivot with multiple Indexes

Hi to all the experts,
I'm new to Python and Data Science and actually I'm learning with a real world example to get into Data Science.
I loaded already a CSV and did some work on the data. That's ok. I have the following dataframe:
dataframe
Then, I created a Pivot from the dataframe:
pivot = pd.pivot_table(
data=df,
index=['Category', 'month', 'year'],
values='Amount',
aggfunc='sum',
margins=True)
Now, I have the following dataframe:
new dataframe
Now, I want to plot the following (line chart or bar chart):
X: Month
Y: Amount
But, I want that for explicit Category like "Business" to see, how the amount changed over the periods.
Whats the best way, to plot a clear, beautiful chart with matplotlib?
Thanks in Advance.
Many Greetings
Leon
You can use the below code to build the graphs. I think the stacked bar graphs would be a good way to see the Amount in each month.
Code
## Add AFTER you have created your pivot table
dfg = pivot.reset_index().set_index(['Month', 'Category']).sort_index(level=[0,1])
fig, ax = plt.subplots(figsize=(6,4))
dfg['Amount'].unstack().plot.bar(stacked=True, ax=ax, legend = False)
ax.set_xticklabels(sorted(df.Month.unique()), rotation=0)
ax.set_title('My Graph')
fig.legend(loc="upper right", bbox_to_anchor=(1.1, 0.9))
plt.show()
Stacked Bar graph
Unstacked Bar graph
Change stacked = True to stacked = False to see the bars next to each other, if you are not a fan of stacked bars
Line Graphs
You can also use line graphs, but not my personal preference.
Replace the plot.bar line in above code to
dfg['Amount'].unstack().plot(kind='line', marker='o', ax=ax, legend = False)

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)

Holoviews - How to create side-by-side bars from dataframe columns?

I would like to create a Holoviews bar chart (using Bokeh backend) in which Year is on the X-axis and columns A and B on the Y-Axis. For each Year, I want bars for values from columns A and B to appear next to each other i.e. for Year 2008, I have bars of heights 1 and 3, for year 2009, I have bars 3 and 6 height, and so on. I have tried numerous different ways including the example of grouped bars in the documentation but can't get it to work. See example below:
%%opts Bars [xrotation=90 width=600 show_legend=False tools=['hover']]
df=pd.DataFrame({'Year':[2008,2009,2010,2011,2012,2013],
'A': [1,2,3,4,5,6],'B':[3,6,9,12,15,18]})
print(df)
bars = hv.Bars(df, kdims=['Year'], vdims=['A'])
bars
Please help. I am losing my mind!
HoloViews generally works best when your data is in what's called a tidy format. However to make it easier to work with data like yours we have developed a companion library called hvPlot. To generate the plot you want you can simply run:
import hvplot.pandas
df=pd.DataFrame({'Year':[2008,2009,2010,2011,2012,2013],
'A': [1,2,3,4,5,6],'B':[3,6,9,12,15,18]})
df.hvplot.bar('Year')
Alternatively you can learn about the pd.melt method, which can take your data in a wide format and convert it to a tidy dataset:
%%opts Bars [xrotation=90 width=600 show_legend=False tools=['hover']]
df=pd.DataFrame({'Year':[2008,2009,2010,2011,2012,2013],
'A': [1,2,3,4,5,6],'B':[3,6,9,12,15,18]})
tidy_df = df.melt(id_vars=['Year'], value_vars=['A', 'B'])
bars = hv.Bars(tidy_df, ['Year', 'variable'], ['value'])
bars
To respond to #pongo30 you can suppress printing 'A' and 'B' on the x-axis by adding .opts(xlabel='') to the call to hvplot.bar() (ex: df.hvplot.bar('Year').opts(xlabel=''))

Categories

Resources