Pycharm does not show plot
Solution 1
Just use
import matplotlib.pyplot as plt
plt.show()
This command tells the system to draw the plot in Pycharm.
Example:
plt.imshow(img.reshape((28, 28)))
plt.show()
Solution 2
I realize this is old but I figured I'd clear up a misconception for other travelers. Setting plt.pyplot.isinteractive()
to False
means that the plot will on be drawn on specific commands to draw (i.e. plt.pyplot.show()
). Setting plt.pyplot.isinteractive()
to True
means that every pyplot
(plt
) command will trigger a draw command (i.e. plt.pyplot.show()
). So what you were more than likely looking for is plt.pyplot.show()
at the end of your program to display the graph.
As a side note you can shorten these statements a bit by using the following import command import matplotlib.pyplot as plt
rather than matplotlib as plt
.
Solution 3
I had the same problem. Check wether plt.isinteractive()
is True. Setting it to 'False' helped for me.
plt.interactive(False)
Solution 4
I tried different solutions but what finally worked for me was plt.show(block=True)
. You need to add this command after the myDataFrame.plot()
command for this to take effect. If you have multiple plot just add the command at the end of your code. It will allow you to see every data you are plotting.
Solution 5
import matplotlib
matplotlib.use('TkAgg')
Works for me. (PyCharm/OSX)
Comments
-
Rasmus Larsen about 2 years
Pycharm does not show plot from the following code:
import pandas as pd import numpy as np import matplotlib as plt ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000)) ts = ts.cumsum() ts.plot()
What happens is that a window appears for less than a second, and then disappears again.
Using the Pyzo IEP IDE (using same interpreter) on the same code the plot shows as expected.
...So the problem must be with some setting on Pycharm. I've tried using both python.exe and pythonw.exe as interpreter both with same results.
This is my sys_info:
C:\pyzo2014a\pythonw.exe -u C:\Program Files (x86)\JetBrains\PyCharm Community Edition 3.4.1\helpers\pydev\pydevconsole.py 57315 57316 PyDev console: using IPython 2.1.0import sys; print('Python %s on %s' % (sys.version, sys.platform)) Python 3.4.1 |Continuum Analytics, Inc.| (default, May 19 2014, 13:02:30) [MSC v.1600 64 bit (AMD64)] on win32 sys.path.extend(['C:\\Users\\Rasmus\\PycharmProjects\\untitled2']) In[3]: import IPython print(IPython.sys_info()) {'commit_hash': '681fd77', 'commit_source': 'installation', 'default_encoding': 'UTF-8', 'ipython_path': 'C:\\pyzo2014a\\lib\\site-packages\\IPython', 'ipython_version': '2.1.0', 'os_name': 'nt', 'platform': 'Windows-8-6.2.9200', 'sys_executable': 'C:\\pyzo2014a\\pythonw.exe', 'sys_platform': 'win32', 'sys_version': '3.4.1 |Continuum Analytics, Inc.| (default, May 19 2014, ' '13:02:30) [MSC v.1600 64 bit (AMD64)]'}
-
Padraic Cunningham almost 10 yearsdoes
plt.pyplot.show()
work? -
Rasmus Larsen almost 10 yearsIt does not. When running exactly 'plt.pyplot.show()' it does not work. Should i write something else instead of 'pyplot'? (noob here).
-
Padraic Cunningham almost 10 yearswhat happens when you run the code in pycharm?
-
Rasmus Larsen almost 10 yearsWhen i run the file same thing happens: a window quickly appears and disappears, console printing: Process finished with exit code 0. When I send it to the console the window appears and stays but nothing is drawn in the window, it's just white. The label of the window is "Figure 1 (Not Responding)"
-
Ronak Poriya almost 8 yearsYou should add matplotlib.pyplot to visulize it.Refer : stackoverflow.com/a/29042575/4989929
-
Mona Jalal over 6 years@PadraicCunningham AttributeError: module 'matplotlib.pyplot' has no attribute 'pyplot'
-
spacewanderer about 6 yearsthis worked for me: from matplotlib import pyplot as plt and then plt.show()
-
Azim almost 5 yearsIf running your code from inside an IDE, use
plt.waitforbuttonpress()
. The figure will pop up and waits for the user to click on the new window. Only then it resumes the program and runs the rest of the code.
-
-
DanT over 9 yearsCalling plt.show(block=True) helped showing the chart once for me. But the console was blocked even after closing the charts window.
-
fantabolous over 9 yearsplt.interactive(False) did it for me in PyCharm
-
Rasmus Larsen over 9 yearsI don't have Python installed anymore, so I don't know if it would work for me, but since it works for others I'll accept this answer (I hope this is correct according to stackoverflow-rules)
-
Dhruv about 9 yearsBut I am now facing a new problem. Once the plot is generated in a new window, new code which I added after the plot does not run, until I close the newly opened window.
-
SummerEla almost 8 yearsAdding plt.show(block=True) to the end of my function is what worked for me. Thank you for the clarification. (Python 3.x, PyCharm 2016.1.4, Ubuntu)
-
Taka over 7 yearsThis is what works for me (Python 2.7, Pycharm 2016.3, Ubuntu 16.04): "import matplotlib.pyplot as plt", then the function plot from DataFrame like in "corr_data[col].plot(kind="bar", figsize=(8, 5), grid=True, color="r", title=col)" and before leaving the function, "plt.show()"
-
frostbite over 7 yearsThis did it for me. Should be the accepted answer. Messing with the plt.interactive() did nothing
-
frostbite over 7 yearsThe correct solution is to set plt.show(block=True) as @charlie has suggested below. After you add this line the value of the plt.interactive() flag had no bearing on the plot being displayed.
-
Yoav about 7 yearsThe important line that was missing in the question is plt.show()
-
Mona Jalal over 6 yearsThis didn't work for me ` for cluster_id in range(len(tempx[0])): plt.plot(tempx, tempy, 'rx--', markersize=8) plt.legend(loc=4, framealpha=0.5) plt.interactive(False) plt.show(block=True)`
-
whiletrue over 6 yearsI agree because this helped me, too. Setting the interactive mode to False and adding "DISPLAY=TRUE" to the environment variables didn't help.
-
tairen over 6 yearssorry to hear that. I find it also works in Pycharm 2017.3 (community edition). May you try to use a new version of Pycharm?
-
Zoe L about 6 yearsAdd this before importing matplotlib.pyplot
-
tairen almost 6 yearsHi, Saria, sorry for that. I test it in my Pycharm 2018.1.2 (community edition) with Ubuntu OS. It works too.
-
Ambareesh over 5 yearsIt's really amazing - this worked for me, and none of the others did. What was surprising is, I didn't need plt.show() in Jupyter Notebook, dunno why the behavior is different in PyCharm
-
NelsonGon almost 5 yearsThis is what I was looking for.
-
pooria over 4 yearssame here. I didn't need it in Jupyter Notebook
-
MNM over 4 yearsI always forget to call that too. This tell the system to actually draw it in pycharm. Notebook will auto call draw and print functions by default. I think it was made like that for quicker prototyping.
-
Mark Rotteveel about 4 yearsThis answer was already provided two years ago (eg in stackoverflow.com/a/52221178/466862). When answering older questions, make sure you either answer a new, different solution, or a significantly better explanation.
-
Milad Nourizade about 4 yearsHi, it works for Pycharm 2019.3 with MacOs Catalina
-
CGFoX almost 4 yearsWhat if I need to use
plt.draw()
since I'm updating the figure later on?plt.show()
works fine, butplt.draw()
doesn't show anything in PyCharm. -
Corbie almost 4 yearsThis has also worked for me on Windows 10 when
plt.show()
did not help any more in a newer version of PyCharm (2020.1). -
xaviersjs almost 4 yearsCould be that you don't need it in notebooks because you did something like this
%matplotlib inline
. @CGFox, draw is meant explicitly to render to memory but not display to the screen (or file). You need to either plt.pause(0.1) to get the plots to show, or call plt.show(pause=True) at some other point in code to get them to render -
questionto42standswithUkraine almost 4 yearsThis is repeating @AbdulRehmanLiaqat's answer, but it at least clearly states that this can be a possible solution. I first downvoted this because of the repetition. Now I upvoted, simply because the other answer does not state clearly that this single step here can already solve it, instead it mentions an error afterwards which did not appear in my case.
-
questionto42standswithUkraine almost 4 yearsPlease make clearer that the first step could still be the solution, as it worked for me. Stated like this, the other repeating answer of @amir1122 has a right to exist. Which is OK as well of course, and you still got my upvote.
-
AbdulRehmanLiaqat almost 4 years@Lorenz. Updated according to your suggestion which makes sense.
-
Josiah Yoder almost 4 yearsStep 1 works, but feels like a workaround. I suspect in my case that it is because I have multiple versions of Python (3.6, 3.7, and 3.8) floating around.
-
Shimon S over 3 yearsFor me too. The interesting is, that in Pycharm CE it works without it, but in Pro (2020.1) matplotlib.use('TkAgg') was the only thing that helped. What's the point?
-
markemus about 3 yearsTo use TkAgg you also need to install tkinter.
-
Amlan Chatterjee about 3 yearsI only unchecked the box and then it showed a window for a quick display and disappeared. After that I added plot.waitforbuttonpress() and now it works perfectly
-
CodePrinz almost 3 yearsAm I wrong, or is this answer technically wrong? He uses
import matplotlib as plt
so it would have beenplt.pyplot.show()
or the note, that we use the commonimport matplotlib.pyplot as plt
!? -
rocksNwaves about 2 yearsIt looks like Pycharm doesn't like interactive mode. Even with it on and a call to
plt.show()
I get a frozen and blank display window. I have to force close python to move on. However, settingplt.show(block=True)
resulted in the desired behavior. -
Tom Fuller almost 2 yearsworked on windows too!