我正在尝试绘制嵌入到tkinter的散点图,并使用for循环对其进行更新。我可以更新我的情节;但是,当我的程序初始化绘图时,颜色栏颜色覆盖了图形的某些部分。这是我的代码:
import matplotlib as mtpl
import matplotlib.pyplot as plt
import numpy as np
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from mpl_toolkits.axes_grid1 import make_axes_locatable
mtpl.use("TkAgg")
class RealTime(object):
def __init__(self, master):
self.master = master
self.lf = tk.LabelFrame(self.master, text="Plot")
self.lf.grid(row=0, column=0, padx=3, pady=3)
plt.ion()
self.fig = plt.Figure()
self.axis = self.fig.add_subplot(111)
data = np.arange(2, 0, -0.1).reshape(5, 4)
divider = make_axes_locatable(self.axis)
self.cax = divider.append_axes('right', size='3%', pad=0.4, add_to_figure=True)
im = self.axis.imshow(data, cmap="GnBu")
self.fig.colorbar(im, cax=self.cax, orientation='vertical')
self.axis.set_xlim(-1, 1)
self.axis.set_ylim(-1.65, 1.65)
self.axis.set_xlabel('Y-Direction')
self.axis.set_ylabel('X-Direction')
self.axis.set_title("Real Time Position")
self.canvas = FigureCanvasTkAgg(self.fig, master=self.lf)
self.canvas.get_tk_widget().grid(row=0, column=0, padx=3, pady=3)
self.canvas.draw()
self.master.protocol("WM_DELETE_WINDOW", self.on_closing)
def plot(self, data):
s = self.axis.scatter(data[:, 0], data[:, 1], s=10, c=data[:, 2], cmap=plt.get_cmap("GnBu"), vmin=0, vmax=2)
self.axis.legend(['Position'])
self.fig.canvas.draw()
self.fig.canvas.flush_events()
您知道我的图上涂有颜色条颜色吗? 最好的问候
解决方案
添加颜色条后添加self.axis.cla()
可解决此问题。但是我仍然不明白为什么我以前有过这种行为?