gpt4 book ai didi

Python matplotlib - 缩放重绘

转载 作者:太空宇宙 更新时间:2023-11-04 03:34:21 24 4
gpt4 key购买 nike

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

有没有简单的方法来做到这一点?还是只是一种方式? :)

最佳答案

查看 event-handling功能,为此提供了一个简单示例 here .

您只需要在上面链接的示例中的“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()

关于Python matplotlib - 缩放重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29821177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com