Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch matplotlib warning

I have a code (shown below as a minimal working example, MWE) which produces a warning when plotting a colorbar:

/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py:1533: UserWarning: This figure includes Axes that are not compatible with tight_layout, so its results might be incorrect.
  warnings.warn("This figure includes Axes that are not "

I want to catch this warning so it is not displayed.

I know I should apply something along the lines of what is stated in this question How do I catch a numpy warning like it's an exception (not just for testing)?, but I'm not sure how to do it.

Here's the MWE:

import matplotlib.pyplot as plt 
import numpy as np
import matplotlib.gridspec as gridspec

x = np.random.randn(60) 
y = np.random.randn(60)
z = [np.random.random() for _ in range(60)]

fig = plt.figure()
gs = gridspec.GridSpec(1, 2)

ax0 = plt.subplot(gs[0, 0])
plt.scatter(x, y, s=20)

ax1 = plt.subplot(gs[0, 1])
cm = plt.cm.get_cmap('RdYlBu_r')
plt.scatter(x, y, s=20 ,c=z, cmap=cm)
cbaxes = fig.add_axes([0.6, 0.12, 0.1, 0.02]) 
plt.colorbar(cax=cbaxes, ticks=[0.,1], orientation='horizontal')

fig.tight_layout()
plt.show()
like image 671
Gabriel Avatar asked Mar 06 '14 14:03

Gabriel


People also ask

How do you print a warning message in Python?

The warn() function defined in the ' warning ' module is used to show warning messages. The warning module is actually a subclass of Exception which is a built-in class in Python. print ( 'Geeks !' )

How do you stop a runtime warning in Python?

Print the warning the first time it is generated from each module. Print the warning the first time it is generated. Following interactive session sets filter to default by simplefilter() function. In order to temporarily suppress warnings, set simplefilter to 'ignore'.


2 Answers

You probably don't want to catch this warning as an exception. That will interrupt the function call.

Use the warnings standard library module to control warnings.

You can suppress a warning from a specific function call using a context manager:

import warnings
with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    fig.tight_layout()

To ignore all warnings from matplotlib:

warnings.filterwarnings("ignore", module="matplotlib")

To ignore only UserWarnings from matplotlib:

warnings.filterwarnings("ignore", category=UserWarning, module="matplotlib")
like image 113
Bryce Guinta Avatar answered Sep 27 '22 17:09

Bryce Guinta


The printing of warning messages is done by calling showwarning(), which may be overridden; the default implementation of this function formats the message by calling formatwarning(), which is also available for use by custom implementations.

Override the showwarning() method to do nothing when the warning is issued. The function has the message and category of the warning available to it when called, so you can check and only hide the warnings from matplotlib.

Source: http://docs.python.org/2/library/warnings.html#warnings.showwarning

like image 35
Eric Urban Avatar answered Sep 27 '22 19:09

Eric Urban