英文:
Changing xticks to the name of the months
问题
我有一个多层次索引系列,其外层索引是月份,内层索引是日期,假设它存储在系列中。
print(series)
请查看输出->output
所以,为了绘制图表,我使用以下代码:
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
print(series)
Please see the output->output
So to make plot i use the below code
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:
year = 2023 if len(series) == 365 else 2024 # leap year
dti = series.index.rename(['month', 'day']).to_frame().assign(year=year)
dti = pd.Index(pd.to_datetime(dti), name='Date')
series.index = dti
Output:
>>> series
Date
2023-01-01 -19.9
2023-01-02 -11.8
2023-01-03 -27.1
2023-01-04 -10.8
2023-01-05 -22.8
...
2023-12-27 -26.9
2023-12-28 -12.9
2023-12-29 -10.2
2023-12-30 -15.1
2023-12-31 -29.4
Length: 365, dtype: float64
Now, you can use a formatter and a locator:
import matplotlib.dates as mdates
ax = series.plot.line(rot=45)
locator = mdates.AutoDateLocator()
formatter = mdates.DateFormatter('%B')
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
plt.tight_layout()
plt.show()
英文:
Instead of use month only for display, maybe you should convert your MultiIndex
(Month, Day) as DatetimeIndex
for further analyze:
year = 2023 if len(series) == 365 else 2024 # leap year
dti = series.index.rename(['month', 'day']).to_frame().assign(year=year)
dti = pd.Index(pd.to_datetime(dti), name='Date')
series.index = dti
Output:
>>> series
Date
2023-01-01 -19.9
2023-01-02 -11.8
2023-01-03 -27.1
2023-01-04 -10.8
2023-01-05 -22.8
...
2023-12-27 -26.9
2023-12-28 -12.9
2023-12-29 -10.2
2023-12-30 -15.1
2023-12-31 -29.4
Length: 365, dtype: float64
Now, you can use a formatter and a locator:
import matplotlib.dates as mdates
ax = series.plot.line(rot=45)
locator = mdates.AutoDateLocator()
formatter = mdates.DateFormatter('%B')
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
plt.tight_layout()
plt.show()
答案2
得分: 0
如果只需要月份名称,可以使用 Series.droplevel
删除多级索引中的天数,并使用 DatetimeIndex.month_name
转换为日期时间:
series.index = pd.to_datetime(series.index.droplevel(1), format='%m').month_name()
series.plot.line(figsize=(20,10))
或者,如果需要更复杂的格式,例如带有月份名称的日期,请使用 matplotlib.dates.DateFormatter
将 MulitIndex
转换为日期时间:
from matplotlib.dates import DateFormatter
series.index = pd.to_datetime([f'2020-{m}-{d}' for m, d in series.index])
ax = series.plot.line(figsize=(20,10))
date_form = DateFormatter("%d-%B")
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
:
series.index = pd.to_datetime(series.index.droplevel(1), format='%m').month_name()
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:
from matplotlib.dates import DateFormatter
series.index = pd.to_datetime([f'2020-{m}-{d}' for m, d in series.index])
ax = series.plot.line(figsize=(20,10))
date_form = DateFormatter("%d-%B")
ax.xaxis.set_major_formatter(date_form)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论