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 run this script in a Jupyter notebook:
import matplotlib.pyplot as plt
plt.plot(range(10))
I get a nice plot as an output. I run this debug.py from terminal:
import matplotlib.pyplot as plt
plt.plot(range(10))
plt.show()
./debug.py doesn't give a plot. My cursor changes shape, it becomes a cross pointer, but I don't get any plot, and I don't get back my terminal prompt.
Following the recommendation given in this answer, I run in a local python3 shell this:
>>> import matplotlib
>>> print(matplotlib.rcParams['backend'])
The output is TkAgg, as it should be, based on the answer to the aforementioned question.
Why I don't get a matplotlib window then?
I am trying to use matplotlib to view an image in ipython. However, I solely obtain a description of the image as opposed to seeing the actual image:
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> plt.imshow(an_array)
Out[4]: <matplotlib.image.AxesImage at 0x7fb626f063d0>
This is with the backend set to Qt5Agg or TkAgg in .matplotlibrc
I have the same issues whether or not I am working in a conda virtual environment
I am running: Scientific Linux release 6.8 (Carbon), Python 2.7.13, Anaconda 4.3.0 (64-bit)
I have had similar issues on my local computer running MacOSX
I would be most grateful for any advice,
Thanks
Rob
Add %matplotlib inline before import matplotlib.pyplot as plt to enable automatic visualization of your plots when the cell has finished executing.
Note, however, that this works just in iPython notebooks. Normally, you'd need to call plt.show() (without any arguments) to visualize the plots.
I am using
%matplotlib inline
to display plots inside the notebook. I would like to disable this for several cells. So, I try
%matplotlib qt
This outputs the following error:
ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5,
or PySide package to be installed, but it was not found.
I am not sure how to solve this, as everything is up to date.
How can I solve the above?
Is there another way to disable %matplotlib inline in a certain cell without restarting the entire kernel?
You might be able to use plt.switch_backend, although as the documentation states, this is an experimental feature. The following works for me, using matplotlib 1.5 and an IPython 4.0.1:
In [1]: from matplotlib import pyplot as plt
In [2]: import numpy as np
# plot appears inline (default)
In [3]:plt.plot(np.random.randn(10))
Out[3]:[<matplotlib.lines.Line2D at 0x7fac4408e390>]
In [4]: plt.switch_backend('QtAgg4')
# plot appears inside a separate Qt4 window
In [5]:plt.plot(np.random.randn(10))
Out[5]:[<matplotlib.lines.Line2D at 0x7fac3b408a90>]
You might need to change 'QtAgg4' according to whichever version of PyQt you have installed - this could be the cause of the error you mentioned in the question. Another interactive backend that should work on Mac would be 'CocoaAgg'. If the images are very large you could also use the 'Agg' backend to suppress plotting altogether, and instead save the resulting figure(s) straight to disk.
If you don't have a specific backend installed use "agg":
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
Reference: https://github.com/matplotlib/matplotlib/issues/9017
I just installed matplotlib in Ubuntu 9.10 using the synaptic package system.
However, when I try the following simple example
>>> from pylab import plot;
>>> plot([1,2,3],[1,2,3])
[<matplotlib.lines.Line2D object at 0x9aa78ec>]
I get no plot window. Any ideas on how to get the plot window to show?
You can type
import pylab
pylab.show()
or better, use ipython -pylab.
Since the use of pylab is not recommended anymore, the solution would nowadays be
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()
pylab.show() works but blocks (you need to close the window).
A much more convenient solution is to do pylab.ion() (interactive mode on) when you start: all (the pylab equivalents of) pyplot.* commands display their plot immediately. More information on the interactive mode can be found on the official web site.
I also second using the even more convenient ipython -pylab (--pylab, in newer versions), which allows you to skip the from … import … part (%pylab works, too, in newer IPython versions).
Try this:
import matplotlib
matplotlib.use('TkAgg')
BEFORE import pylab
The code snippet below works on both Eclipse and the Python shell:
import numpy as np
import matplotlib.pyplot as plt
# Come up with x and y
x = np.arange(0, 5, 0.1)
y = np.sin(x)
# Just print x and y for fun
print x
print y
# Plot the x and y and you are supposed to see a sine curve
plt.plot(x, y)
# Without the line below, the figure won't show
plt.show()
Any errors show up? This might an issue of not having set the backend. You can set it from the Python interpreter or from a config file (.matplotlib/matplotlibrc) in you home directory.
To set the backend in code you can do
import matplotlib
matplotlib.use('Agg')
where 'Agg' is the name of the backend. Which backends are present depend on your installation and OS.
http://matplotlib.sourceforge.net/faq/installing_faq.html#backends
http://matplotlib.org/users/customizing.html
Modern IPython uses the "--matplotlib" argument with an optional backend parameter. It defaults to "auto", which is usually good enough on Mac and Windows. I haven't tested it on Ubuntu or any other Linux distribution, but I would expect it to work.
ipython --matplotlib
If you encounter an issue in which pylab.show() freezes the IPython window (this may be Mac OS X specific; not sure), you can cmd-c in the IPython window, switch to the plot window, and it will break out.
Apparently, future calls to pylab.show() will not freeze the IPython window, only the first call. Unfortunately, I've found that the behavior of the plot window / interactions with show() changes every time I reinstall matplotlib, so this solution may not always hold.
If you are starting IPython with the --pylab option, you shouldn't need to call show() or draw(). Try this:
ipython --pylab=inline
--pylab no longer works for Jupyter, but fortunately we can add a tweak in the ipython_config.py file to get both pylab as well as autoreload functionalities.
c.InteractiveShellApp.extensions = ['autoreload', 'pylab']
c.InteractiveShellApp.exec_lines = ['%autoreload 2', '%pylab']
If you are user of Anaconda and Spyder then best solution for you is that :
Tools
-->
Preferences
-->
Ipython console
-->
Graphic Section
Then in the Support for graphics (Matplotlib) section:
select two avaliable options
and in the Graphics Backend:
select Automatic
Another possibility when using easy_install is that you need to require the most recent version of matplotlib.
Try:
import pkg_resources
pkg_resources.require("matplotlib")
before you import matplotlib or any of its modules.