python - matplotlib axes bar plot unable to plot bars next to each other due to indexing x-axis from pandas df -


i want plot 2 bars side side each other keep getting error:

valueerror: cannot shift no freq

this error occurred when set x in axes.bar x-width.

here code:

df.date_1 = pd.to_datetime(df.date_1) df_percent.date_1 = pd.to_datetime(df_percent.date_1)  df = df.set_index(df['date_1']).sort_index()  df_percent = df_percent.set_index(['date_1']).sort_index() df_percent = df_percent.reindex(df.index).fillna(0)  fig, ax = plt.subplots(figsize=(10, 8)) ax.plot( df.index, df.line1,label='line1', c='b') ax.plot( df.index, df.line2,label='line2', c='r')  ax2=ax.twinx()  #i added x-10 bar chart want shift right ax2.bar(df_percent.index, df_after, width=10, alpha=0.1, color='r', label='after') ax2.bar(df_percent.index-10, df_before, width=10, alpha=0.1, color='g', label='before') 

if stacked bar chart works fine.


           date_1                       line1                         line2 date_1                                                               2014-06-01 2014-06-01                        65                       66 2014-07-01 2014-07-01                        68                       70 2014-08-01 2014-08-01                        62                       65 2014-09-01 2014-09-01                        62                       76 2014-10-01 2014-10-01                        63                       66 2014-11-01 2014-11-01                        79                       80 2014-12-01 2014-12-01                        80                       50 2015-02-01 2015-02-01                        70                       72 2015-03-01 2015-03-01                        67                       67 2015-04-01 2015-04-01                        69                       60 2015-05-01 2015-05-01                        66                       83            date_1                     before                after date_1                                                       2014-06-01 2014-06-01                 19.80                15.37 2014-07-01 2014-07-01                 62.82                44.87 2014-08-01 2014-08-01                 36.70                27.52 2014-09-01 2014-09-01                 56.18                34.27 2014-10-01 2014-10-01                 16.31                10.95 2014-11-01 2014-11-01                 32.35                14.71 2014-12-01 2014-12-01                 53.33                26.67 2015-02-01 2015-02-01                 44.44                17.78 2015-03-01 2015-03-01                 23.08                23.08 2015-04-01 2015-04-01                 36.84                15.79 2015-05-01 2015-05-01                 46.58                13.70 

i think error message (interpreted time index cannot shift integer no time frequency attached) complains last line of code df_percent.index-10. tries tell pandas subtract integer 10 pd.datetimeindex, not defined.

by -10, means shifting datetime index 10 days? (use df_percent.index - pd.tseries.offsets.dateoffset(10, 'day') if want) or index excluding first 10 days? (use df_percent.index[10:])

for pandas plot bars side-by-side:

import pandas pd import matplotlib.pyplot plt  # data # =========================================  print(df)                  date_1  line1  line2 date_1                               2014-06-01  2014-06-01     65     66 2014-07-01  2014-07-01     68     70 2014-08-01  2014-08-01     62     65 2014-09-01  2014-09-01     62     76 2014-10-01  2014-10-01     63     66 2014-11-01  2014-11-01     79     80 2014-12-01  2014-12-01     80     50 2015-02-01  2015-02-01     70     72 2015-03-01  2015-03-01     67     67 2015-04-01  2015-04-01     69     60 2015-05-01  2015-05-01     66     83   print(df_percent)                  date_1  before  after date_1                                2014-06-01  2014-06-01   19.80  15.37 2014-07-01  2014-07-01   62.82  44.87 2014-08-01  2014-08-01   36.70  27.52 2014-09-01  2014-09-01   56.18  34.27 2014-10-01  2014-10-01   16.31  10.95 2014-11-01  2014-11-01   32.35  14.71 2014-12-01  2014-12-01   53.33  26.67 2015-02-01  2015-02-01   44.44  17.78 2015-03-01  2015-03-01   23.08  23.08 2015-04-01  2015-04-01   36.84  15.79 2015-05-01  2015-05-01   46.58  13.70  # plot # ======================================== fig, ax = plt.subplots(figsize=(14, 8)) df[['line1', 'line2']].plot(ax=ax, color=['b', 'r']) ax2 = ax.twinx() df_percent[['before', 'after']].plot(kind='bar', ax=ax2, color=['r', 'g'], alpha=0.1) 

enter image description here


Comments