Search code examples
pythonpandasnumpymatplotlibtrendline

How to add multiple trendlines pandas


I have plotted a graph with two y axes and would now like to add two separate trendlines for each of the y plots.

This is my code:

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

amp_costs=pd.read_csv('/Users/Ampicillin_Costs.csv', index_col=None, usecols=[0,1,2])
amp_costs.columns=['PERIOD', 'ITEMS', 'COST PER ITEM']

ax=amp_costs.plot(x='PERIOD', y='COST PER ITEM', color='Blue', style='.', markersize=10)
amp_costs.plot(x='PERIOD', y='ITEMS', secondary_y=True,
color='Red', style='.', markersize=10, ax=ax)

Any guidance as to how to plot these two trend lines to this graph would be much appreciated!


Solution

  • Here is a quick example of how to use sklearn.linear_model.LinearRegression to make the trend line.

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn.linear_model import LinearRegression
    plt.style.use('ggplot')
    %matplotlib inline
    
    period = np.arange(10)
    items = -2*period +1 + np.random.randint(-2,2,len(period))
    cost = 35000*period +15000 + np.random.randint(-25000,25000,len(period))
    data = np.vstack((period,items,cost)).T
    df = pd.DataFrame(data, columns=\['P','ITEMS', 'COST'\]).set_index('P')
    
    
    lmcost = LinearRegression().fit(period.reshape(-1,1), cost.reshape(-1,1))
    lmitems = LinearRegression().fit(period.reshape(-1,1), items.reshape(-1,1))
    
    df['ITEMS_LM'] = lmitems.predict(period.reshape(-1,1))
    df['COST_LM'] = lmcost.predict(period.reshape(-1,1))
    
    fig,ax = plt.subplots()
    
    
    df.ITEMS.plot(ax = ax, color = 'b')
    df.ITEMS_LM.plot(ax = ax,color= 'b', linestyle= 'dashed')
    df.COST.plot(ax = ax, secondary_y=True, color ='g')
    df.COST_LM.plot(ax = ax, secondary_y=True, color = 'g', linestyle='dashed')
    

    enter image description here