[PYTHON] When generating a large number of graphs with matplotlib, I do not want to display the graph on the screen (jupyter environment)

In conclusion, the solution is plt.close ().

Introduction

In jupyter (Google Colab.) Environment, if you want to generate a large number of graphs and save them as an image file using matplotlib, if you do not want to display the graph in the ** execution result cell (on the screen) of the notebook **there is.

In particular, if you try to display more than 20 graphs, you will get the following warning.

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:8: 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_open_warning).

A note on avoiding this issue.

Current status

The following code is "generate 40 normal random numbers with an average of 50 and a standard deviation of 10 and draw a histogram of them and save them as a png file".

I'm looping 50 times to see the distribution of each set when I try 50 sets.

A large number of graphs are output in the execution result cell


import numpy as np
import matplotlib.pyplot as plt

#Generate 50 image files
for i in range(50) : 
  dat = (np.random.normal(50,10,size=40)).astype(np.int64)
  plt.figure()
  plt.hist(dat,bins=10,range=(0,100))
  plt.yticks(range(0,31,10))
  plt.xlim(0,100)
  plt.ylim(0,30)
  plt.savefig(f'fig{i}.png') #Save as image file

When I execute the above code, ** the warning at the beginning is displayed **, and the execution result cell is lined with a large number of graphs, which makes the notebook difficult to see. At the same time, the notebook (xxxx.ipynb) also contains the image of the execution result, so the file size will also increase.

Solution

The problem is solved by putting plt.close () after outputting the graph to a file plt.savefig (...).

Do not output the graph in the execution result cell


import numpy as np
import matplotlib.pyplot as plt

for i in range(50) : 
  dat = (np.random.normal(50,10,size=40)).astype(np.int64)
  plt.figure()
  plt.hist(dat,bins=10,range=(0,100))
  plt.yticks(range(0,31,10))
  plt.xlim(0,100)
  plt.ylim(0,30)
  plt.savefig(f'fig{i}.png')
  plt.close() #■■■ Addition ■■■

When I execute the above code, nothing is output to the execution result cell. Of course, the file output will be done without any problem.

Recommended Posts

When generating a large number of graphs with matplotlib, I do not want to display the graph on the screen (jupyter environment)
I want to solve the problem of memory leak when outputting a large number of images with Matplotlib
I want to display the number of num_boost_rounds when early_stopping is applied using XGBoost callback (not achieved)
I want to plot the location information of GTFS Realtime on Jupyter! (With balloon)
I want to start a jupyter environment with one command
I want to use a virtual environment with jupyter notebook!
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
Display the graph of tensorBoard on jupyter
I tried to display the altitude value of DTM in a graph
I want to backtest a large number of exchange pairs and strategies at once with Python's backtesting.py
I want to save the trouble of inputting when debugging Paiza's skill check example in a local environment such as Jupyter [Python]
I want to display multiple images with matplotlib.
I want to display only different lines of a text file with diff
What to do when the graph does not appear in jupyter (ipython) notebook
Library for "I want to do that" of data science on Jupyter Notebook
A script that combines margins when pasting a number of graphs on tiles with gnuplot
I want to save a file with "Do not compress images in file" set in OpenPyXL
I want to take a screenshot of the site on Docker using any font
I want to manually create a legend with matplotlib
A tool to follow posters with a large number of likes on instagram [25 minutes to 1 second]
I want to be notified of the connection environment when the Raspberry Pi connects to the network
Make a note of what you want to do in the future with Raspberry Pi
I want to set up a GUI development environment with Python or Golang on Mac
I want to output a beautifully customized heat map of the correlation matrix. matplotlib edition
[Shell startup] I tried to display the shell on the TV with a cheap Linux board G-cluster
[Graph drawing] I tried to write a bar graph of multiple series with matplotlib and seaborn
How to plot a lot of legends by changing the color of the graph continuously with matplotlib
What to do when Japanese is not displayed on matplotlib
[Visualization] I want to draw a beautiful graph with Plotly
I wrote the basic operation of matplotlib with Jupyter Lab
I drew a Python graph using public data on the number of patients positive for the new coronavirus (COVID-19) in Tokyo + with a link to the national version of practice data
How to write when you want to put a number after the group number to be replaced with a regular expression in re.sub of Python
I want to output while converting the value of the type (e.g. datetime) that is not supported when outputting json with python
The goodness of the touch screen disappeared when the tablet PC was made into a Manjaro i3 environment
Manage the overlap when drawing scatter plots with a large amount of data (Matplotlib, Pandas, Datashader)
What to do when a part of the background image becomes transparent when the transparent image is combined with Pillow
A story that I had a hard time displaying graphs on top of each other with matplotlib
[Introduction to StyleGAN] I played with "The Life of a Man" ♬
I want to output the beginning of the next month with Python
I want to do a full text search with elasticsearch + python
I want to check the position of my face with OpenCV!
I want to do ○○ with Pandas
I want to load the pytest fixture as a library somewhere else (pytest may not exist in the environment)
[Small story] How to save matplotlib graphs in a batch with Jupyter
Understand how to display images on Jupyter (utilization of imshow / matplotlib of OpenCV)
I want to sort a list in the order of other lists
I want to express my feelings with the lyrics of Mr. Children
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
I want to stop the automatic deletion of the tmp area with RHEL7
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
Notes on what to do when matplotlib scatter () / scatter3d () does not work
The story of trying to push SSH_AUTH_SOCK obsolete on screen with LD_PRELOAD
Python: I want to measure the processing time of a function neatly
[Words spelled to me when I was in the first grade ①] I'm not afraid to build a programming environment.
I'm an amateur on the 14th day of python, but I want to try machine learning with scikit-learn
I wanted to know the number of lines in multiple files, so I tried to get it with a command
Example of what to do when the sample script does not work (OpenCV-Python)
I want to visualize the transfer status of the 2020 J League, what should I do?
One-liner to create a large number of test files at once on Linux
The story of IPv6 address that I want to keep at a minimum
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X