Tkinter:如何沿x轴调整日期?

时间:2013-11-07 19:01:07

标签: python-2.7 matplotlib plot tkinter pandas

我一直在修改一个接受DataFrame(具有日期时间索引)的函数,然后选择一个列并将其转换为一个系列,然后绘制它。一切似乎都很好,但我无法正确显示日期。我希望x轴按周而不是数据点。

Here is the output I am getting.

    class App: 
        def __init__(self,master, df): 
            frame = Frame(master) 
            frame.pack() 


            series = df['Daily  T'].cumsum()

            self.f = Figure(figsize=(12,6), dpi=100, linewidth=2) 
            self.a = self.f.add_subplot(111) 
            self.a.grid(True) 
            self.a.set_xlabel("DATES")
            self.a.plot(series, 'r-o') 

            self.a.set_xticks(range(len(series)))

            self.a.set_xticklabels([datetime.strftime(date, '%x') for date in series.index])

            self.f.autofmt_xdate()


            self.canvas = FigureCanvasTkAgg(self.f, master=child_win_plot) 
            self.canvas.show() 
            self.canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1) 





    app=App(child_win_plot, filtered_df) 

我不确定应该改变什么。

1 个答案:

答案 0 :(得分:1)

我无法测试,但这个怎么样

# range(0,len(series),7) give (0,7,14,21)
self.a.set_xticks(range(0,len(series),7)) 

和日期为空字符串

# empty string in place of some dates 
# [date, '', '', '', '' , '', '', '' , date ...]
xlabels = [ datetime.strftime(date, '%x') if index%7 == 0 else '' for index, date in enumerate(series.index)]
self.a.set_xticklabels(xlabels)

或仅限日期 - 没有空字符串

xlabels = [ datetime.strftime(date, '%x') for index, date in enumerate(series.index) if index%7 == 0 ]
self.a.set_xticklabels(xlabels)