When run as a script, you are working with global variables which will be slower. If you put the bulk of your computation inside a function (like p_f did), so you are instead working with local variables, you should see a speed improvement.
Note that to debug after doing this you will either click “Run Debug” on a file which contains the line myplottingfunc()
or type @enter myplottingfunc()
into the REPL. (You start debugging at the call site not at the function definition.)