using matplotlib giving me the following warning: "UserWarning: tight_layout:

Publish date: 2021-10-22

Try to do a graph using python matplotlib: but keep getting the following waring message:

"UserWaring: tight_layout: falling back to Agg renderer warnings.warn("tight_layout: falling back to Agg renderer") 

My code is below:

plt.legend(loc='upper left',prop = {'size':7},bbox_to_anchor=(1,1)) plt.tight_layout(pad=7) plt.xlabel ('Build') plt.ylabel ('Time/Sec') plt.title ('Performance Test') plt.grid() plt.show() 

graph from the code How to fix that warning message?

1

1 Answer

Are you using MacOSX? It seems to be a known and open issue

https://github.com/matplotlib/matplotlib/issues/1852

I will suggest to reorganize the code such that you will use Figure instead of pyplot. You can get the Figure from plt.figure() method. Then, on the Figure instance call set_tight_layout(True). Try this example code:

import matplotlib matplotlib.use('pdf') import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(1,1,1) fig.set_tight_layout(True) fig.savefig('asd.pdf') # No warning now 

As a side note, have a look at matplotlib documentation

tight_layout() can take keyword arguments of pad, w_pad and h_pad. These control the extra padding around the figure border and between subplots. The pads are specified in fraction of fontsize.

http://matplotlib.org/users/tight_layout_guide.html

This suggest that your code

plt.tight_layout(pad=7)

is wrong, as the value of pad should be between 0 and 1.

1

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoanBrYG6CdoWOrqqippdiuqLAz6WmraSZl3qotdWipaBlnZp6tbTEZp2opJykxKq6xmaumqqenruoedSsnKuvkae7qrrGZquin5ipeq2t2KisrQ%3D%3D