self.scatter.set_sizes([size] * len(self.scatter.get_offsets()))
时间: 2024-03-06 16:46:16 浏览: 114
`self.scatter.set_sizes([size] * len(self.scatter.get_offsets()))` 是一个Matplotlib中scatter对象的方法,它将设置散点图中所有点的大小。
具体来说,`self.scatter.get_offsets()`方法返回散点图中所有点的坐标,`len(self.scatter.get_offsets())`将返回散点图中点的总数。然后,`[size] * len(self.scatter.get_offsets())`将创建一个大小为总点数的列表,其中每个元素都设置为`size`。最后,`self.scatter.set_sizes()`方法将使用这个列表来设置散点图中所有点的大小。
例如,如果您希望将散点图中所有点的大小设置为10,请使用以下代码:
```
size = 10
self.scatter.set_sizes([size] * len(self.scatter.get_offsets()))
```
这将使所有点的大小都为10。您可以将`size`变量设置为其他值,以更改点的大小。
相关问题
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画布上加一个滚轮,让图片不要被压缩显示,怎么改代码?
你可以在创建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()
```
优化这段代码 def run(self) -> bool: print('check_os_version') # debug if self._check_os_version(): print('uncompress_rom') # debug if self._uncompress_rom(): print('find_scatter_file') # debug if (scatter_file := self._find_scatter_file()) is not None: subp_run('adb reboot bootloader') print('check download status...') # debug if self._check_download_status(False): print('check download status...') # debug if self._download(scatter_file): print('check boot status...') # debug if self._check_download_status(): return wait_for_device(self.serial) return False
可以尝试使用嵌套的条件语句来简化代码,如下所示:
```python
def run(self) -> bool:
print('check_os_version') # debug
if not self._check_os_version():
return False
print('uncompress_rom') # debug
if not self._uncompress_rom():
return False
print('find_scatter_file') # debug
scatter_file = self._find_scatter_file()
if scatter_file is None:
return False
subp_run('adb reboot bootloader')
print('check download status...') # debug
if not self._check_download_status(False):
return False
print('check download status...') # debug
if not self._download(scatter_file):
return False
print('check boot status...') # debug
if not self._check_download_status():
return False
return wait_for_device(self.serial)
```
这样,我们将所有的判断条件都写在了第一层的 `if` 语句中,如果该条件不满足,直接返回 `False`。这样可以减少代码的缩进层数,让代码更加清晰易读。
阅读全文