+10 XP
Storing and Plotting Time Series
To plot V(t), store V at every time step during the loop — not just the final value. Use two parallel lists: one for time, one for voltage.
The pattern: initialize empty lists before the loop, append each new value inside the loop. After the loop both lists have the same length — one value per time step.
python
import numpy as npimport matplotlib.pyplot as pltdt=1e-3; tau=20e-3; el=-60e-3; vr=-70e-3; vth=-50e-3r=100e6; i_mean=25e-11; t_max=150e-3v = elt_list, v_list, i_list = [], [], []for step in range(int(t_max/dt)): t = step * dt i_t = i_mean * (1 + np.sin(2 * np.pi * t / 0.01)) v = v + (dt/tau) * (el - v + r * i_t) if v >= vth: v = vr t_list.append(t * 1000) # ms→ Append AFTER the spike check so reset voltage is stored
v_list.append(v * 1000) # mV→ * 1000 converts V→mV for readable axis
i_list.append(i_t * 1e12) # pAfig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 5), sharex=True)→ plt.subplots(2,1): 2 rows, 1 column; sharex links x-axes
ax1.plot(t_list, i_list, color='#1CB0F6')ax1.set_ylabel('I (pA)'); ax1.set_title('Synaptic Current')ax2.plot(t_list, v_list, color='#58CC02')ax2.axhline(y=-50, color='red', linestyle='--', label='Threshold')ax2.set_ylabel('V (mV)'); ax2.set_xlabel('Time (ms)')ax2.set_title('Membrane Voltage'); ax2.legend()plt.tight_layout(); plt.show()Run this — you should see voltage rising then snapping down at each spike, plus the oscillating current above.