def scatter(self): self.scatter_frame = Frame(self.init_window_name) # 创建一个竖直滚动条 scrollbar = Scrollbar(self.scatter_frame, orient=tk.VERTICAL) scrollbar.pack(side=tk.RIGHT, fill=tk.Y) self.scatter_frame.place(x=10, y=460, width=750, height=310) fig = plt.figure() ax = fig.add_subplot(111) pd.plotting.scatter_matrix(self.df,alpha = 0.3,figsize = (10,10),grid = True,ax = ax) self.scatter_view = FigureCanvasTkAgg(fig,master = self.scatter_frame) self.scatter_view.draw() self.scatter_view.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1) # 将滚动条绑定到FigureCanvasTkAgg上 self.scatter_view.configure(yscrollcommand=scrollbar.set) scrollbar.config(command=self.scatter_view.yview)我想在self.scatter_view画布上加一个滚轮,让图片不要被压缩显示,怎么改代码?
时间: 2024-01-15 21:05:26 浏览: 97
m_scatter.rar_m.scatter_m_scater下载_m_scatter_scatter_scatterm ma
5星 · 资源好评率100%
你可以在创建FigureCanvasTkAgg时,将其参数resize设置为True,这样就可以自动调整大小,避免图片被压缩显示。同时,你需要在scatter_frame上添加鼠标滚动事件的绑定,用来处理滚轮事件。
修改后的代码如下:
```python
def scatter(self):
self.scatter_frame = Frame(self.init_window_name)
# 创建一个竖直滚动条
scrollbar = Scrollbar(self.scatter_frame, orient=tk.VERTICAL)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.scatter_frame.place(x=10, y=460, width=750, height=310)
fig = plt.figure()
ax = fig.add_subplot(111)
pd.plotting.scatter_matrix(self.df,alpha = 0.3,figsize = (10,10),grid = True,ax = ax)
# 将resize设置为True
self.scatter_view = FigureCanvasTkAgg(fig,master = self.scatter_frame, resize=True)
self.scatter_view.draw()
self.scatter_view.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
# 将滚动条绑定到FigureCanvasTkAgg上
self.scatter_view.configure(yscrollcommand=scrollbar.set)
scrollbar.config(command=self.scatter_view.yview)
# 添加鼠标滚动事件的绑定
self.scatter_view.mpl_connect('scroll_event', self.on_scroll)
def on_scroll(self, event):
# 获取当前滚轮的方向
if event.button == 'up':
direction = 1
elif event.button == 'down':
direction = -1
else:
direction = 0
# 根据滚轮方向调整缩放比例
scale = 1.1
if direction:
x, y = event.x, event.y
ax = self.scatter_view.figure.axes[0]
if direction > 0:
# 放大
ax.set_xlim(xdata - scale * (xdata - ax.get_xlim()[0]),
xdata + scale * (ax.get_xlim()[1] - xdata))
ax.set_ylim(ydata - scale * (ydata - ax.get_ylim()[0]),
ydata + scale * (ax.get_ylim()[1] - ydata))
else:
# 缩小
ax.set_xlim(xdata - scale * (xdata - ax.get_xlim()[0]),
xdata + scale * (ax.get_xlim()[1] - xdata))
ax.set_ylim(ydata - scale * (ydata - ax.get_ylim()[0]),
ydata + scale * (ax.get_ylim()[1] - ydata))
self.scatter_view.draw()
```
阅读全文