Python – Use a for loop to plot multiple plots in 1 graph

Use a for loop to plot multiple plots in 1 graph… here is a solution to the problem.

Use a for loop to plot multiple plots in 1 graph

I

have data in the following format, what I’m going to do is:

1) Iterate through each value
in the Region
2) For each region, plot a time series that aggregates (cross-category) sales quantities.

Date | Region | Category | Sales
01/01/2016| USA| Furniture|1
01/01/2016| USA| Clothes |0
01/01/2016| Europe| Furniture|2
01/01/2016| Europe| Clothes |0
01/02/2016| USA| Furniture|3
01/02/2016| USA| Clothes|0
01/02/2016| Europe| Furniture|4
01/02/2016| Europe| Clothes|0 …

The diagram should look similar to the attachment (done in Excel).

enter image description here

However, if I try to do this in Python using the code below, I get multiple charts when I really want all the lines to be displayed in one graph.

Python code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.read_csv(r'C:\Users\wusm\Desktop\Book7.csv')

plt.legend()
for index, group in df.groupby(["Region"]):
    group.plot(x='Date',y='Sales',title=str(index))

plt.show()

If the data is not reformatted, can someone suggest how to get the chart in one graph?

Solution

You can use pivot_table :

df = df.pivot_table(index='Date', columns='Region', values='Sales', aggfunc='sum')
print (df)
Region      Europe  USA
Date                   
01/01/2016       2    1
01/02/2016       4    3

or >groupby + sum + unstack :

df = df.groupby(['Date', 'Region'])['Sales'].sum().unstack(fill_value=0)
print (df)
Region      Europe  USA
Date                   
01/01/2016       2    1
01/02/2016       4    3

Then DataFrame.plot

df.plot()

Related Problems and Solutions