Sorry, you do not have permission to ask a question.

Sorry, you do not have permission to add post.

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Aurora Telekom Logo Aurora Telekom Logo

Aurora Telekom

Aurora Telekom Navigation

  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Home
  • About Us
  • Blog
  • Contact Us
Home/ Questions/Q 280

Aurora Telekom Latest Questions

Questioner
  • 0
  • 0
Questioner
Asked: December 3, 20242024-12-03T17:59:38+00:00 2024-12-03T17:59:38+00:00

update the colorbar visualsation from the previous to the new one

  • 0
  • 0

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()
buran write Oct-17-2024, 01:48 PM:
Please post all code, output and errors (it it’s entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the “Preview Post” button to make sure the code is presented as you expect before hitting the “Post Reply/Thread” button.
  • 0 0 Answers
  • 92 Views
  • 0 Followers
  • 0
    • Report
  • Share
    Share
    • Share on Facebook
    • Share on Twitter
    • Share on LinkedIn
    • Share on WhatsApp
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Stats

  • Questions 86
  • Answers 3
  • Best Answers 0
  • Users 3
  • Popular
  • Answers
  • Questioner

    How do native speakers tell I’m foreign based on my ...

    • 3 Answers
  • Questioner

    Funbahis Giriş Güncel Giriş Linki 2024

    • 0 Answers
  • Questioner

    Trying to create a simple linear regression module but getting

    • 0 Answers
  • James Wane
    James Wane added an answer Because non-native speakers use English differently as compared to native… April 19, 2018 at 2:03 am
  • Barry Carter
    Barry Carter added an answer You probably have strange grammar. Pretty much every language has… April 19, 2018 at 2:03 am
  • John Peter
    John Peter added an answer It may be little things like not using native idioms,… April 19, 2018 at 2:03 am

Top Members

Trending Tags

english

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • Buy Theme

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.