0

I have a problem where I have multiple pandas data frames where the index is a date_range with the format %m-%d-%y and each data frame represents one year of data, each data frame for a separate year. I would like to overlay the data in a plot only as a function of month and day, such that each plot is on top of the other. However, when I try to plot them, the resulting plot places each curve side by side, because they are in different years. An example is shown below.

Below the pseudo code posted in this question is an example of the real code output. I am trying to get the blue plot to be on top of the red plot, but, the code always wants to account for the year and place the blue plot to the right of the red one instead of on top of it for the same month-year data points. Any advice on how to fix this would be greatly appreciated.

import matplotlib
import matplotlib.dates as dates 
from matplotlib import pyplot as plt 

data1 = {'Date': ['2009-01-01', '2009-01-02', '2009-01-03', ...], 'Data': [4.0, 3.1, 1.2, ...]}
df1 = pd.DataFrame(data1)
df1['Date'] = pd.to_datetime(df['Date'])
df1 = df1.drop(['Date'], axis=1)

data2 = {'Date': ['20010-01-01', '20010-01-02', '20010-01-03', ...], 'Data': [4.0, 3.1, 1.2, ...]}
df2 = pd.DataFrame(data2)
df2['Date'] = pd.to_datetime(df2['Date'])
df2 = df2.drop(['Date'], axis=1)

fig, td_plot = plt.subplots()
td_plot.xaxis.set_major_formatter(dates.DateFormatter('%m-%d'))
td_plot.plot(df1.index, df1['Data'])
td_plot.plot(df2.index, df2['Data'])
plt.show()

enter image description here

Jon
  • 1,621
  • 5
  • 23
  • 46
  • 1
    Your code is not reproducible. You also don't have a sample of what you want your chart to look like. Please read [this](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) on how to provide something we can help you with. – Ukrainian-serge Mar 18 '20 at 01:50

1 Answers1

0

If you're not interested in the year information you can just 'ignore' it using .strftime(). Eg:

import pandas as pd
import matplotlib
import matplotlib.dates as dates 
from matplotlib import pyplot as plt 

data1 = {'Date': ['2009-01-01', '2009-05-02', '2009-09-03'], 'Data': [4.0, 3.1, 1.2]}
df1 = pd.DataFrame(data1)
df1['Date'] = pd.to_datetime(df1['Date'])
df1.index = df1['Date']
df1 = df1.drop(['Date'], axis=1)

data2 = {'Date': ['2010-01-01', '2010-05-02', '2010-09-03'], 'Data': [8.0, 2.1, 3.2]}
df2 = pd.DataFrame(data2)
df2['Date'] = pd.to_datetime(df2['Date'])
df2.index = df2['Date']
df2 = df2.drop(['Date'], axis=1)

# Plot using day, month and year
fig, ax = plt.subplots()
ax.plot(df1.index, df1['Data'], label='Year x')
ax.plot(df2.index, df2['Data'], label='Year y')
plt.legend()
fig.set_size_inches(10,8)
plt.show()

Plot before dropping year info

Plot using .strftime('%d-%m') to ignore the year information:

fig, ax = plt.subplots()
ax.plot(df1.index.strftime('%d-%m'), df1['Data'], label='Year x')
ax.plot(df2.index.strftime('%d-%m'), df2['Data'], label='Year y')
plt.legend()
fig.set_size_inches(10,8)
plt.show()

enter image description here

Jason
  • 4,346
  • 10
  • 49
  • 75