I've got a class project to do, it's supposed to display the mandelbrot set with a customizable colour map and a max_iter slider. I've got the colour map radio buttons working, but I can't manage the slider. I had some AI give me a solution, and it only works for iterations under 30 (the default value), it goes up to 100 and does nothing past the default.
Here's the original code that currently doesn't work. The comments are just what it does in polish, ignore them:
# sparametryzować liczbę iteracji
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons, Slider
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.3)
# Rozmiary i obszar
width, height = 1500, 1500
x_min, x_max = -2, 0.5
y_min, y_max = -1.25, 1.25
max_iter = 30
# Siatka
x = np.linspace(x_min, x_max, width)
y = np.linspace(y_min, y_max, height)
X, Y = np.meshgrid(x, y)
C = X + 1j * Y
# Mandelbrot z użyciem pętli
Z = np.zeros(C.shape, dtype=complex)
M = np.zeros(C.shape, dtype=int)
axiter = plt.axes([0.25,0.05,0.65,0.03], facecolor="#777777")
iter_slider = Slider(axiter, "Ilość iteracji", 1, 100, valinit = 30, track_color="#777777",)
for k in range(max_iter):
mask = np.abs(Z) <= 2.0
Z[mask] = Z[mask]**2 + C[mask]
M[mask] = k + 1
# Wyświetlenie fraktalu
img = ax.imshow(M, extent=(x_min, x_max, y_min, y_max), cmap='twilight', origin='lower')
ax.axis('equal')
ax.axis('off')
# RadioButtons do zmiany cmap
ax.set_title('Fraktal Mandelbrota', pad=10)
rax = plt.axes([0.05, 0.4, 0.15, 0.15])
radio = RadioButtons(rax, ('twilight', 'Spectral', 'inferno', 'pink', 'viridis', 'berlin'))
rax.set_title('Wybór mapy kolorów', pad=10)
def update(label):
sliter=iter_slider.val
max_iter = sliter
for k in range(max_iter):
mask = np.abs(Z) <= 2.0
Z[mask] = Z[mask]**2 + C[mask]
M[mask] = k + 1
ax.imshow(M, extent=(x_min, x_max, y_min, y_max), cmap='twilight', origin='lower')
img.set_cmap(label)
fig.canvas.draw_idle()
radio.on_clicked(update)
plt.show()
Code given by AI, also ignore the comments:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons, Slider
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.3)
# Rozmiary i obszar
width, height = 1500, 1500
x_min, x_max = -2, 0.5
y_min, y_max = -1.25, 1.25
max_iter = 30
# Siatka
x = np.linspace(x_min, x_max, width)
y = np.linspace(y_min, y_max, height)
X, Y = np.meshgrid(x, y)
C = X + 1j * Y
# Mandelbrot z użyciem pętli
Z = np.zeros(C.shape, dtype=complex)
M = np.zeros(C.shape, dtype=int)
axiter = plt.axes([0.25,0.05,0.65,0.03], facecolor="#777777")
iter_slider = Slider(axiter, "Ilość iteracji", 1, 100, valinit=30, track_color="#777777")
for k in range(max_iter):
mask = np.abs(Z) <= 2.0
Z[mask] = Z[mask]**2 + C[mask]
M[mask] = k + 1
# Wyświetlenie fraktalu
img = ax.imshow(M, extent=(x_min, x_max, y_min, y_max), cmap='twilight', origin='lower')
ax.axis('equal')
ax.axis('off')
# RadioButtons do zmiany cmap
ax.set_title('Fraktal Mandelbrota', pad=10)
rax = plt.axes([0.05, 0.4, 0.15, 0.15])
radio = RadioButtons(rax, ('twilight', 'Spectral', 'inferno', 'pink', 'viridis', 'berlin'))
rax.set_title('Wybór mapy kolorów', pad=10)
def redraw():
max_iter = int(iter_slider.val)
Z = np.zeros(C.shape, dtype=complex)
M = np.zeros(C.shape, dtype=int)
for k in range(max_iter):
mask = np.abs(Z) <= 2.0
Z[mask] = Z[mask]**2 + C[mask]
M[mask] = k + 1
img.set_data(M)
img.set_cmap(radio.value_selected)
fig.canvas.draw_idle()
def update(label):
redraw()
def update_iter(val):
redraw()
radio.on_clicked(update)
iter_slider.on_changed(update_iter)
plt.show()
I have no idea why the update works only for iterations lower than the default, here's a comparison of manually changing it to 100 iter vs the 100 (actually 30) iter from the slider:
Manual 100 iter:

slider "100" iter

As I was writing this another thing occured to me. In the AI corrected code, if I set the original max_iter to 100 (near the top of the code),
30 iterations looks like this

It's completely different than normal 30 iterations (see slider "100 " iter img), and in the same code the 100 iter actually looks the way it should (see manual 100 iter img).