Search code examples
pythonpandasmatplotlibplottimeserieschart

Matplotlib Plot time series with colored points Pandas (Python)


I'm trying to plot time series data where there are multiple points at each time. I want to have timeon the x axis, Amount on the y axis and then color each point by ID i.e. where ID = 344 they would all have the same color etc.

Below is example data i'm using. I'm sure this must exist but I can't find gallery examples on Matplotlib.org

                ID     Amount
Time                        
2015-12-09      344    0.333333
2015-12-10      345    0.333333
2015-12-09      345    0.333333
2015-12-09      344    0.750000
2015-12-09      342    0.583333

Things i've tried include trying to reshape the data as a pivot table (didn't work because there are two duplicate values for ID 344. Groupby, but I've struggled grouping by two columns, I think if I could groupby ID and retain the Time field this would go some way towards solving my problem.

Any help or advice would be massively appreciated.


Solution

  • Read the docs and look at examples for scatter within pylab or matplotlib

    import pylab as pl
    
    fig= pl.figure( figsize=(5,5) )
    ax  = fig.add_subplot(111)
    
    ax.scatter(df.index, df.Amount, s=20, c=df.ID)
    

    This can be customized to meet your needs.