Interactive mode in vscode Python interactive

Viewed 16001

I'm trying to open any matplotlib chart in the interactive mode in Python Interactive in vscode (Windows)

I tried to use magic

%matplotlib qt 

and sometimes it works, but, very often, it just 'blinks' (open chart window and close it instantly, I don't know why) and shows chart in Python Interactive instead

I have tried plt.ion() but it seems that it doesn't change anything.

What is the right way of an opening chart in interactive mode?

2 Answers

If you want to show the plot in a separate window, you may need to set a backend. Also, your selected backend may not be compatible with your setup, so try a few.

In [4]: plt.switch_backend('QtAgg4')

If you DO want to show plots inline, you do not need to do anything. By default, VS Code will show the plots inline. You do not need %matplotlib inline, or plt.show(). If you DO want plots inline and it's not working, try:

get_ipython().run_line_magic('matplotlib', 'inline')

This is what shows up when you convert a jupyter notebook into VS Code by importing it.

    import numpy as np
    import matplotlib.pyplot as plt

    x = np.arange(0,100)
    y = x*2

    # Functional Method
    fig = plt.figure()
    ax = fig.add_axes([0, 0, 1, 1])
    ax.plot(x, y)
    ax.set_title('title')
    ax.set_xlabel('X')
    ax.set_ylabel('Y')

My Code, tested on my VS Code in Interactive Window, with plot showing inline.


Updated answer as of Nov 2019:
This problem is gone with the recent update to VS Code. Make sure to install all Jupyter Notebook extensions. Also, you can now right-click on a .ipynb file and select "Open-With VS Code". This will automatically open the file in 2 windows, raw-json-code on left and interactive-jupyter-notebook on right. Slick. Really love this new update to VS Code.

I'm a developer on the Interactive Window. Jennifer has already given some good advice above for getting pop out plots working with our current builds. But if you are interested, we just recently added a plot viewer to our newest development builds. You can see the issue here:

https://github.com/microsoft/vscode-python/issues/4976

It won't be in our full release until our next monthly release. But if you want to try it out early and see if it works for you, you can check out our dev build here:

https://github.com/microsoft/vscode-python/blob/master/CONTRIBUTING.md#development-build

Related