나는 x 값이 utc 타임 스탬프 인 거대한 데이터 세트 (5E5 이상의 길이의 배열)를 계획하고있다. 예 : HH 대신 MM : SS 형식으로 변환하고 싶습니다. 1.47332886e + 09 초입니다. 따라서 타임 스탬프를 변환하는 작은 함수를 만들었습니다. 필자가 작성한 데이터 세트가 거대하기 때문에 모든 타임 스탬프를 datetime 튜플로 변환 할 수는 없습니다. 너무 오래 걸릴 것입니다. 그래서 xtick 값을 읽고 원하는 값으로 변환 할 수 있다고 생각했습니다. 문제는, 이렇게하면, x-tick 레이블은 고정되어 있고, 똑같은 x-tick 레이블을 확대하면됩니다. 따라서 기본적으로 줌 기능을 사용할 때마다 내 기능을 실행해야합니다. 나는 오히려 이것을 자동화하고 싶다. 그래서 이벤트 처리를 사용하려고했지만 이벤트 처리기에서 내 함수를 호출하는 방법을 찾을 수 없습니다.이벤트를 감지하고 사용자 정의 함수를 호출하여 Python matplotlib의 눈금 레이블을 편집하는 방법은 무엇입니까?
이벤트 처리기에서 함수를 올바르게 호출하려면 어떻게해야합니까? 아니면 내 목표를 달성하기위한 더 좋은 방법이 있습니까?
def timeStamp2dateTime(timeArray):
import datetime
import numpy as np
# first loop definition:
timeArrayExport = []
timeArrayExport = datetime.datetime.fromtimestamp(timeArray[0])
for i in range(1,len(timeArray)):
# Convert timestamps to datetime
timeArrayExport = np.append(timeArrayExport, datetime.datetime.fromtimestamp(timeArray[i]))
return(timeArrayExport)
def set_xticklabels_timestamp2time(ax):
'''
this function reads xtick values (assuming that they are timestamps) and
converts the xtick values to datetime format. from datetime format is
xticklabel list generated in format HH:MM:SS and also added to the plot
which has the handle "ax" (function input).
'''
import matplotlib.pyplot as plt
# manipulating the x-ticks -----
plt.pause(0.1) # update the plot
xticks = ax.get_xticks()
xticks_dt = timeStamp2dateTime(xticks)
xlabels = []
for item in xticks_dt:
xlabels.append(str(item.hour).zfill(2) +':'+ str(item.minute).zfill(2) +':'+ str(item.second).zfill(2))
ax.set_xticklabels(xlabels)
plt.gcf().autofmt_xdate() # rotates the x-axis values so that it is more clear to read
plt.pause(0.001) # update the plot
return(ax)
def onrelease(event):
ax = set_xticklabels_timestamp2time(ax)
import numpy as np
import matplotlib.pyplot as plt
# example data
x = np.arange(1.47332886e+09,1.47333886e+09) # UTC timestamps
y = np.sin(np.arange(len(x))/1000) + np.cos(np.arange(len(x))/100)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y,'.-')
ax.grid(True)
ax = set_xticklabels_timestamp2time(ax)
# try to automate the xtick label convertion
try:
cid = fig.canvas.mpl_connect('button_release_event', set_xticklabels_timestamp2time(ax))
except:
cid = fig.canvas.mpl_connect('button_release_event', onrelease)
# => both ways fails
이를 읽어 주셔서 감사합니다
(I 파이썬 3.3을 사용하고하는 것은) 여기 내 코드입니다!
감사합니다. 많은 pathoren 덕분에 정말 잘됩니다! –