mwaskom / seaborn

Statistical data visualization in Python
https://seaborn.pydata.org
BSD 3-Clause "New" or "Revised" License
12.59k stars 1.93k forks source link

Test for datetime input on jointplot, code to pass test (#3664) #3742

Open athompson1991 opened 3 months ago

athompson1991 commented 3 months ago

The initial error comes from the _freedman_diaconis_bins function, which I fixed with a straightforward check on data type. However, once the code got through that hurdle, it runs into a more complicated blocker with an overflow error derived from matplotlib's handling of dates as integer. I get around the overflow issue with some manipulation of the object state, but some of the fix is admittedly imperfect. Nonetheless, this code alleviates the original problem seen with datetime64[ns] handling, see plot below for example code and plot:

import numpy as np
from seaborn import axisgrid as ag

dates = np.arange("2024-01-01", "2024-04-01", dtype="datetime64[D]").astype("datetime64[ns]")
random_dates = np.random.choice(dates, size=1000)
y = np.random.normal(size=1000)
plot = ag.jointplot(x=random_dates, y=y, kind="hex")

plot.fig.savefig("out.png")

out