Maplotlib 和底图:无法导入名称“dedent”

Jor*_*t G 4 matplotlib matplotlib-basemap

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

  • 底图=1.3.0=py36ha7665c8_0
  • matplotlib=3.3.1=0
  • matplotlib-base=3.3.1=py36hba9282a_0
  • 网络x=2.5=py_0

当我只运行线路时
from mpl_toolkits.basemap import Basemap

我得到
from matplotlib.cbook import dedent
ImportError: cannot import name 'dedent'

我已经尝试了几个不同版本的软件包,但无法找到正确的功能组合。
任何人对 matpltlib 和底图的组合有任何想法吗?或者另一种在底图上绘制我的网络的方法?

use*_*290 8

当我从 python 3.2.7 传递到 3.3.6 时,我在导入底图时遇到了同样的错误。

该错误消息来自于您尝试从 mpl_toolkits.basemap 导入 Basemap,但 mpl_toolkits.basemap 模块需要从 matplotlib.cbook 模块导入 dedent函数,但该函数不存在。

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

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

这是dedent函数,因为我在我放置的链接上找到了它,它也有 header : @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
Run Code Online (Sandbox Code Playgroud)

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

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

    match = _find_dedent_regex.match(s)
Run Code Online (Sandbox Code Playgroud)

和 _dedent_regex 的行:

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

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

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

我希望这是有用的。

  • 欢迎提供解决方案的链接,但请确保您的答案在没有它的情况下也是有用的:[在链接周围添加上下文](//meta.stackexchange.com/a/8259),这样您的其他用户就会知道它是什么,并且为什么它在那里,然后引用您链接到的页面中最相关的部分,以防目标页面不可用。[仅是链接的答案可能会被删除。](/help/deleted-answers) (2认同)

小智 8

对我来说最好的是:

降级为:

pip install -U matplotlib==3.2
Run Code Online (Sandbox Code Playgroud)