Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing datetime-like object to seaborn.lmplot

I am trying to do a plot of values over time using seaborn linear model plot but I get the error

TypeError: invalid type promotion

I have read that it is not possible to plot pandas date objects, but that seems really strange given seaborn requires you pass a pandas DataFrame to the plots.

Below is a simple example. Does anyone know how I can get this to work?

import pandas as pd
import seaborn as sns; sns.set(color_codes=True)
import matplotlib.pyplot as plt

date = ['1975-12-03','2008-08-20', '2011-03-16']
value = [1,4,5]
df = pd.DataFrame({'date':date, 'value': value})
df['date'] = pd.to_datetime(df['date'])

g = sns.lmplot(x="date", y="value", data=df, size = 4, aspect = 1.5)

I am trying to do a plot like this one I created in r using ggplot hence why I want to use sns.lmplot enter image description here

like image 545
Robin Avatar asked Feb 19 '18 06:02

Robin


1 Answers

You need to convert your dates to floats, then format the x-axis to reinterpret and format the floats into dates.

Here's how I would do this:

import pandas
import seaborn
from matplotlib import pyplot, dates
%matplotlib inline

date = ['1975-12-03','2008-08-20', '2011-03-16']
value = [1,4,5]
df = pandas.DataFrame({
    'date': pandas.to_datetime(date),   # pandas dates
    'datenum': dates.datestr2num(date), # maptlotlib dates
    'value': value
})

@pyplot.FuncFormatter
def fake_dates(x, pos):
    """ Custom formater to turn floats into e.g., 2016-05-08"""
    return dates.num2date(x).strftime('%Y-%m-%d')

fig, ax = pyplot.subplots()
# just use regplot if you don't need a FacetGrid
seaborn.regplot('datenum', 'value', data=df, ax=ax)

# here's the magic:
ax.xaxis.set_major_formatter(fake_dates)

# legible labels
ax.tick_params(labelrotation=45)

enter image description here

like image 147
Paul H Avatar answered Oct 08 '22 05:10

Paul H