使用matplotlib,如何绘制条形图而不显示日期索引值之间的日期?

时间:2017-04-28 13:05:04

标签: python matplotlib bar-chart

我有一个数据框

enter image description here

fig, ax = plt.subplots(figsize=(10, 8))
ax.bar(df_temp.index, df_temp['Client $ Amt'].cumsum()/100000)
ax.set_xticks(ax.get_xticks()[::2])

enter image description here

问题是图表显示了我的图表索引之间的空日期。我想不要展示那些。

我已经完成了

df_temp.plot(kind='bar')

enter image description here

但是x轴上的日期是长格式的,我无法添加行

ax.set_xticks(ax.get_xticks()[::2]) 

我崩溃的日期。我需要折叠日期,否则当我向数据框添加更多日期时,x轴刻度将变得不可读。

我正在寻找两种解决方案中的任何一种。图中没有显示日期之间的日期,或者在df.plot()示例中以较短的格式组合日期和显示。

修改 这是数据帧的json转储。您可以使用df_temp_new = pd.read_json(json_dict)

加载它

' {"客户$ Amt":{" 1483401600000":20," 1483488000000": - 20.4," 1483574400000&#34 ;:20.76" 14839.2亿":79.5684759707," 1484006400000":20.123," 1484179200000":20.654," 1484265600000": - 20.876,& #34; 1484611200000":203.1234" 1484697600000":20.654," 14847.84亿":20.432," 1484870400000":204.432," 1485129600000&#34 ;: - 20.543" 14852.16亿":20.654," 1485388800000":108.106," 1485475200000":2151.18" 1485734400000":1515.12,& #34; 1485820800000":102.327," 1485907200000":573.41" 14860.8亿":449.65" 1486339200000":48.9152" 1486684800000&#34 ;:268.7302" 14869.44亿":415.744," 1487030400000":22.335167," 1487116800000":20.6546" 1487203200000":865.45&# 34; 1487635200000":43.23" 1487721600000":543.234," 1488153600000":154.476," 14882.4亿":20," 1488326400000" :20," 1488412800000":20 " 1488499200000":280.17256}}'

2 个答案:

答案 0 :(得分:0)

您可以将axis传递给大熊猫情节

fig, ax = plt.subplots(figsize=(10, 8))
df_temp.plot(kind='bar', ax=ax)

所以你可以使用之后的所有matplotlib法术

答案 1 :(得分:0)

我偶然发现了一篇解释如何处理Pandas bar plot changes date format的帖子。

适用于我的代码是

import matplotlib.ticker as ticker

ax = (df_temp.cumsum()).plot(kind='bar')

ticklabels = ['']*len(df_temp.index)

# Every 4th ticklable shows the month and day
ticklabels[::4] = [item.strftime('%b %d') for item in df_temp.index[::4]]

ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))