Why on sololearn do you need "plt.savefig("plot.png")" when plotting | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why on sololearn do you need "plt.savefig("plot.png")" when plotting

Why on sololearn do you need "plt.savefig("plot.png")" before the "plt.show()" when plotting from python.? When I run python locally on my iPhone (pythonista 3 IDE) the 'plt.savefig("plot.png")' is not need to display the plot. Also the information on 'matplotlib.pyplot' doesn't mention 'plt.savefig("plot.png")', or at least I haven't found it yet.

24th Jan 2023, 6:34 AM
Rick Shiffman
Rick Shiffman - avatar
2 Answers
+ 5
Hi Rick Shiffman, it is due to the way Sololearn kluged their batch run script in order to enable graphic display. Normally the console output gets redirected to a text file. At the end of run, their script simply dumps the text file to the console. If they tried to dump the graphic output, you only see it rendered in Klingon (i.e., binary characters). So they made a special case to recognize when an image file gets created. The script detects it and enables html rendering in the app, which enables graphic rendering of the image. Thus, saving an image file is only needed in the Sololearn environment.
24th Jan 2023, 7:29 AM
Brian
Brian - avatar
+ 3
Thanks for your answers, I thought it was most likely something particular, and how SoloLearn handles python. I also see saving the plug file would be useful for sharing with others.
24th Jan 2023, 7:47 AM
Rick Shiffman
Rick Shiffman - avatar