question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot use dates on x-axis of lmplot

See original GitHub issue

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")

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:2
  • Comments:18 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
bwbensonjrcommented, Aug 24, 2016

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:

import pandas as pd
import seaborn as sns

p = pd.read_csv("http://elections.huffingtonpost.com/pollster/api/charts/2016-general-election-trump-vs-clinton.csv", parse_dates=["start_date", "end_date"])
sns.regplot(["Clinton", "Trump"], "end_date", data=p, lowess=True)
2reactions
ianozsvaldcommented, Aug 25, 2015

+1 for support for pandas datetime64 on the x-axis of an lmplot. Converting the datetime64 to seconds-since-1970 (https://stackoverflow.com/questions/13703720/converting-between-datetime-timestamp-and-datetime64/13704307#13704307) lets me run the lmplot and gives me a useful model.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found