Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib-缩放时的replot_Python_Matplotlib - Fatal编程技术网

Python matplotlib-缩放时的replot

Python matplotlib-缩放时的replot,python,matplotlib,Python,Matplotlib,我发现Matplotlib库可以为它们的图形设置动画并处理事件,比如鼠标单击。我需要制作交互式Mandelbrot集缩放的简单代码。由于显而易见的原因,我无法以无限的精度绘制所有内容;)我认为,也许绘图,在用户缩放后,应该采用新的边界,并在新边界中绘制一部分Mandelbrot集,具有足够的新精度 有没有一个简单的方法可以做到这一点?或者只是一种方式?:) 查看提供了一个简单示例的功能 在上面链接的示例中,您只需要在诸如“onpress”(连接到鼠标事件)之类的函数中添加自己的重新计算代码。类似

我发现Matplotlib库可以为它们的图形设置动画并处理事件,比如鼠标单击。我需要制作交互式Mandelbrot集缩放的简单代码。由于显而易见的原因,我无法以无限的精度绘制所有内容;)我认为,也许绘图,在用户缩放后,应该采用新的边界,并在新边界中绘制一部分Mandelbrot集,具有足够的新精度

有没有一个简单的方法可以做到这一点?或者只是一种方式?:)

查看提供了一个简单示例的功能

在上面链接的示例中,您只需要在诸如“onpress”(连接到鼠标事件)之类的函数中添加自己的重新计算代码。类似于以下的东西对我很有用:

import matplotlib.pyplot as plt
import numpy as np

class ZoomPlot():

    def __init__(self):
        self.fig = plt.figure()
        self.ax = self.fig.add_subplot(111)
        self.xmin = -2.5; self.xmax = 1.0;
        self.ymin = -1.5; self.ymax = 1.5;
        self.xpress = self.xmin
        self.xrelease = self.xmax
        self.ypress = self.ymin
        self.yrelease = self.ymax
        self.resolution = 200
        self.maxiters = 30

        self.fig.canvas.mpl_connect('button_press_event', self.onpress)
        self.fig.canvas.mpl_connect('button_release_event', self.onrelease)
        self.plot_fixed_resolution(self.xmin, self.xmax,
                                   self.ymin, self.ymax)

    def mandlebrot(self, X, Y):
        C = X + Y*1j
        Z = C
        divtime = self.maxiters + np.zeros(Z.shape, dtype=int)
        for n in range(self.maxiters):
            Z = Z**2 + C
            diverge = Z*np.conj(Z) > 2**2
            div_now = diverge & (divtime == self.maxiters)
            divtime[div_now] = n
            Z[diverge] = 2 

        return divtime 

    def plot_fixed_resolution(self, x1, x2, y1, y2):
        x = np.linspace(x1, x2, self.resolution)
        y = np.linspace(y1, y2, self.resolution)
        X, Y = np.meshgrid(x, y)
        C = self.mandlebrot(X, Y)
        self.ax.clear()
        self.ax.set_xlim(x1, x2)
        self.ax.set_ylim(y1, y2)
        self.ax.pcolormesh(X, Y, C)
        self.fig.canvas.draw()

    def onpress(self, event):
        if event.button != 1: return
        self.xpress = event.xdata
        self.ypress = event.ydata

    def onrelease(self, event):
        if event.button != 1: return
        self.xrelease = event.xdata
        self.yrelease = event.ydata
        self.xmin = min(self.xpress, self.xrelease)
        self.xmax = max(self.xpress, self.xrelease)
        self.ymin = min(self.ypress, self.yrelease)
        self.ymax = max(self.ypress, self.yrelease)
        self.plot_fixed_resolution(self.xmin, self.xmax,
                                   self.ymin, self.ymax)


plot = ZoomPlot()
plt.show()

我不知道我是否应该接受这个回答,但是谢谢:)@CheshireCat,非常欢迎。你错过了什么?有什么我可以澄清的吗?现在我正试图用这段代码来生成我的Mandelbrot示例。如果我这样做了,我会发布它,你会得到我的接受:)@CheshireCat希望更新能有所帮助。Mandlebrot的计算结果来自于。哇,谢谢。老实说,还有一件事情需要解决,但我相信这会对我的工作有所帮助:)+1