1

I have a dataframe

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

The problem is the graph is showing the empty dates in between the index of my graph. I want to not show those though.

I have already done

df_temp.plot(kind='bar')

enter image description here

But the dates in the x-axis are in long format and I am not able to add the line

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

where I collapse the dates. I need to collapse the dates otherwise the x-axis ticks become unreadable when I add more dates to the dataframe.

I am looking for either one of two solutions. Not show in-between dates in the figure plot, or combine dates and show in a shorter format in the df.plot() example.

Edit Here is the json dump of the dataframe. You can load it with df_temp_new = pd.read_json(json_dict)

'{"Client $ Amt":{"1483401600000":20,"1483488000000":-20.4,"1483574400000":20.76,"1483920000000":79.5684759707,"1484006400000":20.123,"1484179200000":20.654,"1484265600000":-20.876,"1484611200000":203.1234,"1484697600000":20.654,"1484784000000":20.432,"1484870400000":204.432,"1485129600000":-20.543,"1485216000000":20.654,"1485388800000":108.106,"1485475200000":2151.18,"1485734400000":1515.12,"1485820800000":102.327,"1485907200000":573.41,"1486080000000":449.65,"1486339200000":48.9152,"1486684800000":268.7302,"1486944000000":415.744,"1487030400000":22.335167,"1487116800000":20.6546,"1487203200000":865.45,"1487635200000":43.23,"1487721600000":543.234,"1488153600000":154.476,"1488240000000":20,"1488326400000":20,"1488412800000":20,"1488499200000":280.17256}}'

Alex F
  • 2,086
  • 4
  • 29
  • 67

2 Answers2

0

You can pass in an axis to the pandas plot

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

So you can use all the matplotlib sorcery afterwards

Maarten Fabré
  • 6,938
  • 1
  • 17
  • 36
  • I gave it a shot but `ax.set_xticks(ax.get_xticks()[::2])` has no effect. I still see every date on the x-axis. – Alex F Apr 28 '17 at 13:42
0

I stumbled upon a post explaining how to handle this Pandas bar plot changes date format.

The code that worked for me is

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))
Community
  • 1
  • 1
Alex F
  • 2,086
  • 4
  • 29
  • 67