I'm still relatively new to Python and I'm having a bit of trouble with the axis formatter for matplotlib
.
My axis uses datetime
values and shows hours, minutes, seconds, and then decimal seconds. I want to get rid of the decimals and be able to show Year/day/month Hour:minute:second on the x-axis or something similar. To start with, I just tried to get Year/month/day. However, DateFormatter
is seeming to have no effect.
I got much of my information from these two links: format dates and Matplotlib instruction
My shortened code is below:
tft
is a list of lists (hence tft[event_num]
) of the datetime
type and spans over the course of several weeks with data to the nearest second.
import matplotlib.pyplot as plt
from Tkinter import *
from PIL import ImageTk, Image
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np
from images_to_list import images_to_list
from tkFileDialog import askopenfilename, askdirectory
import matplotlib.dates as mdates
extra = Tk()
extra.update()
class App:
def __init__(self, master):
self.event_num = 1
# Create a container
self.frame = Frame(master)
self.fig = Figure()
self.fig.set_size_inches(9,7)
self.ax = self.fig.add_subplot(111)
self.ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
self.fig.autofmt_xdate()
self.line, = self.ax.plot(tft[self.event_num],tf1[self.event_num],'.')
self.line2, = self.ax.plot(tft[self.event_num],tf2[self.event_num],'.')
self.ax.set_ylim([0,3.5])
self.canvas = FigureCanvasTkAgg(self.fig,master=self.frame)
self.canvas.get_tk_widget().grid(row=3,column=0,columnspan=4)
self.frame.grid(row=0,column=0)
app = App(extra)
extra.mainloop()
I think you need to set the major formatter for the xticks
:
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S'))
Note that fmt_xdata
appears to change the format of the mouseover values on the x axis, not the ticks themselves.