After performing a groupby.sum()
on a DataFrame
I'm having some trouble trying to create my intended plot.
import pandas as pd
import numpy as np
np.random.seed(365)
rows = 100
data = {'Month': np.random.choice(['2014-01', '2014-02', '2014-03', '2014-04'], size=rows),
'Code': np.random.choice(['A', 'B', 'C'], size=rows),
'ColA': np.random.randint(5, 125, size=rows),
'ColB': np.random.randint(0, 51, size=rows),}
df = pd.DataFrame(data)
Month Code ColA ColB
0 2014-03 C 59 47
1 2014-01 A 24 9
2 2014-02 C 77 50
dfg = df.groupby(['Code', 'Month']).sum()
ColA ColB
Code Month
A 2014-01 124 102
2014-02 398 282
2014-03 474 198
2014-04 830 237
B 2014-01 477 300
2014-02 591 167
2014-03 522 192
2014-04 367 169
C 2014-01 412 180
2014-02 275 205
2014-03 795 291
2014-04 901 309
How can I create a subplot (kind='bar'
) for each Code
, where the x-axis is the Month
and the bars are ColA
and ColB
?
Creating a MultiIndex (hierarchical index) object A MultiIndex can be created from a list of arrays (using MultiIndex. from_arrays() ), an array of tuples (using MultiIndex. from_tuples() ), a crossed set of iterables (using MultiIndex. from_product() ), or a DataFrame (using MultiIndex.
I found the unstack(level) method to work perfectly, which has the added benefit of not needing a priori knowledge about how many Codes there are.
ax = dfg.unstack(level=0).plot(kind='bar', subplots=True, rot=0, figsize=(9, 7), layout=(2, 3)) plt.tight_layout()
Using the following DataFrame ...
# using pandas version 0.14.1
from pandas import DataFrame
import pandas as pd
import matplotlib.pyplot as plt
data = {'ColB': {('A', 4): 3.0,
('C', 2): 0.0,
('B', 4): 51.0,
('B', 1): 0.0,
('C', 3): 0.0,
('B', 2): 7.0,
('Code', 'Month'): '',
('A', 3): 5.0,
('C', 1): 0.0,
('C', 4): 0.0,
('B', 3): 12.0},
'ColA': {('A', 4): 66.0,
('C', 2): 5.0,
('B', 4): 125.0,
('B', 1): 5.0,
('C', 3): 41.0,
('B', 2): 52.0,
('Code', 'Month'): '',
('A', 3): 22.0,
('C', 1): 14.0,
('C', 4): 51.0,
('B', 3): 122.0}}
df = DataFrame(data)
... you can plot the following (using cross-section):
f, a = plt.subplots(3,1)
df.xs('A').plot(kind='bar',ax=a[0])
df.xs('B').plot(kind='bar',ax=a[1])
df.xs('C').plot(kind='bar',ax=a[2])
One for A, one for B and one for C, x-axis: 'Month', the bars are ColA and ColB. Maybe this is what you are looking for.
seaborn
can easily aggregate long form data from a dataframe without .groupby
or .pivot_table
.df
, the easiest option is the convert it to a long form with pandas.DataFrame.melt
, and then plot with seaborn.catplot
, which is a high-level API for matplotlib.
estimator
from mean
to sum
'Month'
column in the OP is a string type. In general, it's better to convert the column to datetime dtype with pd._to_datetime
python 3.8.11
, pandas 1.3.2
, matplotlib 3.4.2
, seaborn 0.11.2
seaborn.catplot
import seaborn as sns
dfm = df.melt(id_vars=['Month', 'Code'], var_name='Cols')
Month Code Cols value
0 2014-03 C ColA 59
1 2014-01 A ColA 24
2 2014-02 C ColA 77
3 2014-04 B ColA 114
4 2014-01 C ColA 67
# specify row and col to get a plot like that produced by the accepted answer
sns.catplot(kind='bar', data=dfm, col='Code', x='Month', y='value', row='Cols', order=sorted(dfm.Month.unique()),
col_order=sorted(df.Code.unique()), estimator=sum, ci=None, height=3.5)
sns.catplot(kind='bar', data=dfm, col='Code', x='Month', y='value', hue='Cols', estimator=sum, ci=None,
order=sorted(dfm.Month.unique()), col_order=sorted(df.Code.unique()))
pandas.DataFrame.plot
pandas
uses matplotlib
and the default plotting backend.pandas.DataFrame.pivot_table
instead of .groupby
, because the resulting dataframe is in the correct shape, without the need to unstack.dfp = df.pivot_table(index='Month', columns='Code', values=['ColA', 'ColB'], aggfunc='sum')
dfp.plot(kind='bar', subplots=True, rot=0, figsize=(9, 7), layout=(2, 3))
plt.tight_layout()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With