Cannot use dates on x-axis of lmplot
See original GitHub issueI 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")
Issue Analytics
- State:
- Created 9 years ago
- Reactions:2
- Comments:18 (3 by maintainers)
Top Results From Across the Web
format x-axis (dates) in sns.lmplot() - python - Stack Overflow
I have found this question but I want the grouped plot, so I cannot use regplot, and the code plt.xticks(fake_dates) (following this answer) ......
Read more >How to display Month wise on x-axis usning seaborn - Kaggle
As you might have guessed, it helps us to specify the format in which we want to see the dates. Since, we'll be...
Read more >Multi-plot grid in Seaborn - GeeksforGeeks
Example 6: Here we'll create a 3×4 grid of subplot using subplots(), where all axes in the same row share their y-axis scale,...
Read more >seaborn.lmplot — seaborn 0.12.1 documentation - PyData |
It is intended as a convenient interface to fit regression models across conditional subsets of a dataset. When thinking about how to assign...
Read more >Fixing common date annoyances - Matplotlib
We'll load up some sample date data which contains datetime.date objects ... Use a more precise date string for the x axis locations...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
datetime64
on 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 thelmplot
and gives me a useful model.