选择并在矩形补丁内显示缩放时出错 [英] Error when select and show zoom inside rectangle patch

查看:24
本文介绍了选择并在矩形补丁内显示缩放时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在另一个面板中可视化(缩放)矩形块内的​​区域,当矩形移动时,单击鼠标我可以可视化矩形块内的​​区域

i try to visualize in another panel ( zoom) the area inside rectangle patch all time when the rectangle move witch click mouse i can visualize the area inside rectangle patch

问题是缩放没有显示矩形补丁内的所有区域只是一部分,或者我认为 x y

the problem is the zoom is not show the all the area inside rectangle patch just part or i think the x y

如何在绘图中移动鼠标时始终显示矩形块内的​​区域?

how can i show the area inside rectangle patch all time when i move mouse in plot ?

我的代码和显示的内容:

that my code and what is show :

import wx
from numpy import arange, sin, pi,cos
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.patches as patches

class MainFrame(wx.Frame):
    def __init__(self, parent ):
        wx.Panel.__init__(self, parent,name="Main", size = (800,800))
        self.Panel = Panel(self)


class Panel(wx.Panel):
    def __init__(self,parent):
        super().__init__(parent)
        panel = wx.Panel(self)
        self.canvas_panel = CanvasPanel(self)
        self.zoom_panel=Zoom(parent=self)
        canvas_sizer = wx.BoxSizer(wx.HORIZONTAL)
        canvas_sizer.Add(self.canvas_panel,1,wx.EXPAND)
        canvas_sizer.Add(self.zoom_panel,1,wx.EXPAND)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(panel)
        sizer.Add(canvas_sizer)
        self.SetSizerAndFit(sizer)
        self.Show()

class CanvasPanel(wx.Panel):
    """ Panel du graphique matplotlib """
    def __init__(self, parent , size=(200,350)):
        super().__init__(parent)
        self.figure = Figure(figsize =(4,4))
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.axes = self.figure.add_subplot(111)
        self.Size = self.canvas.Size
        self.parent = parent
        t = arange(0.5, 3.0, 0.01)
        s = cos(2 * pi * t)
        self.axes.plot(t, s)
        self.axes.get_xaxis().set_visible(False)
        self.axes.get_yaxis().set_visible(False)
        self.canvas.mpl_connect('button_press_event', self.on_press)
        x = y = 0.02
        self.rect = patches.Rectangle((x, y), 0.4,0.4,edgecolor='g', alpha=1, fill=None, label='Label')
        self.axes.add_patch(self.rect)
        self.axes.plot()

    def on_press(self, click):
        x1, y1 = click.xdata, click.ydata
        x2, y2 = click.xdata, click.ydata
        self.rect.set_x(x1 - 0.2) #Move the rectangle and centre it on the X click point
        self.rect.set_y(y1 - 0.2) #Move the rectangle and centre it on the Y click point


        self.axes.plot()
        self.canvas.draw()
        self.zoom_axes=[x1,x2,y1,y2]
        self.parent.zoom_panel.Update(self)



class Zoom(wx.Panel):
    def __init__(self,parent):
        wx.Panel.__init__(self,parent,size=(200,200))
        self.Show()
    def Update(self,parent):
        #Load axis values of the selected rectangle
        zoom_axes=parent.zoom_axes

        #duplicate the plot from the main panel
        self.figure = Figure(figsize =(4,4))
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.axes = self.figure.add_subplot(111)
        #Apply axis of drawn rectangle to the plot
        self.axes.axis(zoom_axes)
        t = arange(0.5, 3.0, 0.01)
        s = cos(2 * pi * t)
        self.axes.plot(t, s)
        self.axes.get_xaxis().set_visible(False)
        self.axes.get_yaxis().set_visible(False)
        self.canvas.draw()
        self.Refresh()




app = wx.App()
frame = MainFrame(None).Show()
app.MainLoop()

当我单击矩形以显示缩放时出现此错误:

and i have this error when i click in rectangle to show zoom :

   self.set_ylim([v[2], v[3]], emit=emit, auto=False)
UserWarning: Attempting to set identical bottom==top results
in singular transformations; automatically expanding.
bottom=0.30714285714285716, top=0.30714285714285716

谢谢

推荐答案

您为 x1,x2 和 y1, y2 发送相同的 x 和 y 值,因此本质上您是在缩放到一个点而不是一个矩形.
请记住,您正在单击一个点并计算矩形,因此您需要从该点计算缩放区域.见下文:

You are sending identical x and y values for x1,x2 and y1, y2, so in essence you are zooming to a point and not a rectangle.
Remember, you are clicking on a point and calculating the rectangle, so you need to calculate the zoom area from the point. See below:

def on_press(self, click):
    x1, y1 = click.xdata, click.ydata
    zx1 = x1 - 0.2
    zy1 = y1 - 0.2
    zx2 = x1 + 0.2
    zy2 = y1 + 0.2
    self.rect.set_x(x1 - 0.2) #Move the rectangle and centre it on the X click point
    self.rect.set_y(y1 - 0.2) #Move the rectangle and centre it on the Y click point


    self.axes.plot()
    self.canvas.draw()
    self.zoom_axes=[zx1,zx2,zy1,zy2]
    self.parent.zoom_panel.Update(self)

如果你不想计算矩形/缩放然后研究 matplotlibRectangleSelector

If you don't want to calculate the rectangle/zoom then investigate matplotlib's RectangleSelector

这篇关于选择并在矩形补丁内显示缩放时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆