1 回答
TA贡献1842条经验 获得超21个赞
您可以使用Date locators和Date formatters。
import numpy
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas
N = 192
numpy.random.seed(N)
dates = pandas.date_range('1/1/2004', periods=N, freq='m')
df = pandas.DataFrame(
data=numpy.random.randn(N),
index=dates,
columns=['A']
)
fig, ax = plt.subplots()
ax.plot(df.index, df['A'])
ax.xaxis.set_major_locator(mdates.YearLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
fig.autofmt_xdate()
plt.show()
interval是每次迭代之间的间隔。例如,如果interval=2,标记每两次出现一次。
import numpy
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas
N = 192
numpy.random.seed(N)
dates = pandas.date_range('1/1/2004', periods=N, freq='m')
df = pandas.DataFrame(
data=numpy.random.randn(N),
index=dates,
columns=['A']
)
fig, ax = plt.subplots()
ax.plot(df.index, df['A'])
ax.xaxis.set_major_locator(mdates.MonthLocator(interval=5))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
fig.autofmt_xdate()
plt.show()
添加回答
举报