16
votes

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 for example I create the following script:

import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.show() 

Running this with IDLE generates a plot, however using sublimetext no output is generated. It just keeps 'thinking' until I end the python.exe process after which [Finished] is output.

Have I misunderstood how sublimetext is meant to be used?

Thanks.

5
How are you executing the script in Sublime Text? What platform are you on? - noio
I am not sure what you mean, I simply hit f5. - iRoygbiv
I am using Sublime Text 3 build 3156 on Windows (and version 3143 on CentOS). The code works fine on both system. The image windows is shown after we press Ctrl+B to execute the script. - jdhao

5 Answers

14
votes

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.

14
votes

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
}
11
votes

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.

8
votes

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:

enter image description here

2
votes

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.