python 在 x 轴上绘制带有日期时间对象的散点图
python plot a scatter plot with datetime object on the x axis
我有一个日期为字符串格式的数据框。我使用 strptime
将它们转换为 datetime 对象
y 是一个浮点数
然后我使用plt.plot_date尝试制作数据的散点图
但我不断收到 float() argument must be a string or a number, not 'datetime.date'
我的完整代码如下:
from datetime import datetime
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
def plot():
data = []
df = pd.read_csv('random_case_number.csv')
for index, row in df.iterrows():
if (row['recipt_number'][: 3] != 'IOE'):
data.append([int(row['recipt_number'][5:]) - 90000000, datetime.strptime(row['date'], '%Y-%m-%d').date()])
dates = mdates.date2num(data[1])
plt.plot_date(dates, data[0])
示例数据:
recipt_number,date
IOE0914808418,1998-01-01
MSC2290138517,2021-11-18
LIN2190425860,2021-07-09
LIN2290143717,2022-02-22
WAC2190082175,2021-05-03
IOE9086351290,2022-01-06
IOE0912562803,2021-07-09
IOE9086351290,2022-01-06
SRC2290076110,2022-01-24
IOE0913043187,1322-07-27
非常感谢任何帮助
不要遍历您的 DataFrame。尝试:
df = pd.read_csv('random_case_number.csv')
df["date"] = pd.to_datetime(df["date"])
df["recipt"] = df["recipt_number"].str[5:].astype(int).sub(90000000).where(~df["recipt_number"].str.startswith("IOE"))
ax = df.plot.scatter(x="date",y="recipt",rot=90)
我有一个日期为字符串格式的数据框。我使用 strptime
将它们转换为 datetime 对象y 是一个浮点数
然后我使用plt.plot_date尝试制作数据的散点图
但我不断收到 float() argument must be a string or a number, not 'datetime.date'
我的完整代码如下:
from datetime import datetime
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
def plot():
data = []
df = pd.read_csv('random_case_number.csv')
for index, row in df.iterrows():
if (row['recipt_number'][: 3] != 'IOE'):
data.append([int(row['recipt_number'][5:]) - 90000000, datetime.strptime(row['date'], '%Y-%m-%d').date()])
dates = mdates.date2num(data[1])
plt.plot_date(dates, data[0])
示例数据:
recipt_number,date
IOE0914808418,1998-01-01
MSC2290138517,2021-11-18
LIN2190425860,2021-07-09
LIN2290143717,2022-02-22
WAC2190082175,2021-05-03
IOE9086351290,2022-01-06
IOE0912562803,2021-07-09
IOE9086351290,2022-01-06
SRC2290076110,2022-01-24
IOE0913043187,1322-07-27
非常感谢任何帮助
不要遍历您的 DataFrame。尝试:
df = pd.read_csv('random_case_number.csv')
df["date"] = pd.to_datetime(df["date"])
df["recipt"] = df["recipt_number"].str[5:].astype(int).sub(90000000).where(~df["recipt_number"].str.startswith("IOE"))
ax = df.plot.scatter(x="date",y="recipt",rot=90)