【问题标题】:Maplotlib and Basemap: cannot import name 'dedent'Matplotlib 和底图:无法导入名称“dedent”
【发布时间】:2020-12-29 12:15:39
【问题描述】:

我正在尝试在底图叠加层上绘制网络。 我有包裹:

  • 底图=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: cannot import name 'dedent'

我尝试了几个不同版本的软件包,但无法找到正确的功能组合。
有人对 matpltlib 和底图的组合有什么想法吗?或者以其他方式在底图上绘制我的网络?

【问题讨论】:

标签: matplotlib matplotlib-basemap


【解决方案1】:

最适合我的是:

降级到:

pip install -U matplotlib==3.2

【讨论】:

    【解决方案2】:

    当我从 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 站点复制函数 dedent:https://matplotlib.org/3.1.1/_modules/matplotlib/cbook.html#dedent 在模块 init.py - matplolib\cbook 内。 现在它对我有用。

    注意将其复制到正确的行,因为它的一些变量在模块中预定义,例如:_find_dedent_regex for the line:

        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
    

    This is where I put the function in the moddule

    对于我能做的拼写和/或语法错误,我深表歉意,我会尽我所能纠正那些我会错过并报告给我的。

    我希望这是有用的。

    【讨论】:

    【解决方案3】:

    我试图在网上找到一个不需要更改任何文件的解决方案。在 Anaconda 环境中,使用:

    conda 安装 matplotlib=3.2

    请注意,对底图的支持已停止,应避免使用。

    【讨论】:

      猜你喜欢
      • 2021-01-18
      • 2019-03-29
      • 2021-02-27
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      • 2015-12-22
      相关资源
      最近更新 更多