Matplotlib plots not displaying in sublimetext

前端 未结 5 2113
情话喂你
情话喂你 2020-12-09 04:30

I\'ve recently started using sublimetext 2, however I noticed today that the plt.show() function doesn\'t seem to work when run within sublimetext.

If

相关标签:
5条回答
  • 2020-12-09 05:15

    I tried out theta's answer as suggested above. It works fine. In case the console opens and closes instantly like running python files directly from file explorer, just add the following line at the end:

    matplotlib.pyplot.show('hold')
    

    Its my first time answering here. Sorry if I didn't adhere to the answer format.

    0 讨论(0)
  • 2020-12-09 05:26

    More appropriate way is to just add one line in default Python build system file:

    "shell": true
    

    Or make additional Python build system with:

    {
        "cmd": ["python", "-u", "$file"],
        "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
        "selector": "source.python",
        "shell": true
    }
    
    0 讨论(0)
  • 2020-12-09 05:26

    I had a similar issue under Ubuntu. I was trying to plot a pandas DataFrame like this:

    df = pandas.DataFrame([*range(5)])
    df.plot()
    

    But the plot did not show. Turns out you need to do plt.show(), as discussed here.

    0 讨论(0)
  • 2020-12-09 05:28

    Note in sublime text 3 (with anaconda package installed)

    You need to add a custom build system as theta said, by:

    first add a folder named python in \path\to\Sublime Text Build XXXX\Data\Packages

    then put a file named python.sublime-build into that folder.

    {
        "cmd": ["python", "-u", "$file"],
        "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
        "selector": "source.python",
        "shell": true
    }
    

    and remember to add plt.show() like Roald said.

    plt.imshow(image)
    plt.show()
    

    Demo:

    0 讨论(0)
  • 2020-12-09 05:32

    My guess would be that Sublime Text 2 launches your code in its own environment, and only traps and displays the console output. The same problem exists when using GUI's, such as wxPython.

    Another work around is to open the command prompt and launch it manually (python file.py).

    EDIT: Here it states that "Under Windows, GUIs are supressed."

    EDIT 2: Here it lists a way to fix it, right at the bottom of the page.

    0 讨论(0)
提交回复
热议问题