def __init__(self, parent=None): super().__init__(parent) # 设置光标样式 # self.config(cursor='sb_h_double_arrow') self.make_widgets() self.handle = EventHandler(self.fig, self.canvas, self.ax) self.handle.connect()
class Fund(BaseFrame): def __init__(self, parent=None): super().__init__(parent) # 设置光标样式 # self.config(cursor='sb_h_double_arrow') self.make_widgets() self.handle = EventHandler(self.fig, self.canvas, self.ax) self.handle.connect() def make_widgets(self): self.fig, self.ax = plt.subplots() self.ax.yaxis.grid(linestyle="-.", color='silver') self.canvas = FigureCanvasTkAgg(self.fig, self) # self.draw() self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=tk.YES) def display(self, data): self.ax.cla() # self.ax.clear() # 根据回测日期选择显示的时间格式fmt # temp = [f['Time'] for f in data["Fund"]] x = [pd.Timestamp(int2date(f['Time'])) for f in data['Fund']] y = [f['DynamicEquity'] for f in data['Fund']] self.ax.set_xlim(0, len(y) - 1) self.handle.set_border() self.handle.set_x_labels(x) if data['KLineType'] == EEQU_KLINE_DAY: fmt = mdate.DateFormatter('%Y-%m') elif data['KLineType'] == EEQU_KLINE_MINUTE: fmt = mdate.DateFormatter('%Y-%m-%d') else: # 有待更改 fmt = mdate.DateFormatter('%Y-%m-%d') self.ax.plot(y, marker='.', color='red', linewidth=2.0, linestyle='-') self.ax.set_xticklabels(x) def format_date(x_, pos=None): if x_ < 0 or x_ > len(x) - 1: return return x[int(x_)] #TODO: length先暂时这样处理吧! length = 0 if len(x) > 0 and len(x) <= 100: length = 30 elif len(x) > 100 and len(x) <= 1000: length = 200 elif len(x) > 1000 and len(x) < 10000: length = 2000 else: length = 20000 self.ax.xaxis.set_major_formatter(ticker.FuncFormatter(format_date)) self.ax.xaxis.set_major_locator(ticker.MultipleLocator(length)) # 此处使用self.canvas.draw()会使程序崩溃 self.canvas.draw_idle()