Python spyder matplotlib UserWarning:对matplotlib.use()的调用无效,因为已选择后端

Python spyder matplotlib UserWarning:对matplotlib.use()的调用无效,因为已选择后端,python,matplotlib,Python,Matplotlib,所以我试图编写一段代码来创建一个图形,但是为了让它在我想要的计算机上运行(school one),我不能使用x-window后端来创建图形。我尝试切换后端使用(我的代码中有matplotlib.use('Agg')语句),但是每当它创建图形时,当我只需要一个色条时,它在图上给了我3个色条。这也给了我错误 UserWarning: This call to matplotlib.use() has no effect because the backend has already been ch

所以我试图编写一段代码来创建一个图形,但是为了让它在我想要的计算机上运行(school one),我不能使用x-window后端来创建图形。我尝试切换后端使用(我的代码中有matplotlib.use('Agg')语句),但是每当它创建图形时,当我只需要一个色条时,它在图上给了我3个色条。这也给了我错误

UserWarning:  This call to matplotlib.use() has no effect
because the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

warnings.warn(_use_error_msg)
我的代码是这样开始的:

import matplotlib
matplotlib.use('Agg')
from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
from pylab import *

有什么想法或帮助吗?

回复晚了,但我想为其他人记录这一点


您可能使用Spyder作为python接口。当您启动Spyder时,它会自动加载这些模块,因此会出现关于如何选择它的错误。这里有一个链接解释了这一点:

您的实际绘图代码是什么?IDE中的某些东西正在为您预导入pyplot。如果您正在编写脚本,我强烈建议不要使用
pyplot
(改用OO接口)我不鼓励您使用pylab import*中的
。我使用的是Juypter笔记本电脑,我收到了类似的错误消息,因此这个问题可能比Spyder有更广泛的应用。