Matplotlib绘图backend报错—UserWarning: Matplotlib is currently using agg, which is a non-GUI backend

Preface: matplotlib drawing there 前端(frontend)and 后端(backend), with the front and back end web development that is slightly different, the front end refers to the use python to write code , such as we call the plot function, set up some basic title, legend parameters and so on. The actual drawing and picture display from 0 requires a lot of complicated work, which requires a backend to do these tasks.

There are two types of backends in matplotlib:

  • User interface backends→ Interactive backends, the interactive drawing backend, focusing on rendering
  • Hardcopy backends→ Non-interactive backends, that is, non-interactive drawing backend, focusing on reading and writing

Set backend

The easiest way is to use a use()function, pay attention to the need pyplotto call this method before

import matplotlib as mpl
mpl.use('nbAgg')
import matplotlib.pyplot as plt

View the two backends supported on the current host:

import matplotlib as mpl
  • View supported interactive backends
mpl.rcsetup.interactive_bk
  • View supported non-interactive backends
mpl.rcsetup.non_interactive_bk

For example, the front and back ends supported on my GPU are

Insert picture description here

一、Interactive backends

Use the interactive backend to automatically draw on the screen, which is suitable when you want to draw in real time and perform other operations directly on the picture.
Function to refresh plot: draw()
Official specific parameters:
Insert picture description here
Examples
Insert picture description here

二、Non-interactive backends

Use a non-interactive backend for scenarios where you want to read and write pictures, or display more than two pictures independently

Function to display pictures:show()

Official specific parameter values:
Insert picture description here

Three, error resolution

Problem description: Since Jupyter was used at the beginning

mpl.use('Agg')

So the error is as follows

UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, 
so cannot show the figure

The solution is to find the interactive backend supported by your device, such as mine nbAgg, and then modify it to

mpl.use('nbAgg')

Can

supplement

For drawing styles, there are many kinds of drawing styles available on the official website, and the default parameters aredefault

import matplotlib as mpl
mpl.style.use('default') 

The URL is as follows: https://matplotlib.org/3.2.1/gallery/style_sheets/style_sheets_reference.html

Insert picture description here

Guess you like

Origin blog.csdn.net/SL_World/article/details/109298877