seaborn: Cannot use dates on x-axis of lmplot
I would like to use lmplot to visualize polling data for multiple candidates as demonstrated in this notebook, but I get TypeError: invalid type promotion.
I can convert the dates to a Days integer, but then I don’t get the date-based axis labels I want. This kind of use of dates is handled transparently when I use R and ggplot2.
Here is the code inline:
import pandas as pd
import seaborn as sns
import StringIO
DATA = """Date,Candidate,Percent
2014-06-01,Smith,43
2014-06-01,Perkins,33
2014-06-05,Smith,44
2014-06-05,Perkins,31
2014-06-10,Smith,49
2014-06-10,Perkins,28
2014-06-12,Smith,49
2014-06-12,Perkins,25
2014-06-19,Smith,49
2014-06-19,Perkins,27"""
df = pd.read_csv(StringIO.StringIO(DATA), parse_dates=["Date"])
# This gives the TypeError
sns.lmplot("Date", "Percent", df, hue="Candidate")
# Converting the date objects to an integer works, but gives
# an innapropriately formatted table.
df["Day"] = df["Date"].map(lambda d: d.day)
sns.lmplot("Day", "Percent", df, hue="Candidate")
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Reactions: 2
- Comments: 18 (3 by maintainers)
It is a well-established technique to use a lowess regression combined with a scatterplot to show election polling trend lines:
http://elections.huffingtonpost.com/pollster/2016-general-election-trump-vs-clinton
Is there a good way of doing this kind of plot in Seaborn? The naive approach doesn’t work:
+1 for support for pandas
datetime64on the x-axis of anlmplot. Converting the datetime64 to seconds-since-1970 (https://stackoverflow.com/questions/13703720/converting-between-datetime-timestamp-and-datetime64/13704307#13704307) lets me run thelmplotand gives me a useful model.