zoukankan      html  css  js  c++  java
  • %pylab的意思

    %pylab的意思

    一、总结

    一句话总结:

    【Load numpy and matplotlib to work interactively.】
    【activate pylab】:This function lets you activate pylab (matplotlib, numpy and interactive support) at any point during an IPython session.
    %pylab makes the following imports:
    import numpy
    import matplotlib
    from matplotlib import pylab, mlab, pyplot
    np = numpy
    plt = pyplot
    
    from IPython.display import display
    from IPython.core.pylabtools import figsize, getfigs
    
    from pylab import *
    from numpy import *

    1、how magic functions work in python?

    【%ls】:to view cwd directory contents
    【begin with the percent sign (%)】:Magic functions in ipython always begin with the percent sign (%) followed without any spaces by a small text string
    【define shortcuts particularly useful for interactive work】:in essence, ipython magic functions define shortcuts particularly useful for interactive work

    二、%pylab的意思

    转自或参考:

    %pylab 是 IPython 中的一个 magic 函数。可以查看代码中 import 了哪些包。

    %pylabIPython中的一个 magic函数。可以查看代码中 import 了哪些包。通过 ipython notebook --pylab inline, 可以让 plot 出来的图片直接嵌入在notebook里面 [1]  。

    ====================================================

    answer one:

    %pylab is a magic function in ipython.

    Magic functions in ipython always begin with the percent sign (%) followed without any spaces by a small text string; in essence, ipython magic functions define shortcuts particularly useful for interactive work, e.g., to give you an idea of how magic functions work in python, a few of my favorites:

    • to view cwd directory contents:

      %ls   
      
    • to run a script in ipython using an empty namespace, type space then a script name:

      %run     
      
    • to execute a code snippet (particularly for multi-line snippets which would usually cause an _IndentationError_ to be thrown):

      %paste
      

    When the %pylab magic function is entered at the IPython prompt, it triggers the import of various modules within Matplotlib.

    Which modules? well, the ones subsumed under the pylab interface.

    The awesome Matplotlib plotting library has two distinct interfaces: a pythonic one, and the original MATLAB-like one intended for plotting at the interactive prompt.

    The former is usually imported like so:

    from matplotlib import pyplot as PLT
    

    Indeed, pyplot has its own magic python magic function

    %pyplot
    

    Why two different interfaces? Matplotlib's original interface was pylab; only later was the pythonic interface added. Scripting and app development were not the primary uses cases for Matplotlib when the project began, plotting in the python shell was.

    Apparently John Hunter (Matplotlib's creator) wanted to include interactive plotting in python so he submitted a patch to Fernando Perez's (FP) IPython project. FP was a Ph.D student at the time and informed JH that he would not able to review the path for some time. As a result, JH created Matplotlib. The significance is that Matplotlib began as a shell-based plotting scheme.

    the pylab interface is indeed more suitable for interactive work:

    from pylab import *
    
    x, y = arange(10), cos(x/2)
    plot(x, y)
    show()
    

    and using the pyplot interface:

    from matplotlib import pyplot as PLT
    import numpy as NP
    
    x, y = NP.arange(10), NP.cos(x/2)
    fig = PLT.figure()
    ax1 = fig.add_subplot(111)
    ax1.plot(x, y)
    PLT.show()
     
    ================================================
     

    answer two:

    %pylab is shortcut for typing all of below commands which in essence adds numpy and matplotlib in to your session. This was added in IPython as a transition tool and current recommendation is that you should not use it. The core reason is that below sets of commands imports too much in the global namespace and also it doesn't allow you to change the mode for matplotlib from UI to QT or something else. You can get history and reasoning behind this at http://nbviewer.ipython.org/github/Carreau/posts/blob/master/10-No-PyLab-Thanks.ipynb?create=1.

    This is what %pylab does:

    import numpy
    import matplotlib
    from matplotlib import pylab, mlab, pyplot
    np = numpy
    plt = pyplot
    
    from IPython.core.pylabtools import figsize, getfigs
    
    from pylab import *
    from numpy import *
    

    This is what I use instead at the start of my notebook:

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    %matplotlib inline
     
    ===============================================================

    answer three:

    %pylab
    %pylab [--no-import-all] [gui]
    

    Load numpy and matplotlib to work interactively.

    This function lets you activate pylab (matplotlib, numpy and interactive support) at any point during an IPython session.

    %pylab makes the following imports:

    import numpy
    import matplotlib
    from matplotlib import pylab, mlab, pyplot
    np = numpy
    plt = pyplot
    
    from IPython.display import display
    from IPython.core.pylabtools import figsize, getfigs
    
    from pylab import *
    from numpy import *
    

    If you pass --no-import-all, the last two * imports will be excluded.

    See the %matplotlib magic for more details about activating matplotlib without affecting the interactive namespace.

    positional arguments:
    gui Name of the matplotlib backend to use (‘gtk’, ‘gtk3’,
    ‘inline’, ‘nbagg’, ‘notebook’, ‘osx’, ‘qt’, ‘qt4’, ‘qt5’, ‘tk’, ‘wx’). If given, the corresponding matplotlib backend is used, otherwise it will be matplotlib’s default (which you can set in your matplotlib config file).
    optional arguments:
    --no-import-all
      Prevent IPython from performing import * into the interactive namespace. You can govern the default behavior of this flag with the InteractiveShellApp.pylab_import_all configurable.
     
     
     
     
    参考:
    https://baike.baidu.com/item/pylab/20436252?fr=aladdin
    https://stackoverflow.com/questions/20961287/what-is-pylab
    https://ipython.org/ipython-doc/dev/interactive/magics.html
     
     
     
     
    我的旨在学过的东西不再忘记(主要使用艾宾浩斯遗忘曲线算法及其它智能学习复习算法)的偏公益性质的完全免费的编程视频学习网站: fanrenyi.com;有各种前端、后端、算法、大数据、人工智能等课程。
    博主25岁,前端后端算法大数据人工智能都有兴趣。
    大家有啥都可以加博主联系方式(qq404006308,微信fan404006308)互相交流。工作、生活、心境,可以互相启迪。
    聊技术,交朋友,修心境,qq404006308,微信fan404006308
    26岁,真心找女朋友,非诚勿扰,微信fan404006308,qq404006308
    人工智能群:939687837

    作者相关推荐

  • 相关阅读:
    bzoj1014: [JSOI2008]火星人prefix
    bzoj1934: [Shoi2007]Vote 善意的投票&&bzoj2768:[JLOI2010]冠军调查
    bzoj2705: [SDOI2012]Longge的问题
    bzoj4653: [Noi2016]区间
    bzoj2456: mode
    bzoj5018: [Snoi2017]英雄联盟
    关于树论【左偏树】
    caioj1522: [NOIP提高组2005]过河
    caioj1421&&hdu2167: [视频]【状态压缩】选数
    Linux下全局安装composer方法
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/13861714.html
Copyright © 2011-2022 走看看