Reputation: 3
In pandas, my dataframe has the following structure:
raw_data = {'date': ['1975-07-03','1975-07-03','1975-07-04','1975-08-01'],
'time': [515,1014,1401,1201], 'value': [1,-1,2,11]}
df = pd.DataFrame(raw_data, columns = ['date', 'time', 'value'])
This question is similar to this one, but I cannot figure out how to modify it.
I need to plot the values in the column "value" versus the two columns "date" and "time". Note that here "time" really is hh:mm.
Edit
Since the year does not change on the x-axis I should have date and time in the format "Month-Day Hour:Minute"
Upvotes: 0
Views: 5567
Reputation: 1007
Extending the other answer to include marking specific data points as ticklabels/ticks can be done by using date2num to convert the dates into their tick positions. There are probably better ways to manipulate the date formatting in matplotlib but this method will work.
EDIT: Ensure padding of hhmm if less than 4 characters, more ideomatic pandas
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
raw_data = {'date': ['1975-07-03','1975-07-03','1975-07-04','1975-08-01'],
'time': [415,1014,1401,1201], 'value': [1,-1,2,11]}
def fix_time_str(df):
df['date'] = (df['date'] + ' ' +
df['time'].apply(lambda x: str(x).zfill(4)).replace(r'(\d){2})(\d{2})', r'\1:\2'))
return df
df = (pd.DataFrame(raw_data, columns = ['date', 'time', 'value'])).pipe(fix_time_str).assign(date= lambda x: pd.to_datetime(x['date']))
fig, ax = plt.subplots(1,1, figsize = (8,5))
xtick_locs = mpl.dates.date2num(df['date'].tolist())
xtick_labels = df['date'].astype(str).tolist()
xtick_labels = ["{}-{}".format(*i.split('-')[1:])[:-3] for i in xtick_labels]
ax.plot(df['date'], df['value'])
ax.set_xticks(xtick_locs)
ax.set_xticklabels(xtick_labels)
ax.tick_params(axis='x', rotation=90)
Upvotes: 0
Reputation: 210842
IIUC:
(df.assign(date=pd.to_datetime(df['date'] + ' ' + df['time'].astype(str).replace(r'(\d){2})(\d{2})', r'\1:\2')))
.plot(x='date', y='value'))
Upvotes: 2