如何在pandas中相互绘制两列数据框时更改日期格式?
例如,如果我跑:
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
np.random.seed(1)
dates = pd.date_range('1/1/2000', periods=50)
print('dates: {0}'.format(dates))
df = pd.DataFrame(np.random.randn(len(dates), 1), index=dates, columns=['A'])
print('df: {0}'.format(df))
plt.figure(figsize=(60,15))
df.plot(y='A', use_index=True)
plt.xticks(rotation=70)
plt.savefig('plot.png', dpi=300, bbox_inches='tight')
我获得:
如何控制xticks上显示的日期格式?
我是否应该将日期转换为字符串,如下所示,还是有更简洁的解决方案?
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
np.random.seed(1)
dates = pd.date_range('1/1/2000', periods=50)
print('dates: {0}'.format(dates))
# I have just changed the line below to introduce `.astype(str)`:
df = pd.DataFrame(np.random.randn(len(dates), 1), index=dates.astype(str), columns=['A'])
print('df: {0}'.format(df))
plt.figure(figsize=(60,15))
df.plot(y='A', use_index=True)
plt.xticks(rotation=70)
plt.savefig('plot2.png', dpi=300, bbox_inches='tight')