Catch matplotlib warning

后端 未结 2 1071
囚心锁ツ
囚心锁ツ 2021-01-04 13:56

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-p         


        
相关标签:
2条回答
  • 2021-01-04 14:17

    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

    0 讨论(0)
  • 2021-01-04 14:34

    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")
    
    0 讨论(0)
提交回复
热议问题