2

Having alot of trouble finding a solution that works when trying to put dates along the xaxis. Im not pulling any data from the internet, all of it is coming from a notepad file at the moment. Many of the solutions i have found give me errors, and the youtube videos i have watched explaining how to do this, all draw in information i think by using urllib.

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)')

the code above is the code i currently have to make the graph, before it get overwritten using the code below.

  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()

And then to place the graph onto my frame i have used the code below

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)

THese are the libraries im importing:

#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 

How would i go about adding dates to my x axis? Thanks in advance, Nas

1 Answer 1

2

I'm currently using this

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))

This will plot one labeled tick on every day counting from most recent whole hour back in time.

samples:

enter image description here enter image description here

reference:

http://strftime.org/

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

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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.