gpt4 book ai didi

python - 显示时钟时间时如何降低 xticks 标签分辨率

转载 作者:太空宇宙 更新时间:2023-11-03 15:08:46 24 4
gpt4 key购买 nike

我的 x 轴上的标签显示了一天中的时钟时间。每当我放大绘图时,它们看起来很傻,6 位小数都为 0。我对显示微秒一点都不感兴趣。

在放大之前查看 xticks 标签: enter image description here

放大后: enter image description here

这是我今天的内容:

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 标签如下所示:

enter image description here

最佳答案

我终于找到了:

ax.xaxis.set_major_formatter(DateFormatter('%H:%M'))

here .


更好的是,上面的解决方案在缩小时不再显示天数:

from matplotlib.dates import AutoDateFormatter, AutoDateLocator

xtick_locator = AutoDateLocator()
xtick_formatter = AutoDateFormatter(xtick_locator)
xtick_formatter.scaled[30.] = '%d %b'
xtick_formatter.scaled[1.] = '%d %b'
xtick_formatter.scaled[1/24.] = '%d/%m %H:%M'
xtick_formatter.scaled[1/(24.*60.)] = '%d/%m %H:%M'
xtick_formatter.scaled[1/(24.*60.*60.)] = '%d/%m %H:%M:%S'
ax_av.xaxis.set_major_locator(xtick_locator)
ax_av.xaxis.set_major_formatter(xtick_formatter)

This帮了大忙(虽然没有参数的例子 AutoDateFormatter 对我不起作用)

关于python - 显示时钟时间时如何降低 xticks 标签分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29766441/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com