4

I'm plotting a stacked bar plot from a Pandas DataFrame. The index dates are in datetime format and plot just fine. The issue I'm having is trying to set xlim values.

day_counts = {'a': count_a,
              'b': count_b,
              'c': count_c,
              'd': count_d}

df_days = pd.DataFrame(day_counts, index=date)

The variables count_a, .., count_d are lists of numbers and date is a list of datetime objects.

Plotting without an xlim parameter gives: enter image description here

Plotting with xlim attempt 1:

ax = df_days.plot(kind='bar', stacked=True,
                  xlim=[pd.Timestamp('2015-09-01'), pd.Timestamp('2016-01-01')])

Plotting with xlim attempt 2:

ax = df_days.plot(kind='bar', stacked=True)
ax.set_xlim(pd.Timestamp('2015-09-01'), pd.Timestamp('2016-01-01')) 

Plotting with xlim attempt 3:

ax = df_days.plot(kind='bar', stacked=True)
ax.set_xlim(datetime.datetime(2015,9,1),date[-1])

I would like to have the xlim command inside the main plot command if possible, the dataset is really big. Suggestions?

mjp
  • 1,618
  • 2
  • 22
  • 37
  • you didn't provide assignments for count_a, count_b, count_c, count_d, or date. Your dictionary assigns to 'a' twice and 'b' twice, clobbering the first assignment. – piRSquared May 26 '16 at 20:11
  • Oops, sorry that was just a simplifying-for-SO error, correcting it now – mjp May 26 '16 at 20:16
  • Old question, but why not applying the time range as a selection on the source dataframe *before* plotting, as such: `df_days[start:stop].plot(...)`? This will be faster than setting `xlim` (assuming you do not want a later change of x axis boundaries). – Joël Jan 10 '19 at 15:06
  • 1
    @Joël For me, this occurs because the limits are defined by the first series I plot (which only has data for the first three days in my month) and do not get extended even if the next series has more data; i.e., my plot changes x limits depending on which series I plot first, and the answer below produces a blank plot. – Marius Wallraff Oct 07 '21 at 07:35

1 Answers1

3

Per https://stackoverflow.com/a/31500017/4893407 the following should work:

ax.set_xlim(pd.Timestamp('2015-09-01'), pd.Timestamp('2016-01-01'))

Are you sure the index of your df is a DatetimeIndex? Does it have duplicates? Is it sorted? Unsorted DatetimeIndex will cause slice indexing with Timestamps to fail.

Community
  • 1
  • 1
Kyle
  • 2,814
  • 2
  • 17
  • 30
  • 1
    It just comes up with a blank plot, although I know that that time period exists in the data. `date[-1]` gives `datetime.datetime(2016, 4, 30, 0, 0)` so yep it's definitely a datetime list. I haven't sorted it myself but all the data will already be in day-order with no gaps – mjp May 26 '16 at 22:18
  • 2
    If I run that example and change kind to bar it stops working. I am having the OP's issue as well. – user815512 Jan 04 '18 at 18:13
  • 1
    true, the blank plot only appears for `bar` plots. has anyone found a solution? – caesarsol Apr 09 '21 at 13:56