Reputation: 238
Below is my dataset:
City Product Spend_Year Total_Spend
0 BANGALORE Gold 2004 248006886
1 BANGALORE Gold 2005 357076946
2 BANGALORE Gold 2006 329375735
3 BANGALORE Platimum 2004 138238363
4 BANGALORE Platimum 2005 167115119
... ... ... ... ...
67 TRIVANDRUM Platimum 2005 222789632
68 TRIVANDRUM Platimum 2006 129408676
69 TRIVANDRUM Silver 2004 14957733
70 TRIVANDRUM Silver 2005 27465271
71 TRIVANDRUM Silver 2006 31632500
I need to plot City vs Total_Spend while selecting one product and Spend_Year for all possible combinations.
Example: Plot of City vs Total_Spend for Gold Product in the Year 2004.
P.S.: In the data set I have 3 product categories and data is available for 3 years.
Any help would be highly appreciated.
Upvotes: 0
Views: 253
Reputation: 150745
You can use seaborn.FaceGrid
:
g = sns.FacetGrid(data=df, row='Spend_Year', col='Product')
g = g.map(sns.lineplot, 'City', 'Total_Spend')
Or you can use groupby()
:
fig, axes = plt.subplots(3,3)
for (k,d), ax in zip(df.groupby(['City','Spend_Year']), axes.ravel()):
city, year = k
d.plot(x='City', y='Total_Spend', ax=ax)
# extra format with ax if needed
ax.set_title(f'{city} - {year}')
Upvotes: 1
Reputation: 1563
The code below will make one plot for each combination. It should be easy to use matplotlib subplots to move them all into one figure.
import matplotlib.pyplot as plt
for product in df['Product'].unique():
for syear in df['Spend_Year'].unique():
plt.figure()
ax = df.query('Product==@product and Spend_year==@syear').set_index('City')['Total Spend'].plot()
ax.set_title(f'Product {product} year {syear}')
Upvotes: 0