4

Here's what I'm trying to recreate in Seaborn (this was done with Matplotlib)

enter image description here

I see in Seaborn you could use a regplot() but this one is for scatter charts. Anyway, if I try to use it, it won't work because the x are datetime, so I did a mixture of Seaborn and Matplotlib, it works, but I don't like it, I think there must be a better way, Seaborn only.

x = range(0, len(hom.fecha))

plt.figure(figsize=(12, 9))
plt.style.use('fivethirtyeight')

chart = sns.lineplot(x='fecha', y='n', data=df, 
                     hue='sexo', markers=True)
chart.set(title='Personas Migrantes', ylabel='# Personas', xlabel="Fecha")

# Linear regressions for each sex
z = np.polyfit(x, hom.n, 1)
p = np.poly1d(z)
plt.plot(hom.fecha, p(x), c="b", ls=":")

z = np.polyfit(x, muj.n, 1)
p = np.poly1d(z)
plt.plot(hom.fecha, p(x), c="r", ls=':')

I get this picture:

enter image description here

Which I think is much more pleasant than the first one, but I just don't get how to add the trend lines using only seaborn.

Any idea?

=== EDIT ===

If I use regplot() it throws an exception...

sns.regplot(x="fecha", y="n", data=df)

This one... (it plots something tho, like a scatter chart, with points)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-70-0f60957abc3f> in <module>
----> 1 sns.regplot(x="fecha", y="n", data=df)

blah blah blah

TypeError: unsupported operand type(s) for *: 'Timestamp' and 'float'
luisfer
  • 1,927
  • 7
  • 40
  • 54
  • I don't understand what's wrong with the plot you get out. – ImportanceOfBeingErnest Mar 07 '20 at 21:27
  • The plot is okay, but its written both with seaborn and matplotlib, and the colors are a bit off. I'd like something like `sns.lineplot(x='fecha', y='n', data=df, hue='sexo', linearregression=True)` – luisfer Mar 07 '20 at 21:30
  • 2
    That's what `regplot` is for (you don't need to show the dots). Since seaborn uses only matplotlib, every seaborn plot is a matplotlib plot. The colors are determined by the `fivethirtyeight` style (which is unrelated to seaborn), you can just [reset the color cycle](https://stackoverflow.com/questions/24193174/reset-color-cycle-in-matplotlib) to get the same colors as previous lines. – ImportanceOfBeingErnest Mar 07 '20 at 21:35
  • I tried to use `regplot`, let me show the errors, I'll edit the question – luisfer Mar 07 '20 at 21:43
  • 1
    seaborn doesn't support date variables as input for regression so one way or another you'll have to fall back to matplotlib. – ayhan Mar 07 '20 at 21:55
  • Is there any workaround? – luisfer Mar 07 '20 at 22:13
  • 2
    Oh well, the error tells you that you cannot use regplot with timestamp data. You could convert your timestamps to numbers, do the regression plot, then create a ticker and formatter for the axes. This will be much more work than just using matplotlib from the start though. – ImportanceOfBeingErnest Mar 07 '20 at 22:30
  • did you just give up? – Gonzalo Garcia Jun 26 '20 at 16:52
  • my hue parameter contains values which have spaces in between, e.g. 'C1 - coffee'. How to I solve this as I also want to plot a trendline for each line. – Nele Oct 23 '20 at 14:26

1 Answers1

2

I see this is a year old, but I just ran into and solved a similar problem so I'll leave the solution here.

The issue with your regplot is that you are using a column name as your x-axis, but (I assume), "fecha" is actually the name of your index and not the name of a column. If you were to take this line:

sns.regplot(x="fecha", y="n", data=df)

and change it to:

sns.regplot(x=df.index, y="n", data=df)

Then I would expect it to work. You may also want to take out the confidence interval which is drawn by default (add paramter ci=False) and change the color to be red or blue or whatever.

I know it's probably too late to help you, but I hope it helps someone!

inteoryx
  • 785
  • 2
  • 9