我的x軸上的標籤顯示一天的時鐘時間。每當我放大圖,他們看起來很傻,小數點後6位都是0。我毫不感興趣地顯示微秒。如何在顯示時間時減少xticks標籤分辨率
見xticks標籤,在縮放前:
在放大後:
這是我今天:
ax_average.xaxis_date()
fig_average.autofmt_xdate()
fig_average.tight_layout()
我怎樣才能確保只有幾秒鐘(或更好的分鐘)顯示在滴答標籤?
下面是代碼的稍微簡化版本繪出以上:
# Requires python-matplotlib
import numpy
from pylab import *
raw_data = [
'Apr 20 12:14:36 a b temp_0 = 21.400000',
'Apr 20 12:14:36 a b temp_1 = 17.860000',
'Apr 20 12:14:36 a b temp_3 = 50.350006',
'Apr 20 12:14:51 a b temp_0 = 21.400000',
'Apr 20 12:14:51 a b temp_1 = 17.860000',
'Apr 20 12:14:51 a b temp_3 = 50.350006',
'Apr 20 12:15:06 a b temp_0 = 21.400000',
'Apr 20 12:15:06 a b temp_1 = 17.860000',
'Apr 20 12:15:06 a b temp_3 = 50.250000',
'Apr 20 12:15:21 a b temp_0 = 21.400000',
'Apr 20 12:15:21 a b temp_1 = 17.860000',
'Apr 20 12:15:21 a b temp_3 = 50.250000']
def month_string_to_int(input):
if (input == 'Jan'):
return 1
elif (input == 'Feb'):
return 2
elif (input == 'Mar'):
return 3
elif (input == 'Apr'):
return 4
elif (input == 'May'):
return 5
elif (input == 'Jun'):
return 6
elif (input == 'Jul'):
return 7
elif (input == 'Aug'):
return 8
elif (input == 'Sep'):
return 9
elif (input == 'Oct'):
return 10
elif (input == 'Nov'):
return 11
elif (input == 'Dev'):
return 12
else:
print "Error in the month string: %s" % input
return -1
data = {}
timestamps = {}
for line in raw_data:
line = line.strip() # strip newline character
lst = line.split() # space as separator
day = datetime.date(2015, month_string_to_int(lst[0]), int(lst[1]))
time = datetime.datetime.strptime(lst[2], "%H:%M:%S").time()
timestamp = datetime.datetime.combine(day, time)
identifier = lst[5]
value = float(lst[7])
if identifier not in data:
data[identifier] = []
timestamps[identifier] = []
data[identifier].append(value)
timestamps[identifier].append(timestamp)
fig, ax = subplots()
plot(timestamps['temp_0'], data['temp_0'])
ax.xaxis_date()
fig.autofmt_xdate()
fig.tight_layout()
show()
的timestamps
字典看起來如我所料:
In [103]: timestamps
Out[103]:
{'temp_0': [datetime.datetime(2015, 4, 20, 12, 14, 36),
datetime.datetime(2015, 4, 20, 12, 14, 51),
datetime.datetime(2015, 4, 20, 12, 15, 6),
datetime.datetime(2015, 4, 20, 12, 15, 21)],
'temp_1': [datetime.datetime(2015, 4, 20, 12, 14, 36),
datetime.datetime(2015, 4, 20, 12, 14, 51),
datetime.datetime(2015, 4, 20, 12, 15, 6),
datetime.datetime(2015, 4, 20, 12, 15, 21)],
'temp_3': [datetime.datetime(2015, 4, 20, 12, 14, 36),
datetime.datetime(2015, 4, 20, 12, 14, 51),
datetime.datetime(2015, 4, 20, 12, 15, 6),
datetime.datetime(2015, 4, 20, 12, 15, 21)]}
的xticks標籤是這樣的:
你能夠發佈代碼來重現你的圖嗎?這可能是值得檢查['DateFormatter'](http://matplotlib.org/api/dates_api.html#matplotlib.dates.DateFormatter),它接受格式字符串 – EdChum
我會盡力讓一個最小的代碼示例。看到我的編輯,但我放大之前的時間格式是確定的。 – Gauthier
@EdChum:這是代碼。不幸的是沒有減少到最低限度,但希望很簡單,足以迅速獲得想法。 – Gauthier