Python-igraph学习笔记

hxy    2020-04-22 11:29


        python-igraph是igraph网络分析包的Python版本,具有高效率、兼容性、可用性好等特点,目前支持Python 3和Python 2.7(即将停止更新),是分析网络包除networkx的又一选择。安装命令:
python install python-igraph
安装之后运行示例代码
from igraph import *
g = Graph.Famous("petersen")
plot(g)
出现错误 TypeError:plotting not available,有网友提示需要安装 cairocffi,
pip install cairocffi
安装完成,运行示例代码,再次出现:AttributeError: module 'cairocffi' has no attribute 'Surface'
最后查看了一下官网:

Starting from this release, we will also aim to provide pre-compiled Python wheels for most platforms. The current release includes Python wheels for Python 2.7, 3.5, 3.6, 3.7 and 3.8 on Windows, macOS and Linux, so most users will not have to compile anything when installing python-igraph from pip. Currently the only limitation is that the Windows wheels do not support GraphML import; this will be fixed in the next patch release (0.8.1).

GraphML import目前支持windows系统python3环境支持的最新版到0.8.0,而在下一版本将会得到支持。似乎也没提画图的事儿,只是说不支持导入graphml。继续查看源码提示出错的地方:
def find_cairo():
    """Tries to import the ``cairo`` Python module if it is installed,
    also trying ``cairocffi`` (a drop-in replacement of ``cairo``).
    Returns a fake module if everything fails.
    """
    module_names = ["cairo", "cairocffi"]
    module = FakeModule()
    for module_name in module_names:
        try:
            module = __import__(module_name)
            break
        except ImportError:
            pass
    return module
有一段这样的代码,替代产品是cairo,看看igraph的项目说明:

Project description

Python interface to the igraph high performance graph library, primarily aimed at complex network research and analysis.

Graph plotting functionality is provided by the Cairo library, so make sure you install the Python bindings of Cairo if you want to generate publication-quality graph plots. You can try either pycairo or cairocfficairocffi is recommended, in particular if you are on Python 3.x because there were bug reports affecting igraph graph plots in Jupyter notebooks when using pycairo (but not with cairocffi).

Unofficial installers for 64-bit Windows machines and/or different Python versions can also be found here. Many thanks to the maintainers of this page!

这回发现了确实可以安装 pycairo,虽然官方更推荐cairocffi。于是又试了试下面的命令:
pip install pycairo
提示 error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools。这个提示需要安装 Visual C++,其实应该就是兼容性问题,还不如直接去搜搜pycairo有没有抢先版 pyhon36的包,还真有pycairo-1.19.1-cp36-cp36m-win_amd64.whl
运行下面的命令:
pip install .\pycairo-1.19.1-cp36-cp36m-win_amd64.whl
Processing c:\users\wjr\desktop\pycairo-1.19.1-cp36-cp36m-win_amd64.whl
Installing collected packages: pycairo
Successfully installed pycairo-1.19.1
安装成功。再次运行测试代码,可以画图了,但是提示拒绝访问,看了一下需要以个管理员身份运行或者添加plot()的target参数,可修改示例代码如下:
from igraph import *
g = Graph.Famous("petersen")
plot(g, target='./res.png')
这样就会在本地生成res.png图片,如上图所示。总算成功了,不得不说,igraph的安装确实比NetworkX麻烦一些。

参考资料:
  1. Microsoft Visual C++ 14.0 is required 的解决方案
  2. https://www.lfd.uci.edu/~gohlke/pythonlibs/


 
Last Modified: 2020-04-24 12:27
Views: 7.1K

[[total]] comments

Post your comment
  1. [[item.time]]
    [[item.user.username]] [[item.floor]]Floor
  2. Click to load more...
  3. Post your comment