From 124d4a1b2ccffb8f58afe03e6f546943de5c4a80 Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Wed, 13 Dec 2023 17:43:08 +0100 Subject: [PATCH] Create README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- workflow/vscode_interactive/README.md | 78 +++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 workflow/vscode_interactive/README.md diff --git a/workflow/vscode_interactive/README.md b/workflow/vscode_interactive/README.md new file mode 100644 index 0000000..ec5416f --- /dev/null +++ b/workflow/vscode_interactive/README.md @@ -0,0 +1,78 @@ +# Python Interactive window +{:.no_toc} + + + +## The goal + +Python Interactive window allows you to interactively develop code with VS Code. + +Questions to [David Rotermund](mailto:davrot@uni-bremen.de) + +**Please use # %% with .py files instead of .ipynb Jupyter notebook files for interactive cells!!!** + +If you don't know what I am talking about, please read: [Python Interactive window](https://code.visualstudio.com/docs/python/jupyter-support-py) + +## Interactive plotting in # %% cells​ + +We can activate the interactive plotting mode via ​ + +```python +%matplotlib widget ​ +``` +The first time we use this command, vs code will need to download a plugin). + +Here an example: + +```python +%matplotlib widget + +import matplotlib.pyplot as plt +import numpy as np + +fig = plt.figure() + +plt.plot(np.sin(np.linspace(0, 20, 100))) +``` + +Yes, mypy will give you an error and yes, you can not suppress it... but non you can zoom and pan the figure... You can not have everything at once! + +### Modifing a plot + +First we plot something but keep the handle: + +```python +import matplotlib.pyplot as plt +import numpy as np + +fig = plt.figure() +line = plt.plot(np.sin(np.linspace(0, 20, 100))) +``` + +Then we can use to change the plot using the handle: + +```python +line[0].remove() +line = plt.plot(np.sin(5.0*np.linspace(0, 20, 100))) +``` + +If you don't know the handle, you can retrieve it like this: + +```python +handles = fig.gca().get_children() +print(handles) +``` + +```python +[, , , , , , , Text(0.5, 1.0, ''), Text(0.0, 1.0, ''), Text(1.0, 1.0, ''), ] +``` + +This allows us to do this: + +```python +fig.gca().get_children()[0].remove() +``` +