Search code examples
pythonnumpydatematplotlibtwinx

Cannot get plot to correctly plot 2 y series with their corresponding dates on x-axis?


Attempting to plot two sets of arrays. x against y and x1 against y1 on the same graph. The datetime values are different between the two sets but the y and y1 are plotted according to the same x values which not what is written in the arrays (the dates for both sets are different).

Any help is appreciated!

x = ['01/01/2019', '05/11/2019', '09/02/2019', '09/10/2020', '09/19/2019', '09/24/2019', 
'10/26/2019', '03/14/2020', '03/16/2020', '03/16/2020', '05/10/2020', '07/28/2020', '09/03/2020', 
'14/09/2020']
y = [0.0025, 0.00881983, 0.0025, 0.009436, 0.01069436, 0.01213136, 0.00925736, 0.01503343, 
0.01803343, 0.02103343, 0.02603343, 0.01353343, 0.02703343, 0.03065149]
fig,ax = plt.subplots()
ax.plot(x, y, color="blue", marker="o")
ax.set_xlabel("Time",fontsize=14)
ax.set_ylabel("Y1",color="blue",fontsize=14)
plt.xticks(rotation=90)
x1 = ['16/11/2018', '27/12/2018', '07/01/2019', '18/03/2019', '13/05/2019', '19/09/2019', 
'16/03/2020', '7/09/2020', '7/09/2020']
y1 = [10, 15, 20, 30, 32, 52, 115.27, 165.27, 160.79]
ax2=ax.twinx()
ax2.plot(x1, y1, color="green",marker="o")
ax2.set_ylabel("Y2",color="green",fontsize=14)
plt.xticks(rotation=90)
plt.show()`

Output graph can be seen here


Solution

  • Here is one potential way using pandas. Since, you want a common timeseries, some preprocessing needs to be performed.

    import pandas as pd
    import matplotlib.pyplot as plt
    
    x = ['01/01/2019', '05/11/2019', '09/02/2019', '09/10/2020', '09/19/2019', '09/24/2019',
    '10/26/2019', '03/14/2020', '03/16/2020', '03/16/2020', '05/10/2020', '07/28/2020', '09/03/2020',
    '14/09/2020']
    y = [0.0025, 0.00881983, 0.0025, 0.009436, 0.01069436, 0.01213136, 0.00925736, 0.01503343,
    0.01803343, 0.02103343, 0.02603343, 0.01353343, 0.02703343, 0.03065149]
    
    x1 = ['16/11/2018', '27/12/2018', '07/01/2019', '18/03/2019', '13/05/2019', '19/09/2019',
    '16/03/2020', '7/09/2020', '7/09/2020']
    y1 = [10, 15, 20, 30, 32, 52, 115.27, 165.27, 160.79]
    
    #Converting first data to dataframe and date to datetime format
    df = pd.DataFrame([x,y]).T
    df.columns = ['Date','Data1']
    df['Date'] = pd.to_datetime(df['Date'])
    
    #Converting second data to dataframe and date to datetime format
    df1 = pd.DataFrame([x1,y1]).T
    df1.columns = ['Date','Data2']
    df1['Date'] = pd.to_datetime(df1['Date'])
    
    #Merging both dataframes to have one common dataset 
    complete_df = df.merge(df1, on='Date', how='outer')
    complete_df.set_index('Date',inplace=True)
    
    #Sorting the data according to Date
    complete_df = complete_df.sort_index()
    
    #Taking care of the nan values coming due to a common timeseries
    complete_df.fillna(method='ffill',inplace=True)
    
    ##Plotting
    ax = complete_df['Data1'].plot(lw=3)
    complete_df['Data2'].plot(ax=ax, lw=3,secondary_y=True)
    plt.show()
    

    enter image description here