Can I build a linear regression with datetimes on the x axis with the seabed?

My DataFrame looks like

            amount
date    
2014-01-06  1
2014-01-07  1
2014-01-08  4
2014-01-09  1
2014-01-14  1

I would like to get some scatter plot over time along the x axis and an amount on y with a line through the data to guide the visual eye. If I use the panadas plot df.plot(style="o"), this is not entirely correct because there is no line there. I would like something like examples here .

+4
source share
2 answers

Since Siborn has problems with dates, I'm going to create a workaround. First, I will make the Date column my index:

# Make dataframe
df = pd.DataFrame({'amount' : [1,
                               1,
                               4,
                               1,
                               1]},
                  index = ['2014-01-06',
                           '2014-01-07',
                           '2014-01-08',
                           '2014-01-09',
                           '2014-01-14'])

Second, convert the index to pd.DatetimeIndex:

# Make index pd.DatetimeIndex
df.index = pd.DatetimeIndex(df.index)

And replace it with the original:

# Make new index
idx = pd.date_range(df.index.min(), df.index.max())

-, (idx):

# Replace original index with idx
df = df.reindex(index = idx)

NaN , :

df edit

-, Seaborn , count , x:

# Insert row count
df.insert(df.shape[1],
          'row_count',
          df.index.value_counts().sort_index().cumsum())

-, , "row_count" x "amount" y:

# Plot regression using Seaborn
fig = sns.regplot(data = df, x = 'row_count', y = 'amount')

-, , x, row_count, x-tick :

# Change x-ticks to dates
labels = [item.get_text() for item in fig.get_xticklabels()]

# Set labels for 1:10 because labels has 11 elements (0 is the left edge, 11 is the right
# edge) but our data only has 9 elements
labels[1:10] = df.index.date

# Set x-tick labels
fig.set_xticklabels(labels)

# Rotate the labels so you can read them
plt.xticks(rotation = 45)

# Change x-axis title
plt.xlabel('date')

plt.show();

plot editing 2

, !

+2

: , , . DataFrame, , .

datetime, . :

Seaborn , . , , .

df['date_ordinal'] = pd.to_datetime(df['date']).apply(lambda date: date.toordinal())

dataframe with ordinals

ax = seaborn.regplot(
    data=df,
    x='date_ordinal',
    y='amount',
)
# Tighten up the axes for prettiness
ax.set_xlim(df['date_ordinal'].min() - 1, df['date_ordinal'].max() + 1)
ax.set_ylim(0, df['amount'].max() + 1)

X ,

ax.set_xlabel('date')
new_labels = [date.fromordinal(int(item)) for item in ax.get_xticks()]
ax.set_xticklabels(new_labels)

regression line chart

-!

+1

All Articles