2017-03-29 1 views
1

xaxis에 날짜를 넣을 때 작동하는 솔루션을 찾는 데 어려움을 겪고 있습니다. 인터넷에서 데이터를 가져 오지 않으면 모든 순간이 메모장 파일에서 나옵니다. 내가 찾은 해결책 중 많은 부분이 나에게 오류를주고, 내가하는 방법을 설명하는 youtube 비디오를 보았는데, 모두 urllib을 사용하여 생각하는 정보를 끌어 낸다.matplotlib + tkinter를 사용하여 그래프의 x 축에 날짜를 설정하십시오.

Fig = Figure(figsize=(10,4), dpi=80) 
a = Fig.add_subplot(111) 
Fig.subplots_adjust(left=0.1, right=0.974, top=0.9, bottom=0.1) 
Fig.patch.set_visible(False) 
a.title.set_text('Graph') 
a.set_xlabel('Date') 
a.set_ylabel('Cost (GBP)') 

위의 코드는 아래 코드를 사용하여 덮어 쓰기 전에 그래프를 작성해야하는 코드입니다.

#importing tkinter libraries 
import tkinter as tk 
from tkinter.ttk import Combobox,Treeview,Scrollbar 

#importing matplotlib libraries 
import matplotlib.pyplot as plt 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg 
from matplotlib.figure import Figure 
import matplotlib.animation as animation 
from matplotlib import style 
import matplotlib.dates as mdates 

#Other libraries 
import hashlib 
import sqlite3 
import os 
import time 
import datetime 

어떻게 날짜를 추가 가겠어요 :

def animate_graph(self, i): 
    pullData = open('financeData.txt','r').read() 
    dataList = pullData.split('\n') 
    xAxisList = [] 
    yAxisList = [] 
    taxList = [] 
    outgoingsList = [] 
    for eachLine in dataList: 
     if len(eachLine) > 1: 
     x, y, o= eachLine.split(',') 
     xAxisList.append(int(x)) 
     yAxisList.append(int(y)) 
     intY = int(y) 
     taxGraphData = intY * TAXRATE 
     taxList.append(taxGraphData) 
     outgoingsList.append(int(o)) 
    a.clear() 
    a.plot(xAxisList, yAxisList, label='Profits line', color='green') 
    a.plot(xAxisList, outgoingsList, label='Outgoings line') 
    a.plot(xAxisList, taxList, label='Tax line') 
    a.title.set_text('Pyraknight Finance Graph') 
    a.set_xlabel('Date') 
    a.set_ylabel('Cost (GBP)') 
    a.legend() 

그리고 내가

canvas = FigureCanvasTkAgg(Fig, graphFrame) 
canvas.show() 
canvas.get_tk_widget().grid(row=0,column=0,padx=10,pady=10,sticky='nsew') 

self.ani = animation.FuncAnimation (Fig, self.animate_graph, interval=1000) 

이 아래의 코드를 사용했습니다 내 프레임에 그래프를 배치하는 메신저 가져 오기는 라이브러리입니다 내 x 축에? 미리 감사드립니다, 나스

답변

1

나는 현재 시간을 거슬러 가장 최근의 전체 시간부터 계산 매일 하나라고 표시된 눈금을 그릴 것이

import datetime 

ax = plt.gca() 
plt.gcf().autofmt_xdate(rotation=30) 
#stepsize = 2592000 # 30 days 
#stepsize = 864000 # 10 days 
stepsize = 86400 # 1 day 
#stepsize = 3600 # 1 hour 
start, end = ax.get_xlim() 
ax.xaxis.set_ticks(np.arange((end - end%3600), start, -stepsize)) 
def timestamp(x, pos): 
     return (datetime.datetime.fromtimestamp(x)).strftime('%Y-%m-%d') 
     #return (datetime.datetime.fromtimestamp(x)).strftime('%m/%d %H:%M') 
ax.xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(timestamp)) 

이 사용하고 있습니다.

샘플 :

enter image description here enter image description here

참조 :

http://strftime.org/

http://matplotlib.org/api/ticker_api.html

http://matplotlib.org/api/ticker_api.html#matplotlib.ticker.FuncFormatter

0,123,516

http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.autofmt_xdate

http://matplotlib.org/devdocs/api/_as_gen/matplotlib.axis.XAxis.set_ticks.html

관련 문제