I'm working on an assignment which involves the need to show multiple figures on screen as the script is run (and also to save them). I am having to use both imshow and show to get the images up on the screen. This works fine, but I have read that show() should only be used once per script. Is there another way to display the image? The saved image files are also saving as blank 800x600 white images. Here's my code:
img = np.zeros((100,100))
plt.figure(0)
plt.imshow(img)
plt.show()
plt.savefig("images/img.png")
plt.close(0)
Each other figure is following the same syntax (obviously with different image names and a new figure number.
Thanks!
Generally, your approach using figure() to create a new figure object for each figure you want to display on the screen and save to a file is fine, if this is what you want to hear.
I'm not sure what your actual question is in this respect, so I would seriously recommend editing your question if there is something else you want to know.
Regarding the second issue: depending on the backend in use, show() may destroy objects in the figure (upon closing), which is why you generally should first savefig() and then show(). This is documented here.
Related
I am struggling with saving figures with ppi.
I know how to save figs with dpi and set figure size but have no idea with ppi.
When I search, I could know the dictionary definition but not related to setting codes.
If I want to set the figure size as 8cm*10cm with 2800 resolution in pixels (or 500ppi),
how can I set the figure and save figs?
The below code is the way I usually made.
e.g.
cm=1/2.54
fig=plt.figure(figsize=(8*cm,10*cm))
(my code)
plt.savefig('Name.jpg', dpi=300)
I tried to search the way to save figures in ppi, but most reviews were related to setting dpi with changed fig size.
I know they are connected each other but it was hard to understand how I can set the code.
I am expecting to make a code with fixed fig size and ppi.
Here is an image of my code:
When I open my file explorer and try to view this image only a plain white image is displayed:
You can see that my bar chart images have saved just fine, so I'm a bit confused as to why the pie charts aren't viewable.
Anyone know what the problem might be??
I'm not completely sure if this is the solution but perhaps try and remove the bit where it says plt.show(). I think this might be preventing the image from saving.
You must write like this:
plt.savefig("Visualsations/plot2.png",dpi=300)
plt.show()
Save the figure first, and then use plt.show() to show it. The order matters; always check it.
I made a bunch of plots using stuff like
plot_to_delete_later = mlab.points3d(...)
I want to remove this later on, but keep the other plots. I can do this in the GUI (see image), but can't seem to find the trick to doing it in code.
Any help much appreciated.
I know there are several questions about this in stackoverflow, but the current solution still has some problems.
I am following the solution provided by http://nbviewer.ipython.org/github/dpsanders/matplotlib-examples/blob/master/colorline.ipynb using LineCollection.
Actually it works. But when I save the figure into PDF file. After zooming in, it looks not very good. The size of PDF file is hundreds of times of simply plot without changing color. I understand why it looks like this. Is there any idea to plot the color-line smoother?
Anyone know how should i work around with that?
I know there is save button which I could do it manually but I am plotting 100+ graph so I hope there will be a way to doing it automatically?
I was using 'TkAgg' backend and I look up for any possible solution around. By using the following at the end of my plot function.
manager = plt.get_current_fig_manager()
manager.resize(*manager.window.maxsize())
plt.savefig(r'C:\Users\310293649\Desktop\PlotFigure\TESTING.png')
plt.show()
EDIT: Tried with this as well but still I was able to plt.show() the figure in the desired way I prefer which is full windows size. But it still automatically save all of my figure in minimize default form.
wm = plt.get_current_fig_manager()
wm.window.state('zoomed')
plt.savefig(r'C:\Users\310293649\Desktop\PlotFigure\TESTING.png')
plt.show()
Below is what I got after plt.show() from above command,
plt.savefig result that I got:
As you can see The code managed to show the plot in max windows size but it still automatically saved the plot with default size So I was wondering if it is possible or is there any solution to save the matplotlib figure in max windows size automatically? OR there is no way I can do that?
Below images is the figure when I done it manually with the save figure button in matplotlib:
EDIT: How to make pylab.savefig() save image for 'maximized' window instead of default size - Most of the answer here refer to showing the figure in full windows form but when it come to saving the max windows automatically(it still save it in the normal size)...you can see the author of the ques have raised up the issue in the comment section of all of the answer but was not answered.
Apparently, this is a bug introduced in Matplotlib 2.0 (January '17).
See here and here.
There seems to be a fairly simple code change to fix this here, but you'll have to apply it yourself since it's not included in any released version yet (it's planned for 2.1.1 2.2).