당신은 np.recfromcsv으로 파일을로드 할 수 있습니다. 그런 다음 시간 열을 datetime 객체로 변환합니다.이 객체에 대해서는 convtime
함수를 정의합니다. 그런 다음이 함수를 사용하여 CSV 파일을 읽습니다. 우리가 datetime
에 그냥 시간 부분을 제공했기 때문에 당신이 걱정하는 경우가 그것에 해당 날짜를 추가 할 수 있습니다 1월 1900 1로,이 날짜를 가정 할 것이다
는
import numpy as np
import matplotlib.pyplot as plt
convtime = lambda x: datetime.datetime.strptime(x, "%H:%M")
all_records = np.recfromcsv("myfilename.csv", names=["time", "user", "val"], converters={0:convtime}) # This says parse column 0 using the convtime function
참고.
이제 데이터를 플롯합니다. 이것은 matplotlib이 플롯되는 모든 포인트에 대해 하나의 심볼 만 사용할 수있는 궁금한 문제를 야기합니다. 불행히도 이것은 우리가 for 루프를 사용해야 함을 의미한다. 첫째, 우리가 각 사용자의 기호와 색상 dict
의를 정의 할 수 있습니다 : 거의을 수행
symbols = {'user1':'*', 'user3':'o', 'empty':'x'}
colours = {'user1':'blue', 'user3':'red', 'empty':'orange'}
for rec in all_records:
plt.scatter(rec['time'], rec['val'], c=colours[rec['user']], marker=symbols[rec['user']])
있다. 우리는 여전히 전설을 놓치고 있습니다. 이 for 루프의 단점은 파일의 모든 행이 범례에 하나의 항목을 작성한다는 것입니다. 우리는 커스텀 범례를 만들어 이것을 이겼습니다.
import matplotlib.lines as mlines
legend_list = []
for user in symbols.keys():
legend_list.append(mlines.Line2D([], [], color=colours[user], marker=symbols[user], ls='none', label=user))
plt.legend(loc='upper right', handles=legend_list)
plt.show()
그건 그렇습니다! 플롯이 찌그러진 것처럼 보일 경우 plt.xlim()
을 사용하여 맛을 조정하십시오.
출처
2017-04-27 09:32:15
VBB