Using Matplotlib Giving Me the Following Warning: "Userwarning: Tight_Layout:

Using Matplotlib Giving Me the Following Warning: "Userwarning: Tight_Layout:

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()

How to fix that warning message?

1

1 Answer

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

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.

This suggest that your code

plt.tight_layout(pad=7)

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

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Maya Lin-Takahashi
Author

Maya Lin-Takahashi

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.