Seaborn: Distplot with datetimes not possible?

Created on 17 Oct 2016  路  4Comments  路  Source: mwaskom/seaborn

Trying to do just a basic histogram of datetimes.
Very minimal example:

sns.distplot(np.arange('2016-01', '2016-05', dtype='datetime64[D]'))

lib/python3.5/site-packages/seaborn/distributions.py in distplot(a, bins, hist, kde, rug, fit, hist_kws, kde_kws, rug_kws, fit_kws, color, vertical, norm_hist, axlabel, label, ax)
    190             line, = ax.plot(0, a.mean())
    191         else:
--> 192             line, = ax.plot(a.mean(), 0)
    193         color = line.get_color()
    194         line.remove()

lib/python3.5/site-packages/numpy/core/_methods.py in _mean(a, axis, dtype, out, keepdims)
     63         dtype = mu.dtype('f8')
     64
---> 65     ret = umr_sum(arr, axis, dtype, out, keepdims)
     66     if isinstance(ret, mu.ndarray):
     67         ret = um.true_divide(

TypeError: ufunc add cannot use operands with types dtype('<M8[D]') and dtype('<M8[D]')

Numpy 1.11.2 and Seaborn 0.7.1

This seems like it should be supported behavior, both on the seaborn and the numpy sides. Is there a clear workaround or anything that I'm missing?

Most helpful comment

I have just come across this issue. Why has it been closed without any solution? Plotting a histograms over dates is certainly something seaborn should be able to do.

All 4 comments

Well, specifically it's failing when it's trying to get the mean of the input data. I guess that numpy datetime objects don't support the mean operation.

Attempts to compute the mean of the input data happen at least twice. Once as part of a routine for selecting a default color that follows the axes color cycle, and once for computing the default number of bins. These can be avoided by specifying values:

sns.distplot(d, color=".5", bins=10)

This draws a plot without erroring, but the plot is "wrong". This is because the scipy/statsmodels kernel density estimate doesn't handle datetime objects (arguably, a gaussian density can't be defined on day-resolution data). You can avoid _that_ problem by passing kde=False. But at that point, it is probably easier to just use plt.hist.

I have just come across this issue. Why has it been closed without any solution? Plotting a histograms over dates is certainly something seaborn should be able to do.

I would like to reopen this issue as I think seaborn should support this kind of operation.

A possible workaround for this issue:

import datetime
import seaborn as sns
import matplotlib.pyplot as plt
# Generate dates -- change by your actual dates!
dates = [datetime.datetime.fromordinal(int(d)) for d in (43 * np.random.randn(200) + 737555)]

# Go back to ordinal numbers...
ordinal_dates = np.array([d.toordinal() for d in dates])

sns.distplot(ordinal_dates, kde=True)

# Now, get the positions of the xticks
ticks_locations, _ = plt.xticks();
# Get the labels for those positions
labels = [datetime.datetime.fromordinal(int(t)).date() for t in ticks_locations]

plt.xticks(ticks_locations, labels, rotation=90)
plt.title("Date distribution");
Was this page helpful?
0 / 5 - 0 ratings

Related issues

rrbarbosa picture rrbarbosa  路  3Comments

sungshine picture sungshine  路  3Comments

queryous picture queryous  路  4Comments

chanshing picture chanshing  路  3Comments

vinay-jayaram picture vinay-jayaram  路  3Comments