Matplotlib Graph not showing up on OSX? - python

I am currently trying to graph data using Python3 and matplotlib. I am developing on OSx Sierra and when I run, it is not showing up. There are no errors returned. I included plt.show() in my code and it is definitely running. Any help to get this graph showing would be appreciated. Vanilla Python3, edited in Emacs, ran from both IDLE and terminal. Neither work. Thank you.
import matplotlib.pyplot as plt
plt.show()
Does not produce anything, and there are no errors. I have tried
plt.switch_backend('MacOSX') and the error persists.

Just to wrap up the comments into an answer: pyplot.show() only produces a figure, if a figure has been created. This can be done explicitly by stating
fig = plt.figure()
or implicitly by plotting something (the figure is then created in the background), e.g.
plt.plot([1,2,3])
once plt.show() is called, all currently active figures will be displayed on the screen.

Related

Seaborn pairplot is closing immediately [duplicate]

I have a simple python script which plots some graphs in the same figure. All graphs are created by the draw() and in the end I call the show() function to block.
The script used to work with Python 2.6.6, Matplotlib 0.99.3, and Ubuntu 11.04. Tried to run it under Python 2.7.2, Matplotlib 1.0.1, and Ubuntu 11.10 but the show() function returns immediately without waiting to kill the figure.
Is this a bug? Or a new feature and we'll have to change our scripts? Any ideas?
EDIT: It does keep the plot open under interactive mode, i.e., python -i ..., but it used to work without that, and tried to have plt.ion() in the script and run it in normal mode but no luck.
I had this same problem, and it was caused by calling show() on the Figure object instead of the pyplot object.
Incorrect code. Causes the graph to flash on screen for a brief instant:
import matplotlib.pyplot as plt
x = [1,2,3]
y = [5,6,7]
fig = plt.figure()
plt.plot(x, y)
fig.show()
Last line should be as follows to show the graph until it is dismissed:
plt.show()
I think that using show(block=True) should fix your problem.
Had the inverse problem, and it seems that matplotlib will work in interactive or non-interaxctive mode based on a number of things that I could not trace (One way in IDLE, another in system console, one way in normal spyder console, another in a dedicated one ...)
This worked for me:
import matplotlib
matplotlib.interactive(False)
(Actually, I wanted interactive mode, but in your case the inverse should help.)
ion() and ioff() should do the same but the above is on matplotlib's level, not just pyplot or pylab. This works for me although I'm (later) importing pyplot separately and never call matplotlib as such again. I'm thinking that plt.ion() only has an effect on pyplot, not other components of matplotlib that may or may not be involved when using pyplot.
This method works for me on Windows 7, using both Python 2.65 with matplotlib 0.99 and Python 2.75 with matplotlib 1.3.1, across all available python consoles and IDEs on both systems (64-bit, both of them). It did, however, not work on Linux (SuSe 11.3, 64 bit), so there is definitely some platform dependency at play here
To replicate the matplotlib.show() behaviour with the tkagg backend when calling show() on the Figure object:
import Tkinter as Tk
import matplotlib.pyplot as plt
fig = plt.figure()
... your plot commands...
fig.show()
Tk.mainloop()
I had the same problem with this code below.
import matplotlib.pyplot as plt
plt.ion()
fig,ax0 = plt.subplots(figsize=(5.3,4))
plt.show()
I removed plt.ion(), and the plot stays without closing automatically.

matplotlib does not show plot()

I tried running plt.show() but no plot is shown. I had tried lots of solutions from stackoverflow including setting matplotlib backend to Qt4Agg, switching to other backends (i.e. TkAgg, Agg) and reinstalling matplotlib package but still not solving the issue. Some solutions that I had tried but not working are:
matplotlib does not show my drawings although I call pyplot.show()
Matplotlib.Pyplot does not show output; No Error
Why matplotlib does not plot?
Matplotlib does not show labels or numbers
Below is the code that I tried to run:
plt.scatter(pf["age"], pf["friend_count"])
plt.xlabel = "age"
plt.ylabel = "friends count"
plt.title = "Facebook Friends count by Age"
plt.show()
When plt.scatter(pf["age"], pf["friend_count"]) code was run, a scatter plot was shown but with no labels and title. Running plt.show() did not plot and no error was shown. Appreciate any help.
I installed Anaconda with Python 3 for Windows OS. My Mac laptop is running on Bootcamp.
The labels must use parentheses such as plt.xlabel("text"), not assigned to a string with = like you have in your example code. Make the changes in your code, save the changes, quit and reopen Spyder or whatever interpreter you are running, then run the code again.
plt.figure(1)
plt.scatter(pf["age"], pf["friend_count"])
plt.xlabel("age")
plt.ylabel("friends count")
plt.title("Facebook Friends count by Age")
plt.show()

How to make Python wait for a function to end? [duplicate]

I have a simple python script which plots some graphs in the same figure. All graphs are created by the draw() and in the end I call the show() function to block.
The script used to work with Python 2.6.6, Matplotlib 0.99.3, and Ubuntu 11.04. Tried to run it under Python 2.7.2, Matplotlib 1.0.1, and Ubuntu 11.10 but the show() function returns immediately without waiting to kill the figure.
Is this a bug? Or a new feature and we'll have to change our scripts? Any ideas?
EDIT: It does keep the plot open under interactive mode, i.e., python -i ..., but it used to work without that, and tried to have plt.ion() in the script and run it in normal mode but no luck.
I had this same problem, and it was caused by calling show() on the Figure object instead of the pyplot object.
Incorrect code. Causes the graph to flash on screen for a brief instant:
import matplotlib.pyplot as plt
x = [1,2,3]
y = [5,6,7]
fig = plt.figure()
plt.plot(x, y)
fig.show()
Last line should be as follows to show the graph until it is dismissed:
plt.show()
I think that using show(block=True) should fix your problem.
Had the inverse problem, and it seems that matplotlib will work in interactive or non-interaxctive mode based on a number of things that I could not trace (One way in IDLE, another in system console, one way in normal spyder console, another in a dedicated one ...)
This worked for me:
import matplotlib
matplotlib.interactive(False)
(Actually, I wanted interactive mode, but in your case the inverse should help.)
ion() and ioff() should do the same but the above is on matplotlib's level, not just pyplot or pylab. This works for me although I'm (later) importing pyplot separately and never call matplotlib as such again. I'm thinking that plt.ion() only has an effect on pyplot, not other components of matplotlib that may or may not be involved when using pyplot.
This method works for me on Windows 7, using both Python 2.65 with matplotlib 0.99 and Python 2.75 with matplotlib 1.3.1, across all available python consoles and IDEs on both systems (64-bit, both of them). It did, however, not work on Linux (SuSe 11.3, 64 bit), so there is definitely some platform dependency at play here
To replicate the matplotlib.show() behaviour with the tkagg backend when calling show() on the Figure object:
import Tkinter as Tk
import matplotlib.pyplot as plt
fig = plt.figure()
... your plot commands...
fig.show()
Tk.mainloop()
I had the same problem with this code below.
import matplotlib.pyplot as plt
plt.ion()
fig,ax0 = plt.subplots(figsize=(5.3,4))
plt.show()
I removed plt.ion(), and the plot stays without closing automatically.

Matplotlib python show() returns immediately

I have a simple python script which plots some graphs in the same figure. All graphs are created by the draw() and in the end I call the show() function to block.
The script used to work with Python 2.6.6, Matplotlib 0.99.3, and Ubuntu 11.04. Tried to run it under Python 2.7.2, Matplotlib 1.0.1, and Ubuntu 11.10 but the show() function returns immediately without waiting to kill the figure.
Is this a bug? Or a new feature and we'll have to change our scripts? Any ideas?
EDIT: It does keep the plot open under interactive mode, i.e., python -i ..., but it used to work without that, and tried to have plt.ion() in the script and run it in normal mode but no luck.
I had this same problem, and it was caused by calling show() on the Figure object instead of the pyplot object.
Incorrect code. Causes the graph to flash on screen for a brief instant:
import matplotlib.pyplot as plt
x = [1,2,3]
y = [5,6,7]
fig = plt.figure()
plt.plot(x, y)
fig.show()
Last line should be as follows to show the graph until it is dismissed:
plt.show()
I think that using show(block=True) should fix your problem.
Had the inverse problem, and it seems that matplotlib will work in interactive or non-interaxctive mode based on a number of things that I could not trace (One way in IDLE, another in system console, one way in normal spyder console, another in a dedicated one ...)
This worked for me:
import matplotlib
matplotlib.interactive(False)
(Actually, I wanted interactive mode, but in your case the inverse should help.)
ion() and ioff() should do the same but the above is on matplotlib's level, not just pyplot or pylab. This works for me although I'm (later) importing pyplot separately and never call matplotlib as such again. I'm thinking that plt.ion() only has an effect on pyplot, not other components of matplotlib that may or may not be involved when using pyplot.
This method works for me on Windows 7, using both Python 2.65 with matplotlib 0.99 and Python 2.75 with matplotlib 1.3.1, across all available python consoles and IDEs on both systems (64-bit, both of them). It did, however, not work on Linux (SuSe 11.3, 64 bit), so there is definitely some platform dependency at play here
To replicate the matplotlib.show() behaviour with the tkagg backend when calling show() on the Figure object:
import Tkinter as Tk
import matplotlib.pyplot as plt
fig = plt.figure()
... your plot commands...
fig.show()
Tk.mainloop()
I had the same problem with this code below.
import matplotlib.pyplot as plt
plt.ion()
fig,ax0 = plt.subplots(figsize=(5.3,4))
plt.show()
I removed plt.ion(), and the plot stays without closing automatically.

matplotlib draw showing nothing

I'm using python's matplotlib to do some contours using contour and contourf functions. They all work fine when using show, but when I try to use draw() inside a method, I get the matplotlib window but not graph. The show() call will be done much later on the code and in a different method, and I would like to show one graph at the moment when it's done with draw(), not having to wait until the much later show(). What I'm doing wrong?
Thanks.
Have you turned interactive mode on using ion()? The following works for me on OSX, using the Tk backend and running from the shell's command line:
import matplotlib.pyplot as plt
plt.ion()
plt.figure()
for i in range(10):
plt.plot([i], [i], 'o')
plt.draw()
raw_input("done >>")
That is, as it does each loop, you see the plot change (i.e., it gets redrawn) as each point is added. Here, btw, if I instead call plt.ioff(), I don't see the figure or any updates.
IIRC ,You should be able call fig.show() multiple times. Also, check out using ipython (ipython -pylab) and http://matplotlib.sourceforge.net/users/shell.html

Categories