JoshPickel
JoshPickel

Reputation: 45

Python pandas scatterplot of year against month-day

So I have a dataframe with a date column.

date
2021-06-17
2020-06-20

What I want to do is to do a scatterplot with the x-axis being the year, and the y-axis being month-day. So what I have already is this:

enter image description here

What I would like is for the y-axis ticks to be the actual month-day values and not the day number for the month-day-year. Not sure if this is possible, but any help is much appreciated.

Upvotes: 0

Views: 308

Answers (1)

Henry Ecker
Henry Ecker

Reputation: 35646

Some Sample Data:

import pandas as pd
from matplotlib import pyplot as plt, dates as mdates

# Some Sample Data
df = pd.DataFrame({
    'date': pd.date_range(
        start='2000-01-01', end='2020-12-31', freq='D'
    )
}).sample(n=100, random_state=5).sort_values('date').reset_index(drop=True)

Then one option would be to normalize the dates to the same year. Any year works as long as it's a leap year to handle the possibility of a February 29th (leap day).

This becomes the new y-axis.

# Create New Column with all dates normalized to same year
# Any year works as long as it's a leap year in case of a Feb-29
df['month-day'] = pd.to_datetime('2000-' + df['date'].dt.strftime('%m-%d'))

# Plot DataFrame
ax = df.plot(kind='scatter', x='date', y='month-day')
# Set Date Format On Axes
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))  # Year Only
ax.yaxis.set_major_formatter(mdates.DateFormatter('%m-%d'))  # No Year
plt.tight_layout()
plt.show()

plot

Upvotes: 1

Related Questions