容易使标签看起来很好的熊猫密谋

时间:2015-07-18 15:20:22

标签: python pandas matplotlib

查看截图。我有一个DataTime索引,一个我每季度重新采样的数据帧,但是当我只绘制日期时间结束时,所有这些都是00.00.00因为它们精确到第二个。

任何提示?这里的代码(在MacOS上使用Jupyter和Vagrant):

import pandas as pd
import numpy as np
%matplotlib nbagg
pd.options.display.mpl_style = 'default'

#get
df = pd.read_csv('MC_720_061813_071715.CSV')
#format
df.index = pd.to_datetime(df.Date)
df.drop(['Status', 'Date'], axis = 1, inplace = True)
df.fillna(0, inplace = True)
df.Credit = df.Credit.apply(lambda x: float(str(x).replace(',','')))
#process
df2 = df.resample('Q', how = sum)
#graph
df2.plot(kind = 'bar')

matplotlib plot

1 个答案:

答案 0 :(得分:0)

您可以简单地在x轴上重命名这些刻度标签。

import pandas as pd
import numpy as np

pd.options.display.mpl_style = 'default'

# generate some artificial data
# ======================================
date_rng = np.random.choice(pd.date_range('2012-01-01', '2015-12-31', freq='D'), 2000)
val = np.random.randint(1, 10, size=2000)
df = pd.DataFrame({'Date': date_rng, 'Credit':val}).set_index('Date').sort_index()

# before: same problem as you saw
# ======================================
df.resample('Q', how=sum).plot(kind='bar')

enter image description here

# processing
# ======================================
df1 = df.resample('Q', how=sum)
labels = ['{}Q{}'.format(y,q) for y,q in zip(df1.index.year, df1.index.quarter)]
ax = df1.plot(kind='bar')
ax.set_xticklabels(labels)

enter image description here