Home > Back-end >  pandas / matplotlib : How do I show all years on the x-axis of my plot?
pandas / matplotlib : How do I show all years on the x-axis of my plot?

Time:03-10

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.markers as markers
from datetime import datetime as dt

df = pd.DataFrame(
    {
        'date': ['2011-01-01', '2011-02-01', '2012-01-01', '2012-02-01', ],
        'amount': [100, 200, 250, 150,],
    }
)
df.date = pd.to_datetime(df.date)

df_TotalDay = df[['date','amount']].copy(deep=True)
df_TotalDay = df_TotalDay.groupby('date').amount.sum()
df_TotalDay

fig, ax = plt.subplots(figsize=(15,5))
ax.plot(df_TotalDay.index, df_TotalDay.values)
plt.xticks([x for x in df_TotalDay.index])
plt.show()

simple plot

In the plot above, I just want to display the unique year. Here, it would just be '2011' and '2012' instead of the actual dates.

I've tried

plt.xticks([x for x in df_TotalDay.index.year.unique()])

but this didn't work.

I know this looks a bit silly with the DataFrame above but my actual DataFrame is quite large and when I plot my data, it looks like this:

actual plot

In the plot above, matplotlib is not listing every year on the x-axis. I would like to include the missing years.

CodePudding user response:

You are very close. You should generate all the dates you want to show and then add to xticks:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(
    {
        'date': ['2011-01-01', '2013-02-01', '2016-01-01', '2018-02-01', ],
        'amount': [100, 200, 250, 150,],
    }
)
df.date = pd.to_datetime(df.date)
df_TotalDay = df[['date','amount']].copy(deep=True)
df_TotalDay = df_TotalDay.groupby('date').amount.sum()

fig, ax = plt.subplots(figsize=(15,5))
ax.plot(df_TotalDay.index, df_TotalDay.values)
plt.xticks(pd.date_range(df_TotalDay.index.min(), df_TotalDay.index.max(), freq='YS'))
plt.show()

To show only year, you could use

import matplotlib.dates as mdates
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))

enter image description here

CodePudding user response:

An alternative to Z Li answer is to use matplotlibs dates.YearLocator

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
from datetime import datetime as dt

df = pd.DataFrame(
    {
        'date': ['2011-01-01', '2011-02-01', '2012-01-01', '2012-02-01', ],
        'amount': [100, 200, 250, 150,],
    }
)
df.date = pd.to_datetime(df.date)

df_TotalDay = df[['date','amount']].copy(deep=True)
df_TotalDay = df_TotalDay.groupby('date').amount.sum()

fig, ax = plt.subplots(figsize=(15,5))
ax.plot(df_TotalDay.index, df_TotalDay.values)
ax.set_xlabel('Year')
ax.minorticks_off()
ax.xaxis.set_major_locator(matplotlib.dates.YearLocator())
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%Y"))
  • Related