如果我使用下面的代碼並打印x是什麼,我會得到1900,1,1,10,29,34的每個時間戳,我有一定的時間寫成hh:mm:ss格式。我怎樣才能拿走年月日?因爲我想以格式hh:mm:ss在Python中使用datetime
有時間編輯:更新整個代碼,因爲它現在看起來來自評論的幫助。
import matplotlib.pyplot as plt
import matplotlib.ticker
import time
import datetime
x = ['10:29:55', '10:34:44']
sy1 = [679.162, 679.802]
x_labels = [datetime.datetime.strptime(elem, '%H:%M:%S') for elem in x]
formatter = matplotlib.ticker. FixedFormatter(x_labels)
plt.gca().xaxis.set_major_formatter(formatter)
plt.gca().xaxis.set_minor_formatter(formatter)
plt.plot(x_labels, sy1, 'ro')
plt.xlabel('Time')
plt.ylabel('Position')
plt.show()
但顯然它也顯示了考慮到年份,月份和日期的時間。
Plotting (wrong) time against y values
如果我使用strptime的strftime的,而不是我得到一個類型錯誤:描述信息的strftime'需要 'datetime.date' 對象,但收到了 'STR'
那麼,解析日期時間的問題,還是與情節?分開這兩件事並獨立解決它們。 – deceze
嘗試'plt.plot(x,y)'。 x和y已經是列表,matplotlib在這種情況下不需要2d列表。 – vroomfondel
您的編輯已將對圖像的引用刪除。你想使用matplotlib繪圖嗎?您可能需要https://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.autofmt_xdate。如果你可以編輯你正在使用的圖庫,你可能會讓人願意回答。 –