Hướng dẫn python save figure object
When using
That gives a rasterized or vectorized output respectively. In addition, there is sometimes undesirable whitespace around the image, which can be removed with:
Note that if showing the plot,
Mateen Ulhaq 22.2k16 gold badges86 silver badges127 bronze badges answered Mar 27, 2012 at 13:35
2 As others have said, However I've found that in certain cases the figure is always shown. (eg. with Spyder having
You should be able to re-open the figure later if needed to with answered Apr 28, 2015 at 22:35
DemisDemis 4,7654 gold badges22 silver badges32 bronze badges 1 The solution is:
answered Mar 27, 2012 at 11:36
1 Just found this link on the MatPlotLib documentation addressing exactly this issue: http://matplotlib.org/faq/howto_faq.html#generate-images-without-having-a-window-appear They say that the easiest way to prevent the figure from popping up is to use a non-interactive backend (eg. Agg), via
I still personally prefer using UPDATE: for Spyder, you usually can't set the backend in this way (Because Spyder usually loads matplotlib early, preventing you from using Instead, use From these two hints: one, two answered Jan 4, 2016 at 0:35
DemisDemis 4,7654 gold badges22 silver badges32 bronze badges 5 If you don't like the concept of the "current" figure, do:
answered Jan 30, 2014 at 18:30
wonder.micewonder.mice 6,8942 gold badges34 silver badges38 bronze badges 5
answered Jun 30, 2015 at 8:38
1
The other answers are correct. However, I sometimes find that I want to open the figure object later. For example, I might want to change the label sizes, add a grid, or do other processing. In a perfect world, I would simply rerun the code generating the plot, and adapt the settings. Alas, the world is not perfect. Therefore, in addition to saving to PDF or PNG, I add:
Like this, I can later load the figure object and manipulate the settings as I please. I also write out the stack with the source-code and NB: Be careful, as sometimes this method generates huge files. answered May 26, 2016 at 11:48
gerritgerrit 21.6k16 gold badges88 silver badges163 bronze badges 2 I used the following:
I found very important to use plt.show after saving the figure, otherwise it won't work.figure exported in png
afroditi 1751 gold badge2 silver badges13 bronze badges answered Apr 5, 2016 at 13:34
After using the plot() and other functions to create the content you want, you could use a clause like this to select between plotting to the screen or to file:
answered Oct 11, 2016 at 22:12
Mark P.Mark P. 3113 silver badges4 bronze badges 1 If, like me, you use Spyder IDE, you have to disable the interactive mode with :
(this command is automatically launched with the scientific startup) If you want to enable it again, use :
answered Aug 29, 2015 at 14:50
CovichCovich 2,3554 gold badges23 silver badges35 bronze badges You can either do:
and remember to let savefig finish before closing the GUI plot. This way you can see the image beforehand. Alternatively, you can look at it with
Alternatively, you can use
answered Sep 23, 2017 at 23:20
NutcrackerNutcracker 3392 silver badges8 bronze badges 1 The Solution :
If you do want to display the image as well as saving the image use:
after answered Mar 27, 2016 at 8:49
According to question Matplotlib (pyplot) savefig outputs blank image. One thing should note: if you use A detailed example:
answered Jul 20, 2018 at 9:38
JayhelloJayhello 5,2953 gold badges46 silver badges54 bronze badges When using
answered May 11, 2020 at 8:24
In Jupyter Notebook you have to remove
Francesco Boi 7,44412 gold badges67 silver badges109 bronze badges answered Nov 17, 2018 at 20:01
PunnerudPunnerud 5,9422 gold badges47 silver badges41 bronze badges 0 Additionally to those above, I added
answered Jul 12, 2019 at 13:11
r_er_e 2223 silver badges14 bronze badges Just a extra note because I can't comment on posts yet. If you are using You may not notice if your plots are similar as it will plot over the previous plot, but if you are in a loop saving your figures the plot will slowly become massive and make your script very slow. answered Jun 29, 2021 at 15:37
WoodyetWoodyet 711 silver badge1 bronze badge Given that today (was not available when this question was made) lots of people use Jupyter Notebook as python console, there is an extremely easy way to save the plots as answered Dec 14, 2018 at 7:20
ShividShivid 1,1851 gold badge20 silver badges35 bronze badges 1 As suggested before, you can either use:
For saving whatever IPhython image that you are displaying. Or on a different note (looking from a different angle), if you ever get to work with open cv, or if you have open cv imported, you can go for:
But this is just in case if you need to work with Open CV. Otherwise
answered Jan 29, 2020 at 11:46
well, I do recommend using wrappers to render or control the plotting. examples can be mpltex (https://github.com/liuyxpp/mpltex) or prettyplotlib (https://github.com/olgabot/prettyplotlib).
I basically use this decorator a lot for publishing academic papers in various journals at American Chemical Society, American Physics Society, Opticcal Society American, Elsivier and so on. An example can be found as following image (https://github.com/MarkMa1990/gradientDescent): answered Aug 6, 2021 at 16:02
You can do it like this:
answered Aug 20, 2019 at 14:51
You can save your image with any extension(png, jpg,etc.) and with the resolution you want. Here's a function to save your figure.
'fig_id' is the name by which you want to save your figure. Hope it helps:) answered Jun 25, 2019 at 17:36
Rudresh DixitRudresh Dixit 3021 gold badge4 silver badges14 bronze badges using 'agg' due to no gui on server. Debugging on ubuntu 21.10 with gui and VSC. In debug, trying to both display a plot and then saving to file for web UI. Found out that saving before showing is required, otherwise saved plot is blank. I suppose that showing will clear the plot for some reason. Do this:
Instead of this:
answered Feb 4 at 20:40
creesercreeser 3131 gold badge3 silver badges10 bronze badges |