Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

graph from the code How to fix that warning message?

like image 293
Peter Chao Avatar asked May 18 '16 20:05

Peter Chao


People also ask

What does PLT Tight_layout () do?

The tight_layout() function in pyplot module of matplotlib library is used to automatically adjust subplot parameters to give specified padding.

What is Matplotlib use (' AGG ')?

The last, Agg, is a non-interactive backend that can only write to files. It is used on Linux, if Matplotlib cannot connect to either an X display or a Wayland display.

Where is Matplotlib used?

Matplotlib is a cross-platform, data visualization and graphical plotting library for Python and its numerical extension NumPy. As such, it offers a viable open source alternative to MATLAB. Developers can also use matplotlib's APIs (Application Programming Interfaces) to embed plots in GUI applications.


1 Answers

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.

like image 81
ciropom Avatar answered Oct 06 '22 21:10

ciropom