[Matplotlib-users] contourf with data frames

nuncio nuncio.m at gmail.com
Fri Oct 13 06:14:04 EDT 2017


HI,
I am trying to plot a time height diagram. here is the code used

dates=mpl.dates.date2num(data.index.to_pydatetime())
xh=list(dates)
dfX=pd.DataFrame(xh)
date2= dfX.groupby(np.arange(len(dfX))//400).mean()
dates=num2date(date2)
dfT=pd.DataFrame(data.iloc[:,5].reshape(400,427).T)
#dfT.insert(loc=0,column='',value=date)
hgtl=data.iloc[0:400:,1]
#plot(dates,dfT.iloc[:,0])
contourf(dates,hgtl,dfT.iloc[0:400:,0].T)

this results in the following error
TypeError                                 Traceback (most recent call last)
<ipython-input-133-d1e220b1ce6a> in <module>()
      8 hgtl=data.iloc[0:400:,1]
      9 #plot(dates,dfT.iloc[:,0])
---> 10 contourf(dates,hgtl,dfT.iloc[0:400:,0].T)

/home/nuncio/anaconda2/lib/python2.7/site-packages/matplotlib/pyplot.pyc in
contourf(*args, **kwargs)
   2781         ax.hold(hold)
   2782     try:
-> 2783         ret = ax.contourf(*args, **kwargs)
   2784     finally:
   2785         ax.hold(washold)

/home/nuncio/anaconda2/lib/python2.7/site-packages/matplotlib/__init__.pyc
in inner(ax, *args, **kwargs)
   1810                     warnings.warn(msg % (label_namer,
func.__name__),
   1811                                   RuntimeWarning, stacklevel=2)
-> 1812             return func(ax, *args, **kwargs)
   1813         pre_doc = inner.__doc__
   1814         if pre_doc is None:

/home/nuncio/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc
in contourf(self, *args, **kwargs)
   5650             self.cla()
   5651         kwargs['filled'] = True
-> 5652         return mcontour.QuadContourSet(self, *args, **kwargs)
   5653     contourf.__doc__ = mcontour.QuadContourSet.contour_doc
   5654 

/home/nuncio/anaconda2/lib/python2.7/site-packages/matplotlib/contour.pyc in
__init__(self, ax, *args, **kwargs)
   1422         are described in QuadContourSet.contour_doc.
   1423         """
-> 1424         ContourSet.__init__(self, ax, *args, **kwargs)
   1425 
   1426     def _process_args(self, *args, **kwargs):

/home/nuncio/anaconda2/lib/python2.7/site-packages/matplotlib/contour.pyc in
__init__(self, ax, *args, **kwargs)
    861         self._transform = kwargs.get('transform', None)
    862 
--> 863         self._process_args(*args, **kwargs)
    864         self._process_levels()
    865 

/home/nuncio/anaconda2/lib/python2.7/site-packages/matplotlib/contour.pyc in
_process_args(self, *args, **kwargs)
   1443                 self._corner_mask =
mpl.rcParams['contour.corner_mask']
   1444 
-> 1445             x, y, z = self._contour_args(args, kwargs)
   1446 
   1447             _mask = ma.getmask(z)

/home/nuncio/anaconda2/lib/python2.7/site-packages/matplotlib/contour.pyc in
_contour_args(self, args, kwargs)
   1526             args = args[1:]
   1527         elif Nargs <= 4:
-> 1528             x, y, z = self._check_xyz(args[:3], kwargs)
   1529             args = args[3:]
   1530         else:

/home/nuncio/anaconda2/lib/python2.7/site-packages/matplotlib/contour.pyc in
_check_xyz(self, args, kwargs)
   1555         y = self.ax.convert_yunits(y)
   1556 
-> 1557         x = np.asarray(x, dtype=np.float64)
   1558         y = np.asarray(y, dtype=np.float64)
   1559         z = ma.asarray(args[2], dtype=np.float64)

/home/nuncio/anaconda2/lib/python2.7/site-packages/numpy/core/numeric.pyc in
asarray(a, dtype, order)
    472 
    473     """
--> 474     return array(a, dtype, copy=False, order=order)
    475 
    476 def asanyarray(a, dtype=None, order=None):

*TypeError: float() argument must be a string or a number*

I identify this as the problem with using "dates" in contourf , however I do
not find any problem in running

plot(dates,dfT.iloc[:,0])

How to get around this

THanks and regards
nuncio



--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html


More information about the Matplotlib-users mailing list