Tag Archives: Numpy-Pandas-Matplotlib

[Solved] Python matplotlib Error: RuntimeError: In set_size: Could not set the fontsize…

Problem
Error when saving image:RuntimeError: In set_size: Could not set the fontsize

Traceback (most recent call last):
  File "/Users/robin/MLcode/Pycharm_Project/tensorflow/2021/0823_face_recognition_environment/0827_img_quality_analysis_v4.py", line 1556, in <module>
    image_cluster_analysis()
  File "/Users/robin/MLcode/Pycharm_Project/tensorflow/2021/0823_face_recognition_environment/0827_img_quality_analysis_v4.py", line 1549, in image_cluster_analysis
    image_showing(img_compressed)
  File "/Users/robin/MLcode/Pycharm_Project/tensorflow/2021/0823_face_recognition_environment/0827_img_quality_analysis_v4.py", line 1408, in image_showing
    plt.savefig(img_name)
  File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/site-packages/matplotlib/pyplot.py", line 722, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/site-packages/matplotlib/figure.py", line 2180, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 2082, in print_figure
    **kwargs)
  File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 579, in print_jpg
    buf, size = self.print_to_buffer()
  File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 535, in print_to_buffer
    FigureCanvasAgg.draw(self)
  File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 388, in draw
    self.figure.draw(self.renderer)
  File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/site-packages/matplotlib/figure.py", line 1709, in draw
    renderer, self, artists, self.suppressComposite)
  File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/site-packages/matplotlib/image.py", line 135, in _draw_list_compositing_images
    a.draw(renderer)
  File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 2607, in draw
    self._update_title_position(renderer)
  File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 2556, in _update_title_position
    if title.get_window_extent(renderer).ymin < top:
  File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/site-packages/matplotlib/text.py", line 890, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)
  File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/site-packages/matplotlib/text.py", line 291, in _get_layout
    ismath="TeX" if self.get_usetex() else False)
  File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 210, in get_text_width_height_descent
    font = self._get_agg_font(prop)
  File "/Users/robin/software/anaconda3/envs/tensorflow/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 250, in _get_agg_font
    font.set_size(size, self.dpi)
RuntimeError: In set_size: Could not set the fontsize

Solution:

The source of the problem is figure_Size and DPI :

The following two codes are different:

figure_size = (6.40, 4.80)
plt.figure(figsize=figure_size, dpi=100)

And:

figure_size = (640, 480)
plt.figure(figsize=figure_size, dpi=1)

Note:

figsize : width, height in inches, default: (6.4, 4.8) , that is, the picture size in inches. The default value in Matplotlib is (6.4, 40.8) DPI : dots (or pixels) per inch, default: 100.0 , that is, the number of pixels per inch. The default value is 100

Although the above two representations seem to be the same, an error is reported by using DPI = 1. (I won’t delve into it for the time being)