要格式化MM-DD
,您可以指定fmt
参数,该参数接受 strftime 格式字符串进行格式化,并且是DateFormatter
.
您还可interval
以为DateLocator
. 例如interval=7
,定位器在每 7 个日期放置一次刻度。
import datetime
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
df = pd.read_csv("1.csv", delim_whitespace=True)
df['data'] = pd.to_datetime(df['data'], format='%m/%d/%Y')
fig, ax = plt.subplots(figsize=(10, 6))
ax.bar(df['data'], df['casosNovos'])
ax.xaxis.set_major_locator(mdates.DayLocator(interval=7))
ax.xaxis.set_major_formatter(mdates.DateFormatter(fmt='%m%d'))
ax.set_xlim(df['data'].min(), datetime.date(2020, 8, 21))
plt.setp(ax.get_xticklabels(), rotation = 45)
plt.show()
1.csv
好像
regiao data casosNovos
Brasil 2/25/2020 0
Brasil 2/26/2020 1
Brasil 2/27/2020 0
Brasil 2/28/2020 0
Brasil 2/29/2020 1
Brasil 3/1/2020 0
Brasil 3/2/2020 0
Brasil 3/3/2020 0