Matplotlib和Basemap:无法导入名称“dedent”

9

我想在Basemap叠加层上绘制一个网络。

我有以下软件包:

  • basemap=1.3.0=py36ha7665c8_0
  • matplotlib=3.3.1=0
  • matplotlib-base=3.3.1=py36hba9282a_0
  • networkx=2.5=py_0

当我只运行以下代码时:
from mpl_toolkits.basemap import Basemap

我会得到以下错误信息:
from matplotlib.cbook import dedent
ImportError: 无法导入名为“dedent”的模块

我尝试了几个不同版本的软件包,但无法找到正确的组合。
有没有人知道哪些matpltlib和basemap的组合是可行的?或者有没有其他方法可以将我的网络绘制在Basemap上?


请问能否尝试降级 matplotlib 库吗? - Karthik
2
https://github.com/matplotlib/basemap/issues/439 - runDOSrun
4个回答

16
我最有效的方法是: 降级到:
pip install -U matplotlib==3.2

8

我在将Python 3.2.7升级到3.3.6时,遇到了导入Basemap的相同错误。

错误消息源于您尝试从mpl_toolkits.basemap中导入Basemap,但是mpl_toolkits.basemap模块需要从matplotlib.cbook模块导入dedent函数,但该函数不在那里。

因此,我想有两个可能的解决方案:注释导入该函数的行或复制它。 我选择了第二个选项。

我不知道为什么matplotlib.cbook模块中没有dedent函数。

这是我在链接上找到的dedent函数,它还包括标题:@deprecated(“3.1”,alternative =“inspect.cleandoc”)

def dedent(s):
    """
    Remove excess indentation from docstring *s*.

    Discards any leading blank lines, then removes up to n whitespace
    characters from each line, where n is the number of leading
    whitespace characters in the first line. It differs from
    textwrap.dedent in its deletion of leading blank lines and its use
    of the first non-blank line to determine the indentation.

    It is also faster in most cases.
    """
    # This implementation has a somewhat obtuse use of regular
    # expressions.  However, this function accounted for almost 30% of
    # matplotlib startup time, so it is worthy of optimization at all
    # costs.
    
    if not s:      # includes case of s is None
        return ''
    
    match = _find_dedent_regex.match(s)
    if match is None:
        return s
    
    # This is the number of spaces to remove from the left-hand side.
    nshift = match.end(1) - match.start(1)
    if nshift == 0:
        return s

    # Get a regex that will remove *up to* nshift spaces from the
    # beginning of each line.  If it isn't in the cache, generate it.
    unindent = _dedent_regex.get(nshift, None)
    if unindent is None:
        unindent = re.compile("\n\r? {0,%d}" % nshift)
        _dedent_regex[nshift] = unindent

    result = unindent.sub("\n", s).strip()
    return result

我从matplotlib网站上的https://matplotlib.org/3.1.1/_modules/matplotlib/cbook.html#dedent复制了函数'dedent',并将其放入了 matplolib\cbook 的 init.py 模块内。现在它能够正常工作。
请注意,要将其复制到正确的行,因为模块中有些变量是预定义好的,例如以下行中的 _find_dedent_regex:
    match = _find_dedent_regex.match(s)

和_dedent_regex用于行:

    unindent = _dedent_regex.get(nshift, None)
    if unindent is None:
        unindent = re.compile("\n\r? {0,%d}" % nshift)
        _dedent_regex[nshift] = unindent

这是我在模块中放置函数的位置

对于我可能犯的拼写和/或语法错误,我深表歉意,我将尽力纠正那些我可能错过并向我报告的错误。

希望这对您有所帮助。


2
欢迎提供解决方案的链接,但请确保您的答案即使没有链接也是有用的:在链接周围添加上下文,以便其他用户了解它的内容和原因,然后引用您链接的页面中最相关的部分,以防目标页面无法访问。仅仅提供链接的答案可能会被删除。 - Sabito stands with Ukraine

1
Matplotlib自3.3.0版本起已经移除了cbook.dedent函数。
升级basemap以解决这个问题: pip install -U basemap

0

我尝试在线寻找不需要更改任何文件的解决方案。在Anaconda环境中使用:

conda install matplotlib=3.2

请注意,Basemap的支持已经停止,应该避免使用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接