How to plot a linear regression with datetimes on the x-axis(如何在 x 轴上绘制带有日期时间的线性回归)
问题描述
我的 DataFrame 对象看起来像
数量日期2014-01-06 12014-01-07 12014-01-08 42014-01-09 12014-01-14 1我想要一种散点图,时间沿 x 轴,数量在 y 上,用一条线穿过数据来引导观察者的视线.如果我使用 pandas plot df.plot(style="o") 这不太正确,因为该行不存在.我想要
用日期轴上的序数绘制图
ax = seaborn.regplot(数据=df,x='date_ordinal',y='金额',)# 收紧坐标轴以保持美观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)哒哒!
My DataFrame object 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 a sort of scatter plot with time along the x-axis, and amount on the y, with a line through the data to guide the viewer's eye. If I use the pandas plot df.plot(style="o") it's not quite right, because the line is not there. I would like something like the examples here.
note: this has a lot in common with Ian Thompson's answer but the approach is different enough to have it be a separate answer. I use the DataFrame format provided in the question and avoid changing the index.
Seaborn and other libraries don't deal as well with datetime axes as you might like them to. Here's how I'd work around it:
Start by adding a column of date ordinals
Seaborn will deal better with these than with dates. This is a handy trick for doing all kind of mathy things with dates and libraries that don't love dates.
from datetime import date
df['date_ordinal'] = pd.to_datetime(df['date']).apply(lambda date: date.toordinal())
Make a plot with the ordinals on the date axis
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)
Replace the ordinal X-axis labels with nice, readable dates
ax.set_xlabel('date')
new_labels = [date.fromordinal(int(item)) for item in ax.get_xticks()]
ax.set_xticklabels(new_labels)
ta-daa!
这篇关于如何在 x 轴上绘制带有日期时间的线性回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 x 轴上绘制带有日期时间的线性回归
基础教程推荐
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
