I'm running some basic code in the Visual Studio Code editor on MacOSX:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.show()
...and can't seem to get the png/svg file image to come up after running this. This also doesn't stop executing and I have to manually terminate the process. However, if I run this directly in the Terminal (each line of code line for line) I get the resulting image. One work-around is to just save the file (plt.savefig('foo.png')). This seems to work - the image is saved in the specified file location. However, it would be good to just see the image come up after running the code.
When running matplotlib codes from the terminal, I experience the same kind of hanging of the application after saving the image to a file. In this case, one 'workaround' that has always worked for me is to turn off blocking. Basically alter your code in this way:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.show(block=False)
input('press <ENTER> to continue')
It's not perfect, but the image is saved correctly and the application stops after you hit ENTER in the terminal. Hope this helps.
I am having a similar issue and I think it is a problem with the exact version of python that vs code is using to run the code.
For reference, I have vscode version 1.52.1 on a mac os Catalina. I installed python via anaconda and created a new environment for python 2.7 (tried with python 3.8 too). I open VSCode by calling code . from the folder I have my simple python codes saved in.
Like OP, I could reproduce the figure if I were to run the code from a python instance called from terminal but not from vscode.
MY SOLUTION:
From vscode, select as python interpreter the one found in /usr/bin/python not the one in ~/opt/anaconda3/env/python27/bin/python
But this is weird, because from a separate terminal window which python returns ~/opt/anaconda3/env/python27/bin/python. This suggests me (though I am no python expert) that there is an issue within vscode about linking the right interpreter with the libraries. Frankly being new to vscode, I find the need to pay attention to these details quite concerning.
I got the same problem, which was driving me nuts. The image was displayed when using Jupyter Notebooks, but not always when using VS Code. I just added one last line_ plt.show() - IMHO unnecessarily - but this worked well.
import matplotlib.pyplot as plt
import matplotlib.image as img
im = img.imread('myimage.png')
plt.imshow(im)
plt.show() # <- added this line and now the image shows every time!
I faced the same issue and here's what I did to solve it.
Since the code runs without any error but also does not generate any plot and needs to be terminated manually, it's difficult to figure out what's going on. I tried running python2.7 test.py
This works, plot is generated but python3 test.py does not work.
So, here's what you need to do -
Run, pip install matplotlib --upgrade to upgrade the matplotlib. This does not resolve the issue but now the error is printed.
"RuntimeError: Python is not installed as a framework" ......
So, finally, to solve the problem, refer to Working with Matplotlib on macOS
Since, I am using Anaconda, all I need to do is conda install python.app and then use pythonw to run all scripts. I hope you also find the solution to your particular case from the FAQ.
Overall, it's a Matplotlib problem, so upgrading (or reinstalling) and trying with different Python versions should get you going.
Related
I have VS Code on a Mac.
Pillow is installed and version verified as 8.3.2 via Pip list in the terminal window of VS Code. I have confirmed via the pillow docs that the ImageOps.contain() is part of 8.3.
My problem is that when I use the terminal, type python and run the following, it works perfectly:
from PIL import Image, ImageOps
im = Image.open("images/Barcelona.jpg")
print(im.format, im.size, im.mode)
im = ImageOps.contain(im, (800, 800), method=3)
im.show()
Preview pops right up and shows me the picture.
When I put the exact code into VS Code or build a .py file with Nano, I get an error message which is shown in this image:
I've verified the right version of Python, Pillow, and such. Any help or pointers would be greatly appreciated.
This turns out to be a problem with what I can only call nested environments and/or a conflict between Anaconda and Workspaces. I'm not really sure but when I used the _version import to figure out a) what VS Code thought and then b) figured out the environment Pip was reporting on, I deactivated up one level, upgraded the (base) to the 8.3 version and all ran fine. The knowledge on importing the version variable to see precisely what the code is importing came from the question asked below and was invaluable.
I want to choose the best IDE for Python programming so I'm testing different softwares but I have problem.
When I try this code blocks on VS Code I don't see any error but the image is not showing
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread('exit-ramp.jpg')
plt.imshow(image)
VS Code
When I try run the same code on PyCharm I see some errors
PyCharm
But when I run the same code on Jupyter Notebook it works. What can I do?
Bro, try that:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread('exit-ramp.jpg')
plt.imshow(image)
plt.show()
i've made a test in my vscode.
Few weeks ago i used a lot pycharm... but i get some library errors with pandas and pycharm its a little bit heavy.
VSCODE its light and i didnt get any library error that i had with pycharm.
I believe this is a backend problem to be changed in matplotlib. If you start your Python shell whether you are in a bash shell or in vscode, you should use a backend that will show the plot. Otherwise the plt.show() call is required.
So, one solution is to change the backend either manually in you Python shell or when starting the Python shell as explained here below.
Note, however, that I always use the qt5 backend but got the same issue as yours: no plot appears. I changed to the Qt5Agg backend and it worked for me (providing PyQt5 is installed).
So, first solution, you can try to set the backend when starting the Python shell:
ipython --pylab Qt5Agg
You should use a GUI backend amongst the possible backend. If you do not known, try to replace Qt5Agg by auto
The second solution consists in changing the backend manually once in the Python shell:
import matplotlib as plt
plt.use('Qt5Agg')
Right click on your code -> Run current file in interactive window.
I use Python data science libraries like Pandas, Matplotlib, Numpy. I like working with these. And for ease of use, I also have Anaconda installed which makes creating environments a breeze. But today when running some matplotlib code I face a really weird issue that I didn't encounter ever before. This issue occurs right when I try to import Pyplot.
$ python
>>> import matplotlib.pyplot as plt
!▓KH═⌐-▌ª╚E├╚Hü⌠>┴┘UÜWF♀ε½░±-Sz;½¼öC¿Æ→$√
)ì⌠┐→⌐V¿U!└φÑ^~π┘╠⌡╗♦{ò►ßq╢ê<_│☺♀àV╣oY}ër╡╫╞╟lBîyû,πƒ=♂▒-Öδbùh♂RΣ}Ω¼╘zâ╫♫┘▀\φ→H
$Æ⌡«ε╖#{╤╧↕ƒÅ▄\.╔~s)╥τ♣E:,-Ut⌠↓R↑J¬τpg┐±`&╟╡▓ ▓û
TS1Å`a↑╚+╬╚?└D¿e£▀z¼i♥☼·s&⌐
When I try to import Pyplot I hear three dots (like morse code) and then this gibberish comes up and my program ends. I have never seen anything like this before.
It didn't give me any ModuleNotFoundError or any sort of errors at all. The program just literally stops and the gibberish is printed. I have copied exactly what gets printed.
I have encountered this issue with Matplotlib and also NLTK. Trying to import NLTK shows the same error. I don't know if this issue occurs with any other library but I can confirm it occurs with Matplotlib and NLTK.
I was thinking about re-installing Anaconda but was curious to know more about this really weird issue. I would also like to say that the issue is with my base (root) conda environment, NLTK doesn't have any issues in my other machine-learning environment. Any help is absolutely appreciated.
Is it possible to use any other graphing library in SPSS that the built in? I just discovered the python extensions that makes SPSS great.
import matplotlib.pyplot as plt
from numpy.random import rand
fig, ax = plt.subplots()
for color in ['red', 'green', 'blue']:
n = 750
x, y = rand(2, n)
scale = 200.0 * rand(n)
ax.scatter(x, y, c=color, s=scale, label=color,
alpha=0.3, edgecolors='none')
ax.legend()
ax.grid(True)
plt.show()
This will create a simple scatter plot and it works fine in any IDE, but when trying to use that code in SPSS BEGIN PROGRAM END PROGRAM i get the following error:
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X.
Please either reinstall Python as a framework, or try one of the other backends. If you are Working with Matplotlib in a virtual enviroment see 'Working with Matplotlib in Virtual environments' in the Matplotlib FAQ
Maybe I am asking too much out of the python extension in SPSS but it would be nice to use another graph library than the one they have built in.
Adding more information as another answer as the comment field is too limited.
I do not get the framework error on Windows, which is a different issue, I think. But running your code inside Statistics works - sort of. Instead of placing the image in the Viewer, it pops up in its own window (which may be buried behind another window).
So Statistics doesn't know about it and patiently waits for the program to complete, which doesn't happen until you dismiss that window (which does have a normal frame not shown in the graphic).
To make this work, you would need to direct the matplotlib code to write the image to a file somewhere and then use SpssClient apis to insert that image in the Viewer. See CreateImageChartItem Method (Python) in the Python programmability help. Alternatively, if you can direct matplotlib to write the image to the standard output stream, Statistics might be able to capture it directly in the Viewer.
I usually do programmability images with R code (even though Python is a way better language(!)), where this all works seamlessly. Or I use Python code to write Statistics graphics commands and GPL to have the Statistics engine, which is pretty powerful, do the charting.
Another thing you mind find helpful if you are into Python, is that you can run Python in external mode, where you start with Python code running from your IDE or a Python command line and then have it invoke Statistics by running
import spss
This has great advantages for developing and debugging Python code, but you can't use the SpssClient module methods directly. I ran your code from my IDE (Wing Professional), and the image window popped right up. And, of course, you can use the spss module and related apis in external mode to communicate with Statistics and control it.
You can use just about any Python code or library, but you need to do some configuring. When the Python support (Python Essentials) is installed, it installs a private, unregistered Python system in order not to conflict with any other Python that might be installed. So if you try to add other libraries, the installer doesn't know what to do or installs them somewhere that the Statistics installation won't know about.
The easiest way to get around this is to install another, standard Python installation (version 2.7 in recent versions or 3.4 with version 24 if you need Python 3). Then go to Edit > Options > Files and point to that distribution. You will need to restart Statistics for that to take effect. I use the Anaconda distribution, which includes a lot of other goodies.
I'm not a Mac user, but something like this should work.
I'm not sure exactly what is going on under the hood, but here is my setup, example code, and problem:
setup:
snow leopard (10.6.8)
Python 2.7.2 (provide by EPD 7.1-2)
iPython 0.11 (provided by EPD 7.1-2)
matplotlib (provided by EPD 7.1-2)
example code:
import numpy as np
import pylab as pl
x=np.random.normal(size=(1000,))
pl.plot(x)
problem:
I can't use the standard Mac OS X shorcuts to access the window generated by the plot command.
For example, I can't Command-Tab to the window. Thus, if the window is behind some other window, I need to mouse over to it! Command-W doesn't close it.
Obviously, this is unacceptable. It seems like perhaps running Lion instead of Leopard might fix this, but i haven't upgraded yet. I feel like the problem has something to do with iPython generating windows that aren't fully Cocoa-aware in some sense, but I really know very little so I'm not particularly confident in this hypothesis.
Thus, any ideas on how to either resolve or get around this issue would be much appreciated.
From the description on the iPython page, it looks like Python uses Qt to generate
UI. This means that the windows it generates are definitely not Cocoa windows and will not act like them.
There's not likely to be an easy solution to this issue.
I experienced the same annoyance with my Anaconda installation of Python 2.7.10 on Mac OS X Yosemite 10.10.5. One solution I found was to change the backend to Mac OS X or Qt4Agg by creating a ~/.matplotlib/matplotlibrc file with the line:
backend: MacOSX
or
backend: Qt4Agg
Now I can easily get to the plot window with Application switcher using Command - Tab and close it with Command - W .