hi,
I’m currently trying to create a Python code that visualizes the mandelbrot set and Julia set fractal in subplots where the Julia set will be plotted based on the value chosen by clicking on the mandelbrot set plot. When a new point is clicked on the Mandelbrot set plot, the previous visualization of julia set will be replaced by the new one and so does its colorbar.
However, when clicking a new point, the previous colorbar for julia plot does not be removed and replaced with a new one but they both appear on the subplot and keep adding as I clicked a new point.
Is there e any way to solve this?
This is the code that I got.
Thank you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
import numpy as np import matplotlib.pyplot as plt #define Mandelbrot function def mandelbrot(c, max_iter): z = 0 for n in range (max_iter): if abs (z) > 2 : return n # Number of iterations before diverging z = z * * 2 + c return max_iter #creating fractal (2D array representing Mandelbrot set over a defined region) def create_M_fractal(M_xmin, M_xmax, M_ymin, M_ymax, width, height, max_iter): real_axis = np.linspace(M_xmin, M_xmax, width) imag_axis = np.linspace(M_ymin, M_ymax, height) return np.array([[mandelbrot( complex (x, y), max_iter) for x in real_axis] for y in imag_axis]) # Parameters for the Mandelbrot set M_xmin, M_xmax, M_ymin, M_ymax = - 2.0 , 1.0 , - 1.5 , 1.5 width, height = 800 , 800 max_iter = 100 # Generate the Mandelbrot set M_fractal = create_M_fractal(M_xmin, M_xmax, M_ymin, M_ymax, width, height, max_iter) # Create subplot fig, (ax1, ax2) = plt.subplots(nrows = 1 , ncols = 2 , figsize = ( 15 , 7 )) # Create the main plot for the Mandelbrot set cax1 = ax1.imshow(M_fractal, extent = (M_xmin, M_xmax, M_ymin, M_ymax), cmap = 'hot' , origin = 'lower' ) ax1.set_title( 'Mandelbrot Set' ) ax1.set_xlabel( 'Real Part' ) ax1.set_ylabel( 'Imaginary Part' ) # Colorbar for Mandelbrot plt.colorbar(cax1, ax = ax1) #Define Julia set def julia(c, max_iter, J_xmin, J_xmax, J_ymin, J_ymax, width, height): z = np.linspace(J_xmin, J_xmax, width).reshape(( 1 , width)) + \ 1j * np.linspace(J_ymin, J_ymax, height).reshape((height, 1 )) img = np.zeros(z.shape, dtype = int ) for n in range (max_iter): mask = np. abs (z) < = 2 img[mask] = n z[mask] = z[mask] * * 2 + c return img # Parameters for Julia J_xmin, J_xmax, J_ymin, J_ymax = - 2.0 , 2.0 , - 1.5 , 1.5 width, height = 800 , 800 max_iter = 100 # Function to update the Julia set based on the clicked point def update_julia(event): if event.inaxes = = ax1: # Check if click is in subplot 1 if event.xdata is not None and event.ydata is not None : # Get the clicked point x = event.xdata y = event.ydata c = complex (x, y) # Create complex number from the clicked point # Generate the Julia set for the clicked point J_fractal = julia(c, max_iter, J_xmin, J_xmax, J_ymin, J_ymax, width, height) # Update the Julia plot ax2.cla() # Clear the previous Julia plot cax2 = ax2.imshow(J_fractal, extent = (J_xmin, J_xmax, J_ymin, J_ymax), cmap = 'hot' , origin = 'lower' ) ax2.set_title( f 'Julia Set for c = {c:.2f}' ) ax2.set_xlabel( 'Real Part' ) ax2.set_ylabel( 'Imaginary Part' ) # Colorbar for Julia set plt.colorbar(cax2, ax = ax2) plt.draw() # Refresh the figure # Connect the click event to the update function fig.canvas.mpl_connect( 'button_press_event' , update_julia) # Show the plots plt.show() |