Efrat Amir-Caspi
Efrat Amir-Caspi

Reputation: 141

How to plot day and month

I have a chart of a daily trend over time.

Price Over Time

The year is not relevant here and I want to show only day and month. I know you can show year and month but that is not the case.

I tried to create a new variable called "Day_Month":

import datetime as dt
df['Day'] =  df['date'].dt.day
df['Month'] =  df['date'].dt.month
df['Day_Month'] = df['Day'].astype(str) + "-" + 

but it's not possible to plot it as a string nor to convert it to date type.

eventually, I would like my chart to look like this:

Final Price chart

Upvotes: 5

Views: 10545

Answers (1)

Efrat Amir-Caspi
Efrat Amir-Caspi

Reputation: 141

The answer I found is:

import matplotlib.dates as mdates
import matplotlib.pyplot as plt 

fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(date, price , label="Price")
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d'))

Upvotes: 9

Related Questions