Plotting a timeseris graph from pandas dataframe using matplotlib

Karthik Sharma karthik.sharma at gmail.com
Fri Nov 20 02:47:52 EST 2015


I have the following data in a csv file 

       SourceID    BSs	 hour	Type
        7208	   87	  11	MAIN
        11060	   67	  11	MAIN
        3737	   88	  11	MAIN
        9683	   69	  11	MAIN
        9276	   88	  11	MAIN
        7754	   62	  11	MAIN
        11111	   80	  12	MAIN
        9276	   88	  12	MAIN
        11111	   80	  12	MAIN
        6148	   70	  12	MAIN
        11111	   80	  12	MAIN
        9866	   80	  12	SUB
        9866	   78	  13	MAIN
        9866	   78	  13	SUB
        20729	   82	  14	MAIN
        9276	   88	  14	MAIN
        11111	   80	  15	MAIN
        20190	   55	  15	MAIN
        7208	   85	  15	MAIN
        7208	   86	  15	MAIN
        7754	   61	  16	MAIN
        8968	   91	  16	MAIN
        3737	   88	  16	MAIN
        9683	   69	  16	MAIN
        20729	   81	  16	MAIN
        9704	   68	  16	MAIN
        11111	   87	  16	PAN


I have the following python code.I want to plot a graph with the following specifications.

For each `SourceID` and `Type` I want to plot a graph of `BSs` over time. I would prefer if each `SourceID` and `Type` is a subplot on single plot.I have tried a lot of options using groupby, but can't seem to get it work.

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

    
    
    COLLECTION = 'NEW'
    DATA = r'C:\Analysis\Test\{}'.format(COLLECTION)
    INPUT_FILE = DATA + r'\in.csv'
    OUTPUT_FILE = DATA + r'\out.csv'
    
    
    with open(INPUT_FILE) as fin:
        df = pd.read_csv(INPUT_FILE,
                      usecols=["SourceID", 'hour','BSs','Type'],
                      header=0)
    
        df.drop_duplicates(inplace=True)
    
        df.reset_index(inplace=True)

   



More information about the Python-list mailing list