将xticks更改为月份的名称

huangapple go评论90阅读模式
英文:

Changing xticks to the name of the months

问题

我有一个多层次索引系列,其外层索引是月份,内层索引是日期,假设它存储在系列中。

  1. print(series)

请查看输出->output

所以,为了绘制图表,我使用以下代码:

  1. series.plot.line(figsize=(20,10))

我得到了,请查看输出->output

但我想要的是xticks应该是[一月,二月,......,十二月],而不是那些元组值,我该怎么做?

英文:

i have a multilevel index series whose outer level index is month and inner level index is day let say it is stored in series

  1. print(series)

Please see the output->output

So to make plot i use the below code

  1. series.plot.line(figsize=(20,10))

i am getting
please see the output->
output

but i want xticks to be [January, February,......,December] instead of those tuple values how can i do that???

答案1

得分: 1

不要回答我要翻译的问题。以下是要翻译的内容:

"Instead of use month only for display, maybe you should convert your MultiIndex (Month, Day) as DatetimeIndex for further analyze:

  1. year = 2023 if len(series) == 365 else 2024 # leap year
  2. dti = series.index.rename(['month', 'day']).to_frame().assign(year=year)
  3. dti = pd.Index(pd.to_datetime(dti), name='Date')
  4. series.index = dti

Output:

  1. >>> series
  2. Date
  3. 2023-01-01 -19.9
  4. 2023-01-02 -11.8
  5. 2023-01-03 -27.1
  6. 2023-01-04 -10.8
  7. 2023-01-05 -22.8
  8. ...
  9. 2023-12-27 -26.9
  10. 2023-12-28 -12.9
  11. 2023-12-29 -10.2
  12. 2023-12-30 -15.1
  13. 2023-12-31 -29.4
  14. Length: 365, dtype: float64

Now, you can use a formatter and a locator:

  1. import matplotlib.dates as mdates
  2. ax = series.plot.line(rot=45)
  3. locator = mdates.AutoDateLocator()
  4. formatter = mdates.DateFormatter('%B')
  5. ax.xaxis.set_major_locator(locator)
  6. ax.xaxis.set_major_formatter(formatter)
  7. plt.tight_layout()
  8. plt.show()

将xticks更改为月份的名称

英文:

Instead of use month only for display, maybe you should convert your MultiIndex (Month, Day) as DatetimeIndex for further analyze:

  1. year = 2023 if len(series) == 365 else 2024 # leap year
  2. dti = series.index.rename(['month', 'day']).to_frame().assign(year=year)
  3. dti = pd.Index(pd.to_datetime(dti), name='Date')
  4. series.index = dti

Output:

  1. >>> series
  2. Date
  3. 2023-01-01 -19.9
  4. 2023-01-02 -11.8
  5. 2023-01-03 -27.1
  6. 2023-01-04 -10.8
  7. 2023-01-05 -22.8
  8. ...
  9. 2023-12-27 -26.9
  10. 2023-12-28 -12.9
  11. 2023-12-29 -10.2
  12. 2023-12-30 -15.1
  13. 2023-12-31 -29.4
  14. Length: 365, dtype: float64

Now, you can use a formatter and a locator:

  1. import matplotlib.dates as mdates
  2. ax = series.plot.line(rot=45)
  3. locator = mdates.AutoDateLocator()
  4. formatter = mdates.DateFormatter('%B')
  5. ax.xaxis.set_major_locator(locator)
  6. ax.xaxis.set_major_formatter(formatter)
  7. plt.tight_layout()
  8. plt.show()

将xticks更改为月份的名称

答案2

得分: 0

如果只需要月份名称,可以使用 Series.droplevel 删除多级索引中的天数,并使用 DatetimeIndex.month_name 转换为日期时间:

  1. series.index = pd.to_datetime(series.index.droplevel(1), format='%m').month_name()
  2. series.plot.line(figsize=(20,10))

或者,如果需要更复杂的格式,例如带有月份名称的日期,请使用 matplotlib.dates.DateFormatterMulitIndex 转换为日期时间:

  1. from matplotlib.dates import DateFormatter
  2. series.index = pd.to_datetime([f'2020-{m}-{d}' for m, d in series.index])
  3. ax = series.plot.line(figsize=(20,10))
  4. date_form = DateFormatter("%d-%B")
  5. ax.xaxis.set_major_formatter(date_form)
英文:

If need only months names is possible remove days from Multiindex by Series.droplevel and convert to datetimes with DatetimeIndex.month_name:

  1. series.index = pd.to_datetime(series.index.droplevel(1), format='%m').month_name()
  2. series.plot.line(figsize=(20,10))

Or if need more complex format, e.g. days with month names use matplotlib.dates.DateFormatter with convert MulitIndex to datetimes:

  1. from matplotlib.dates import DateFormatter
  2. series.index = pd.to_datetime([f'2020-{m}-{d}' for m, d in series.index])
  3. ax = series.plot.line(figsize=(20,10))
  4. date_form = DateFormatter("%d-%B")
  5. ax.xaxis.set_major_formatter(date_form)

huangapple
  • 本文由 发表于 2023年6月9日 14:23:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定