dates on pandas bar plot too long

38 views
Skip to first unread message

questio...@gmail.com

unread,
Jul 28, 2015, 1:17:07 PM7/28/15
to PyData
I would like to reduce the label on the attached plot. All that needs to show is the month and year.
Is there a simple way in pandas to do this?
Any feedback will be greatly appreciated.


import xray

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt


ds = xray.open_mfdataset('/DATA/*/*Temp.nc')


ds_variable=ds['Temp']

yearlymean=ds_variable.resample('1MS', dim='time', how='mean')

clim = yearlymean.groupby('time.month').mean('time')

anom = yearlymean - clim

meananom=anom.groupby('time').mean()

meananom.to_pandas().plot(kind='bar')

plt.show()


print "indexes:", meananom.indexes

print "values:", meananom.values

print "time (3 cases):", meananom.time[1:4]




 %run /Users/anomgroupbyquestion.py
indexes: time: DatetimeIndex(['2009-01-01', '2009-02-01', '2009-03-01', '2009-04-01',
                     '2009-05-01', '2009-06-01', '2009-07-01', '2009-08-01',
                     '2009-09-01', '2009-10-01', '2009-11-01', '2009-12-01',
                     '2010-01-01', '2010-02-01', '2010-03-01', '2010-04-01',
                     '2010-05-01', '2010-06-01', '2010-07-01', '2010-08-01',
                     '2010-09-01', '2010-10-01', '2010-11-01', '2010-12-01'],
                    dtype='datetime64[ns]', name=u'time', freq='MS', tz=None)
values: [ 6.29838684  5.45312294  3.57959444 -0.58853473 -3.08365321 -5.12876764
 -5.87386291 -4.17063811 -2.97671175 -1.57561442  5.12906158  4.71850501
  6.26112894  6.72438706  4.35283995  1.15254386 -2.85085821 -5.83142098
 -6.34662455 -6.10293792 -4.53418635 -0.96604974  2.28066161  4.07962832]
time (4 cases): <xray.DataArray 'time' (time: 3)>
array(['2009-02-01T11:00:00.000000000+1100',
       '2009-03-01T11:00:00.000000000+1100',
       '2009-04-01T11:00:00.000000000+1100'], dtype='datetime64[ns]')
Coordinates:
  * time     (time) datetime64[ns] 2009-02-01 2009-03-01 2009-04-01

Untitled.png

Joris Van den Bossche

unread,
Jul 29, 2015, 4:15:34 AM7/29/15
to PyData
There is an issue on github about datetime axis labeling for barplots: https://github.com/pydata/pandas/issues/1918 (this is indeed a problem currently)

In that issue are also some pointers for a manual solution. You have to use something like
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%m %d %Y'))

Joris

--
You received this message because you are subscribed to the Google Groups "PyData" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pydata+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages