Iterative code is not working in jupyter notebook while it runs in command line interface

I am sorry that the code is wrong. The sin() part should be np.sin(np.pi*x*n). Then, the code should run in CLI such as $python a.py. As for version, I checked jupyter version and found that qtconsole is not installed. (Why is this not installed automatically?) After installation, the versions are:
IPython : 8.30.0
ipykernel : 6.29.5
ipywidgets : 8.1.5
jupyter_client : 8.6.3
jupyter_core : 5.7.2
jupyter_server : 2.14.2
jupyterlab : 4.3.2
nbclient : 0.10.1
nbconvert : 7.16.4
nbformat : 5.10.4
notebook : 7.3.1
qtconsole : 5.6.1
traitlets : 5.14.3
Now, the code runs in jupyter depending on backend. The above is %matplotlib inline. In case of nbAgg, error with “Javascript Error: IPython is not defined”. widget, ipympl opens large inline panel with so many figures nothing inside. The only “TkAgg” works fine fortunately. Why is this so tricky as for backend?