問題描述
我想繪制一些時間戳(年-月-日時-分-秒格式).我正在使用以下代碼,但它沒有顯示任何小時-分鐘-秒信息,它將它們顯示為 00-00-00.我仔細檢查了我的日期數組,從下面的代碼片段可以看出,它們不為零.
I want to plot some timestamps (Year-month-day Hour-Minute-Second format). I am using the following code, however it doesn't show any hour-minute-second information, it shows them as 00-00-00. I double checked my date array, and as you can see from the snippet below, they are not zero.
你知道我為什么會得到 00-00-00 的任何想法嗎?
Do you have any idea about why I am getting 00-00-00's?
import matplotlib.pyplot as plt
import matplotlib.dates as md
import dateutil
dates = [dateutil.parser.parse(s) for s in datestrings]
# datestrings = ['2012-02-21 11:28:17.980000', '2012-02-21 12:15:32.453000', '2012-02-21 23:26:23.734000', '2012-02-26 17:42:15.804000']
plt.subplots_adjust(bottom=0.2)
plt.xticks( rotation= 80 )
ax=plt.gca()
xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)
plt.plot(dates[0:10],plt_data[0:10], "o-")
plt.show()
推薦答案
嘗試放大圖表,您會看到日期時間隨著 x 軸比例的變化而擴大.
Try zooming in on your graph, you will see the datetimes expand as your x axis scale changes.
在 matplotlib 中繪制 unix 時間戳
在嘗試繪制染色體上正選擇的熱圖時,我遇到了類似的煩人問題.如果我縮小得太遠,事情就會完全消失!
I had a similarly annoying problem when trying to plot heatmaps of positive selection on chromosomes. If I zoomed out too far things would disappear entirely!
此代碼完全按照您提供的日期繪制日期,但不會在兩者之間添加刻度.
edit: This code plots your dates exactly as you give them, but doesn't add ticks in between.
import matplotlib.pyplot as plt
import matplotlib.dates as md
import dateutil
datestrings = ['2012-02-21 11:28:17.980000', '2012-02-21 12:15:32.453000', '2012-02-21 23:26:23.734000', '2012-02-26 17:42:15.804000']
dates = [dateutil.parser.parse(s) for s in datestrings]
plt_data = range(5,9)
plt.subplots_adjust(bottom=0.2)
plt.xticks( rotation=25 )
ax=plt.gca()
ax.set_xticks(dates)
xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)
plt.plot(dates,plt_data, "o-")
plt.show()
這篇關于使用 Matplotlib 繪制時間戳(小時/分鐘/秒)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!