简体   繁体   中英

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

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()

简单的情节

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:

实际情节

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

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'))

在此处输入图像描述

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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM