<jupyter_start><jupyter_text>headingfirst import all the stuff we need<jupyter_code>%matplotlib widget
#import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches as mp
import numpy as np
from matplotlib.backend_bases import MouseButton

import cmath as c
print('imports done')
def on_click(event):
    if event.button is MouseButton.LEFT:
        if event.inaxes == axs[0]:
            vector.set_visible(True)
            wave.set_visible(True)
            z = complex(event.xdata, event.ydata)
            draw_complex_wave(z)

def on_motion(event):
    if event.button is MouseButton.LEFT:
        if event.inaxes == axs[0]:
            z = complex(event.xdata, event.ydata)
            draw_complex_wave(z)

def on_release(event):
    pass      

def draw_complex_wave(z): 
    amp, phase = c.polar(z)

    vector.set_positions((0,0),(z.real,z.imag))
    wave.set_xdata(x)
    wave.set_ydata(amp*np.sin(x+phase))
    
    fig.canvas.draw()


fig, axs = plt.subplots(1,2)
fig.canvas.mpl_connect('button_press_event', on_click)
fig.canvas.mpl_connect('motion_notify_event', on_motion)
fig.canvas.mpl_connect('button_release_event', on_release)

for i, ax in enumerate(axs):
    ax.axis('equal')
    ax.set_xlim([-4, 4])
    ax.set_ylim([-4, 4])
    ax.spines[["left", "bottom"]].set_position(('data', 0))
    ax.spines[["top", "right"]].set_visible(False)
    ax.set_box_aspect(1)

#set initial amplitude and phase
z = complex(1,0)
amp, phase = c.polar(z)
x = np.linspace(-np.pi, np.pi, 100)
x_unit = np.cos(x)
y_unit = np.sin(x)

unit_circle, = axs[0].plot(x_unit, y_unit, linestyle='dotted', color='green')
unit_sine, = axs[1].plot(x, y_unit, linestyle='dotted', color='green')

unit_vector = mp.FancyArrowPatch((0, 0), (1, 0),mutation_scale=5, color='green')
vector = mp.FancyArrowPatch((0, 0), (1, 0),mutation_scale=5, color='red', visible=False)

wave, = axs[1].plot(x, amp*np.sin(x+phase), visible=False, color='red')
axs[0].add_patch(unit_vector)
axs[0].add_patch(vector)
fig.canvas.draw()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15380859375, 'fraction_numerical': 0.015625, 'mean_word_length': 4.253846153846154, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '178333', 'license': 'CC0-1.0', 'n_tokens_mistral': 810, 'n_tokens_neox': 783, 'n_words': 148, 'path': 'interactive.ipynb', 'repo_name': 'Geoffysicist/matplotlib_sandpit', 'size': '2048'}