добавление дат к оси X диаграммы с накоплением - PullRequest
0 голосов
/ 25 октября 2019

Я довольно новичок в Python и пытаюсь добавить даты в конец диаграммы с накоплением, но у меня возникают некоторые ошибки, которые я не могу устранить. Пример диаграммы, с которой я работаю: tried

Пока я нашел https://matplotlib.org/3.1.1/gallery/text_labels_and_annotations/date.html полезной, но я не могу реализовать некоторые части кода, потому что яЯ столкнулся с некоторыми ошибками, которые я не могу полностью устранить.

area_plot = (df.pivot(index='login_month',
          columns='user_month_created',
          values='cumulative_logins')
   .plot.area(figsize=(20,18))
)

#labels
plt.title('cumulative monthly logins by user creation cohort month')
plt.xlabel('login month')
plt.ylabel('cumulative monthly logins (in tens of millions)')

#ticks

# plt.xticks(x, 'bbbb')
years = mdates.YearLocator()   # every year
months = mdates.MonthLocator()  # every month
years_fmt = mdates.DateFormatter('%Y')

# format the ticks
area_plot.xaxis.set_major_locator(years)
area_plot.xaxis.set_major_formatter(years_fmt)
area_plot.xaxis.set_minor_locator(months)

# round to nearest years.
datemin = np.datetime64(df['login_month'][0], 'Y')
datemax = np.datetime64(df['login_month'][-1], 'Y') + np.timedelta64(1, 'Y')
area_plot.set_xlim(datemin, datemax)

plt.xticks()
plt.yticks(np.arange(0, 11000000, 250000))

plt.grid(True)
plt.plot()

Я ожидал, что годы будут отображаться на оси X (и я собирался отредактировать их, чтобы они отображались в формате «Mon YYYY»)), но я получаю ошибку ниже:

KeyError -1 ---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-102-5fb53d3f1bb7> in <module>
     24 # round to nearest years.
     25 datemin = np.datetime64(df['login_month'][0], 'Y')
---> 26 datemax = np.datetime64(df['login_month'][-1], 'Y') + np.timedelta64(1, 'Y')
     27 area_plot.set_xlim(datemin, datemax)
     28 

/opt/conda/envs/python3/lib/python3.6/site-packages/pandas/core/series.py in __getitem__(self, key)
    765         key = com._apply_if_callable(key, self)
    766         try:
--> 767             result = self.index.get_value(self, key)
    768 
    769             if not is_scalar(result):

/opt/conda/envs/python3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   3116         try:
   3117             return self._engine.get_value(s, k,
-> 3118                                           tz=getattr(series.dtype, 'tz', None))
   3119         except KeyError as e1:
   3120             if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: -1

Я действительно не знаю, что я делаю здесь неправильно.

Редактировать 1:

Мои login_month данные выглядят следующим образом

   signup_month  login_month  cumulative_logins
0  2016-01       2016-02      16
1  2016-01       2016-03      20
2  2016-01       2016-04      26
3  2016-01       2016-05      29
4  2016-02       2016-03      10
5  2016-02       2016-04      15
6  2016-02       2016-05      20
7  2016-03       2016-04      13
8  2016-03       2016-05      23
9  2016-04       2016-05      35

1 Ответ

1 голос
/ 25 октября 2019

Исключение:

KeyError: -1

указывает, что -1 не является допустимым ключом для df['login_month']. Я не знаком с модулем, который создает для вас df, но я бы рекомендовал либо просмотреть его документацию, либо выбросить все ключи df['login_month'], чтобы вы могли увидеть, какое допустимое использование может быть.

...