This question already has answers here:
Matplotlib (pyplot) savefig outputs blank image
(5 answers)
Closed 4 years ago.
EDIT: This question is a duplicate. Original question's link is posted above.
I am using Python to plot a set of values which are showing good on the Python terminal (using Jupyter NoteBook) but when I save it, the saved file when opened shows an empty (completely white) photo. Here's my code:
import matplotlib.pyplot as plt
plt.plot([1,3,4])
plt.show()
plt.savefig('E:/1.png')
You should save the plot before closing it: indeed, plt.show() dislays the plot, and blocks the execution until after you close it; hence when you attempt to save on the next instruction, the plot has been destroyed and can no longer be saved.
import matplotlib.pyplot as plt
plt.plot([1,3,4])
plt.savefig('E:/1.png') # <-- save first
plt.show() # <-- then display
When you execute the show, plt clears the graph. Just invert the execution of show and savefig:
import matplotlib.pyplot as plt
plt.plot([1,3,4])
plt.savefig('E:/1.png')
plt.show()
I think the plt.show() command is "using up" the plot object when you call it. Putting the plt.savefig() command before it should allow it to work.
Also, if you're using Jupyter notebooks you don't even need to use plt.show(), you just need %matplotlib inline somewhere in your notebook to have plots automatically get displayed.
Related
I am using python on a linux shell and trying to save plot instead of displaying (displaying plot window leads to an error). I looked at question Save plot to image file instead of displaying it using Matplotlib, but didn't help. Here is my code:
import matplotlib.pyplot as plt
#
# list3 is list of data
plt.hist(list3, bins=10)
plt.xlabel('X')
plt.ylabel('Y')
fig.savefig('plot.png')
The problem is figure window is appearing even though I don't call plt.figure(). Is there any way to suppress graphical figure window and instead save plot to the file?
plt.savefig('plot.png') saves the png file for me. May be you need to give full path for the file
This question already has an answer here:
view and then close the figure automatically in matplotlib?
(1 answer)
Closed 1 year ago.
So I got a small problem, but yeah, I need an answer. A created a plot with matplotlib, and after the showing I want to close it.
Of course, I visited some documentation (e.g.: https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.close.html), a lot of forums, like that: matplotlib close does not close the window, but my code isn't working for me.
I used the plt.ion() function, but when I tryed it, the plot wasn't appearing, I just saw an empty window.
After that, I used the plt.show(block = False) and I again got an empty window.
You can see the code above:
#Showing
plt.ion()
plt.show(block = False)
time.sleep(10)
plt.close("all")
As you can see, there's a delay, I would like to see a plot for ten seconds, and after close it.
Feel free, to comment to me, I appreciate that, thank you.
Do not use time.sleep(). Use the plt.pause() function.
Details/Explanation: First, you need plt.show(block=False) so that the plot is not blocked and the code executes the next command.
Second, the second command i.e. plt.pause(3) pauses the plot for 3 seconds and then goes to the next line/command.
Finally, the last line/command, plt.close("all") closes the plot automatically.
This is a script (.py) that plots an imshow and automatically close it after 3 seconds.
import matplotlib.pyplot as plt
import numpy as np
X = np.random.rand(10,10)
plt.imshow(X)
plt.show(block=False)
plt.pause(3) # 3 seconds, I use 1 usually
plt.close("all")
I wrote the code above and it is showing my plot in the Ipython console. However, I want to inspect the plot i.e. be able to zoom in/out and have coordinates displayed when moving my cursor.
I know I can do this with executing the file from the location where it is saved. But is there a way to immediately show the plot in a new window when running my file in spyder?
I see that you have pyplot already imported. Run the following in your console:
plt.switch_backend('qt4agg')
If this does not work because the name plt is not recognized, import it in the console:
from matplotlib import pyplot as plt
I am creating a bar chart with seaborn, and it's not generating any sort of error, but nothing happens either.
This is the code I have:
import pandas
import numpy
import matplotlib.pyplot as plt
import seaborn
data = pandas.read_csv('fy15crime.csv', low_memory = False)
seaborn.countplot(x="primary_type", data=data)
plt.xlabel('crime')
plt.ylabel('amount')
seaborn.plt.show()
I added "seaborn.plt.show() in an effort to have it show up, but it isn't working still.
You should place this line somewhere in the top cell in Jupyter to enable inline plotting:
%matplotlib inline
It's simply plt.show() you were close. No need for seaborn
I was using PyCharm using a standard Python file and I had the best luck with the following:
Move code to a Jupyter notebook (which can you do inside of PyCharm by right clicking on the project and choosing new - Jupyter Notebook)
If running a chart that takes a lot of processing time it might not have been obvious before, but in Jupyter mode you can easily see when the cell has finished processing.
This question already has answers here:
warning about too many open figures
(7 answers)
Closed 5 years ago.
I am using Matplotlib and MPLD3 to create graphs that can be displayed in html plages (using django). Currently my graphs are being generated dynamically from data being pulled from csv files. Every so often I get this message in my Terminal:
RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (matplotlib.pyplot.figure) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam figure.max_num_figures).
max_open_warning, RuntimeWarning)
I am not really sure what it means, but I am assuming it means I should have some way of closing graphs that are not in use. Is there anyway to do this or am I completely off base? Thanks.
I preferred the answer of tacaswell in the comments, but had to search for it.
Clean up your plots after you are done with them:
plt.close(fig)
or
plt.close('all')
Figures will automatically be closed (by garbage collection) if you don't create them through the pyplot interface. For example you can create figures using:
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
def new_fig():
"""Create a new matplotlib figure containing one axis"""
fig = Figure()
FigureCanvas(fig)
ax = fig.add_subplot(111)
return fig, ax
(Based on this answer)