I have a csv file like this:
trade_dt IC pValue
2011/2/28 0.005834749 0.794163818
2011/3/31 -0.016185318 0.465113735
2011/4/29 0.0535994 0.014731813
2011/5/31 -0.023620263 0.278485983
2011/6/30 -0.000771114 0.971613898
2011/7/29 -0.063454359 0.003181087
2011/8/31 0.13892734 6.60E-11
2011/9/30 0.003624149 0.864731069
2011/10/31 0.055471084 0.008852478
first,I read this file using pd.read_csv
.Second,I transform the trade_dt using pd.to_datetime()
.
When I plot using ax.xaxis.set_major_formatter(mdate.DateFormatter('%Y%m%d'))
,it shows, for example 2011-2-28 00:00
I have two questions:
- how to remove the minute and second in the
2011-2-28 00:00
- how to show the xticks every 6 month instead of show it every month
here is my code.Please ignore some code which is not related to this problem and the indent
def analysis_factor_ic(_df_ic):
corrData = _df_ic.copy()
corrData['monthly_IC'] = corrData.IC.rolling(window=5, center=False).mean()
corrData['xtick'] = range(len(corrData))
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
fig = plt.figure(figsize=(16, 6))
ax = fig.add_subplot(111)
sigPos = corrData[(corrData.IC > 0) & (corrData.pValue < 0.05)]
sigNeg = corrData[(corrData.IC < 0) & (corrData.pValue < 0.05)]
nonSig = corrData[~(corrData.pValue < 0.05)]
ax.bar(sigPos.xtick, sigPos.IC, align='center', color='r', label=u'显著为正')
ax.bar(nonSig.xtick, nonSig.IC, align='center', color='grey', label=u'不显著')
ax.bar(sigNeg.xtick, sigNeg.IC, align='center', color='g', label=u'显著为负')
plt.legend(loc='best')
ax.xaxis.set_major_formatter(mdate.DateFormatter('%Y%m%d'))
plt.xticks(corrData.xtick[1:-1:3], corrData.index[1:-1:3])
plt.xlim(-1, len(corrData))
fig.autofmt_xdate(rotation=-90, ha='center')
plt.grid(axis='y')
plt.title(u'因子Rank IC值')
plt.show()
df_ic = pd.read_csv('dfic.csv')
df_ic['trade_dt'] = pd.to_datetime(df_ic['trade_dt'], format='%Y-%m-%d')
df_ic.set_index(df_ic['trade_dt'], inplace=True)
analysis_factor_ic(df_ic)