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
Related
This question is a continuation for this question below
plt.show and plt.savefig give different result
By putting plt.show() infront of plt.savefig(), the plot and save image would be the same.
Is there a way to save the figure which is after plt.show() but not using the plt.show() function due to the blocking it have?
Tried putting block=false but it wont save the same as the plt.show()
Add plt.ion(), this turns on interactive mode for matplotlib. The image will open and the code keeps running.
The plot should come up in a separate tab (at least it does on Spyder). There should be a save button on this here and you can easily save it from there. It should allow multiple plots (if you have them) to be generated and then saved.
The save button is in the top right hand corner.
I am facing couple of issues. First, I wanted all the plots in a separate window. For this, I successfully changed the settings and I got the separate window. The problem is, I got all the plots in same figures, which is bad. Second issue is, how do I inscribe window pan to the Ipconsole? I donot want a separate window. I want this window inside the console?
For the first issue, you can have your plots in different figures by using figure this way:
import matplotlib.pyplot as plt
plt.figure()
# Plot your first graph(s)
plt.figure()
# Plot your other graph(s)
plt.show()
Each time you call figure, a new window is created. For more information on figure, you can check the doc
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.
I am drawing a confusion matrix heatmap in Jupyter using code similar to the example here using imshow
Matplotlib is set to draw plots inline.
This works fine for outputting to the cell in the notebook, but I want to not output to the cell but instead get PNG data (ideally raw, not saved to a file) in this case only, not in general (in general I want matplotlib to display inline).
I'm not quite sure how to do that; examples I've seen seem to be global in nature (e.g. calling matplotlib.use() before importing pyplot).
Is this possible? How?
A simple way to not display the plot inline, is to use plt.close() at the end of the cell.
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot([1,2,3],[1,2,3])
plt.savefig("image.png")
plt.close()
Turn off interactive mode:
plt.ioff()
To reactivate inline images, use
plt.ion()
%matplotlib inline
To save the PNG image as bytes, but not to a file, pass a file-like io.BytesIO object to plt.savefig instead of a file:
import io
data = io.BytesIO()
plt.savefig(data)
I want to create a big figure using matplotlib, and then save a few parts of it at different specific coordinates (so manual zooming after plt.show() is not an option - there is no guarantee I can zoom to some precise coordinates - or is there?). The picture is fairly large, so I don't want to generate it all over again and again, specifying xlim and ylim every time before plotting. Is there any way to change axes limits after the figure is created? And I am not using an ipython console, but I need to use it in a script.
There is no problem with using xlim and ylim here. Take the following example:
import matplotlib.pyplot as plt
plt.plot(range(20))
plt.savefig("1.png")
plt.xlim(0,10)
plt.savefig("2.png")
plt.xlim(0,30)
plt.savefig("3.png")
Here a diagonal line is plotted, then we zoom into the first half of the line, then we zoom back out. At each stage a new png file is created. There is no need for redrawing here.