[英]How to create a scatter plot with timestamps
如何使用timestamps
生成散點圖?
下面是一個示例,但出現錯誤ValueError: First argument must be a sequence
import pandas as pd
import matplotlib.pyplot as plt
d = ({
'A' : ['08:00:00','08:10:00'],
'B' : ['1','2'],
})
df = pd.DataFrame(data=d)
fig = plt.figure()
x = df['A']
y = df['B']
plt.scatter(x,y)
如果我將時間戳轉換為總秒數,則可以使用:
df['A'] = pd.to_timedelta(df['A'], errors="coerce").dt.total_seconds()
但是我希望24小時是在x軸上,而不是總秒數。
使用x標記:
d = ({
'A' : ['08:00:00','08:10:00'],
'B' : [1,2],
})
df = pd.DataFrame(data=d)
fig = plt.figure()
x = df['A']
y = df['B']
x_numbers = list(pd.to_timedelta(df['A'], errors="coerce").dt.total_seconds ())
plt.scatter(x_numbers, y)
plt.xticks(x_numbers, x)
plt.show()
這會將時間戳記標記為x軸上的刻度。
請注意,我將數據框中的“ B”列轉換為數字而不是字符串。 如果確實需要將它們作為字符串或從文件中導入為字符串,則只需將其轉換為數字即可,例如通過eval
函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.